'"Image could not be read." in MigraDocCore on Xamarin Forms

I'm using Xam.Plugin.Media to capture a photo from the camera and I want to use the stream from the image to print it on a pdf. I tried so many ways to do this but no one is working and always is printed a gray rectangle with the "Image could not be read" on the center. Anyone have an ideia how to fix this ?

here is my code

            var paragraph = document.LastSection.AddParagraph();
            paragraph.Format.Alignment = ParagraphAlignment.Center;
            var iimage = DependencyService.Get<IImage>();
            
            using (Stream stream = MyImage.GetStream())
            {
                stream.Position = 0;
                MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource.ImageSourceImpl = iimage.Implementation;
                var foto = MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource.FromStream($"MyImage_{Id}.jpg", () => stream, 100)
                var image = paragraph.AddImage(foto);
                image.LockAspectRatio = false;
                image.Width = "15cm";
                image.Height = "10cm";
            }

Thanks for Helping.

edit 1: I removed the dependen service and Im using MediaPicker instead of Plugin Media and is throwing a NullReferenceException when the ImageSource is created.

Here is the code:

            var paragraph = document.LastSection.AddParagraph();
            paragraph.Format.Alignment = ParagraphAlignment.Center;
            using (Stream stream = await MyImage.OpenReadAsync())
            {
                stream.Position = 0;
                var foto = MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource.FromStream($"{MyImageId}.jpg", () => stream, 100);
                var image = paragraph.AddImage(foto);
                image.LockAspectRatio = false;
                image.Width = "15cm";
                image.Height = "10cm";
            }

here is the stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
at MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource.FromStream (System.String name, System.Func1[TResult] imageStream, System.Nullable1[T] quality) [0x00005] in <491d7d6ccaa64ee5ad963f480330f219>:0
at Myproject.MyClass.MyMethod() '''



Solution 1:[1]

The ImageSource Implementation is null by default, so I added this code to set the ImageSource.Impl, before create the Image Source:

if(MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource.ImageSourceImpl == null)
{
   MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource.ImageSourceImpl = new ImageSharpImageSource<Rgba32>();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matheus