Tuesday, December 29, 2009

WPF Image and HDC

I had a DLL exporting a function drawing to a HDC. I needed to use that function in my WPF application. This solution needs a reference to System.Drawing.


System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100);
using (var g = System.Drawing.Graphics.FromImage(bmp))
{
IntPtr hdc = g.GetHdc();
//use hdc to draw here
}
var img = new System.Windows.Controls.Image
{
Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
(
bmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
)
};
//use img in wpf

1 comment:

  1. Thank you for your code! It helped me migrating code from WinForms to WPF.

    ReplyDelete