Microsoft.Maui.Graphics.Skia DrawImage not working

KSD 5 Reputation points
2023-05-31T01:20:17.0366667+00:00

I'm trying to draw the lat/lng details onto a photo that the user has just taken.

I've managed to get an ICanvas working and the lat/lng drawn onto the canvas, but using DrawImage to get the photo taken onto the canvas is not working.

The result saved is just a blank jpg with the lat/lng printed, no photo.

Can anyone offer some insight as to why the IImage newImage is not being loaded onto the canvas


private async void DrawThisThing(object sender, EventArgs e)
    {
#if IOS || ANDROID || MACCATALYST
        //===================

        setAsset(); // save file details as preference

        if (MediaPicker.Default.IsCaptureSupported)
        {

            GetCurrentLocation(); // get lat long of current location

            FileResult photo = await MediaPicker.CapturePhotoAsync();

            if (photo != null)
            {
                try
                {
                    var fName = projID.Text + "-" + assID.Text + "-" + wDetail.Text + ".jpg";

                    Stream sourceStream = await photo.OpenReadAsync();

                    devOri = DeviceDisplay.Current.MainDisplayInfo.Orientation.ToString();
                    var image = PlatformImage.FromStream(sourceStream);

                    if (image != null)
                    {
                        IImage newImage = image.Downsize(1600, true);

                        double lat = 0.0;
                        double lng = 0.0;
                        
                        var skiaBitmap = new SkiaBitmapExportContext((int)newImage.Width, (int)newImage.Height,72);
                        
                        
                        if (latlong != null)
                        {
                            lat = Math.Round(latlong.Latitude, 6);
                            lng = Math.Round(latlong.Longitude, 6);
                        }
                        else
                        {

                        }
                        ICanvas canvas = skiaBitmap.Canvas;

                        canvas.FontColor = Colors.White;
                        canvas.FontSize = 72;
                        canvas.Font = Font.Default;
                        canvas.DrawImage(newImage, 0,0,newImage.Width,newImage.Height);
                        canvas.DrawString(lat.ToString() + " : " + lng.ToString(), 0, 0, 1600, 100, HorizontalAlignment.Left, VerticalAlignment.Top);
                        canvas.SaveState();

                        using (MemoryStream _result = new MemoryStream())
                        {
                            skiaBitmap.WriteToStream(_result);

                            _result.Position = 0;

                            savetoGallery(_result, fName);

                            fileSaveLoc.Text = fName;
                            
                            if (latlong != null)
                            {
                                latlngRes.Text = lat.ToString() + " : " + lng.ToString();
                                latlong = null;
                            }
                            else
                            {
                                latlngRes.Text = "Unable to Retreive Location";
                            }
                        }

                    }
                }
                catch (Exception ex)
                {
                    await DisplayAlert("Error", ex.Message, "Ok");
                }
            }
        }

#endif
    }
Developer technologies | .NET | .NET MAUI
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.