tags:

views:

79

answers:

0

This is a followup to this question.

The code shown there has been very useful in obtaining a bitmap from a window and applying it as a BitmapSource on a WPF Image control. However, I'm finding that many areas of the image are showing up as transparent, particularly any areas of text. I've posted a sample of what I'm seeing here: http://dev.stimulant.io/misc/BitmapCaptureTest.png

On the left is a "live" DWM image, on the right is the static image created from the aforelinked code. The blue portions on the right-hand image are actually transparent in the resulting BitmapSource.

Below is the code used to create the BitmapSource:

IntPtr hdcSrc = Native.GetWindowDC(handle);

Native.RECT windowRect = new Native.RECT();
Native.GetWindowRect(handle, ref windowRect);

int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;

IntPtr hdcDest = Native.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = Native.CreateCompatibleBitmap(hdcSrc, width, height);

IntPtr hold = Native.SelectObject(hdcDest, hBitmap);
Native.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Native.TernaryRasterOperations.SRCCOPY);
Native.SelectObject(hdcDest, hold);
Native.DeleteDC(hdcDest);
Native.ReleaseDC(handle, hdcSrc);

BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

Native.DeleteObject(hBitmap);

return bitmapSource;

How can I get the transparent bits to come through properly? I'm a managed code guy, so all this pinvoke is new to me. All I can think to do is play with the various TernaryRasterOperations settings, but they don't seem to fix the issue.