0

In my application i want to drawing a rectangle on image box using bitmap called myBitmap. and the problem is how to use System.Drawing.bitMap as source for image in C# Wpf .

   private void MyRectangle(System.Drawing.Point p1, System.Drawing.Point p2)
    {
        int var1,var2;
        var1 = Convert.ToInt16(image1.Width);
        var2 = Convert.ToInt16(image1.Height);

        System.Drawing.Bitmap myBitmap = new System.Drawing.Bitmap(var1,var2);

        using (Graphics g = Graphics.FromImage(myBitmap))
        {
            g.Clear(Color.LightBlue);
            g.DrawRectangle(new Pen(Brushes.Red),p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
        }


      // this.image1.Source = myBitmap;

    }
2
  • you may perhaps use shape classes or event geometry in this scenario unless bitmap is the a extreme necessity. Commented Jul 16, 2014 at 7:08
  • Do not use WinForms bitmaps in a WPF application. Use WriteableBitmapEx instead. Commented Jul 16, 2014 at 7:54

1 Answer 1

2

Convert it to BitmapSource, use the BitmapSource as the Source of your Image control.

    public static BitmapSource ConvertToBitmapSource(Bitmap bitmap)
    {
        if (bitmap == null)
            return null;
        //BitmapImage b=new BitmapImage();
        BitmapSource bitSrc = null;
        var hBitmap = IntPtr.Zero;


        try
        {
            hBitmap = bitmap.GetHbitmap();
            bitSrc = Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }
        catch (Win32Exception)
        {
            bitSrc = null;
        }
        catch (ArgumentException)
        {
            if (hBitmap != IntPtr.Zero)
                DeleteObject(hBitmap);
            hBitmap = IntPtr.Zero;
        }
        catch
        {
            if (hBitmap != IntPtr.Zero)
                DeleteObject(hBitmap);
            bitSrc = null;
            hBitmap = IntPtr.Zero;
        }
        finally
        {
            //bitmap.Dispose();
            if (hBitmap != IntPtr.Zero)
                DeleteObject(hBitmap);
        }
        return bitSrc;
    }


    [DllImport("gdi32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool DeleteObject(IntPtr hObject);
Sign up to request clarification or add additional context in comments.

2 Comments

another method you can use public static BitmapImage ConvertToBitmapImage(Image bitmap) { if (bitmap == null) throw new ArgumentNullException("bitmap"); BitmapImage im = new BitmapImage(); try { im.BeginInit(); im.StreamSource = new MemoryStream(); bitmap.Save(im.StreamSource, ImageFormat.Png); im.EndInit(); } catch (Exception ex) { } return im; }
And simply you can convert Bitmap to byte[] and bind to the byte[] directly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.