Showing System.Drawing.Bitmap in MAUI form?

Raul C 20 Reputation points
2023-04-18T15:19:52.75+00:00

Hello, I want to do a MAUI applciation able to read a USB camera from IDS and paint the incoming images in the main form. I'm able already to detect the camera initialise it and receive the incoming images. I'm able, also, to save those incoming images in the memory of the app, inside a System.Drawing.Bitmap object. And, I can put that object in the ViewModel, and also, if required, in the code behind the form, in the .xmln.cs file. But, and here is the real problem, I don't know how to assing that System.Drawing.Bitmap object, which is essentially an image, as a source for the control Image that I have in the MAUI form. The Image control has a Source, of type ImageSource, but I'm unable to find the way to convert my object to something compatible with ImageSource. Evidently, there must be a way to do that, and I guess it is not complicated, but I don't know how to do it. Doues anyone know how to paint that System.Drawing.Bitmap inside that Image control of the MAUI form? Thanks you all, good people.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,589 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,008 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,931 Reputation points Microsoft Vendor
    2023-04-19T06:04:12.6333333+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.