Home > Mobile >  How can I display real time view in wpf?
How can I display real time view in wpf?

Time:09-10

I am trying to get real time view and my code is working in windows form but in wpf there is image instead of picturebox.Image. I get an error when I try to make equalization.

Here is the error code: Can not implicitly convert type 'System.Drawing.Bitmap' to 'System.MediaControls.Image'

Here is the related part of the code:

if (btn_GetviewWasClicked)
                {
                    Bitmap image = new Bitmap(width, height);
                    Rectangle rect = new Rectangle(0, 0, width, height);
                    BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                    IntPtr ptr = bmpData.Scan0;
                    Marshal.Copy(buf, 0, ptr, 1200000);
                    image.UnlockBits(bmpData);
                    img_original.Source = image;//Here is the part where the error exist


                    Bitmap image2 = new Bitmap(width, height);
                    Rectangle rect2 = new Rectangle(0, 0, width, height);
                    BitmapData bmpData2 = image2.LockBits(rect2, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                    IntPtr ptr2 = bmpData2.Scan0;
                    Marshal.Copy(bf, 0, ptr2, 1200000);
                    image2.UnlockBits(bmpData2);
                    
                    img_original = image2;////Here is the part where the error exist
                }

CodePudding user response:

Using an extension function, like so

if (btn_GetviewWasClicked)
{
    Bitmap image = new Bitmap(width, height);
    img_original.Source = image.ToImageSource();
}

Where ToImageSource is an extension function

public static class WpfUtils {
    
    public static BitmapImage ToImageSource(this Bitmap src)
    {
        var ms = new MemoryStream();
        src.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
        var image = new BitmapImage();
        image.BeginInit();
        ms.Seek(0, SeekOrigin.Begin);
        image.StreamSource = ms;
        image.EndInit();
        return image;
    }
    
    // Other Utility functions..
}
  • Related