I want to create an image of a control that is an imported ActiveX control and display that image in WPF. The reason I don't use the windows host is because it is too slow (I want to create a list with these activex controls): so far I have:
public Bitmap CaptureCtrl(Control ctrl)
{
Console.WriteLine("new Bitmap({0:d}, {0:d}, g)", ctrl.Size.Width, ctrl.Size.Height);
Bitmap newImage = new Bitmap(ctrl.Size.Width, ctrl.Size.Height);
Graphics memoryGraphics = Graphics.FromImage(newImage);
var hwnd = ctrl.Handle;
IntPtr src = NativeMethods.GetDC(hwnd);
IntPtr dest = memoryGraphics.GetHdc();
Console.WriteLine("BitBlt(dest, 0, 0, {0:d}, {1:d}, src, {2:d}, {3:d}, 13369376)", ctrl.ClientRectangle.Width, ctrl.ClientRectangle.Height, ctrl.Location.X, ctrl.Location.Y);
NativeMethods.BitBlt(dest, 0, 0, ctrl.ClientRectangle.Width, ctrl.ClientRectangle.Height, src, ctrl.Location.X, ctrl.Location.Y, 13369376);
NativeMethods.ReleaseDC(hwnd, src);
memoryGraphics.ReleaseHdc(dest);
return newImage;
}
and
public static System.Windows.Media.Imaging.BitmapSource ToBitmapSource(this System.Drawing.Bitmap source)
{
System.Windows.Media.Imaging.BitmapSource bitSrc = null;
IntPtr hBitmap = source.GetHbitmap();
try
{
bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
catch (Win32Exception we)
{
bitSrc = null;
Console.WriteLine(we.ToString()+ " data: "+we.Data + " code: " + we.ErrorCode);
}
finally
{
NativeMethods.DeleteObject(hBitmap);
}
return bitSrc;
}
and calling code:
using (var capture = host.CaptureCtrl(this.control))
{
this.image.Source = capture.ToBitmapSource();
}
NativeMethod calls are imported from the DLL and should be clear. Now the problem is that the pictures are just gray and not filled with content (it seems that something is really copied)