Hello,
By referring to this official document, you could see that the Source
of the Image
in MAUI can be a Stream object Image - Load an image from a stream.
With this piece of information, you could refer to the following steps to convert Bitmap objects to Stream: Step 1: Convert Bitmap object to byte[]:
public static byte[] BitmapToBytes(Bitmap Bitmap)
{
MemoryStream ms = null;
try
{
ms = new MemoryStream();
Bitmap.Save(ms, Bitmap.RawFormat);
byte[] byteImage = new Byte[ms.Length];
byteImage = ms.ToArray();
return byteImage;
}
catch (ArgumentNullException ex)
{
throw ex;
}
finally
{
ms.Close();
}
}
Step 2: Convert byte[] object to Stream:
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
Step 3: Display your Bitmap in Image control:
var bytes = BitmapToBytes(bitmap);
var stream = BytesToStream(bytes);
test_img.Source = ImageSource.FromStream(() => stream);
Update: After your feedback and optimization of this solution, you could use the following utility classes to save Bitmap directly to the Stream and read by the Image control:
public static class BitmapHelper
{
public static void BitmapToImage(Bitmap bitmap, Image image)
{
{
using (MemoryStream stream = new MemoryStream())
{
try
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
image.Source = ImageSource.FromStream(() => stream);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
}
Best Regards, Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.