I have a function which captures an image of a specified window handle and then saves the image to a png file. I'm using gdi32.
This works fine, but for a window which has transparency (example: typical fancy game launcher), the transparent areas will be green (or pink depending on TernaryRasterOperations).
Here's the function which does the capture:
private static Image CaptureWindow(IntPtr handle) {
IntPtr hdc = GetWindowDC(handle);
RECT winRect = new RECT();
if (GetWindowRect(handle, out winRect)) {
Size winSize = new Size(winRect.Right - winRect.Left, winRect.Bottom - winRect.Top);
IntPtr hdcDest = CreateCompatibleDC(hdc);
IntPtr hBitmap = CreateCompatibleBitmap(hdc, winSize.Width, winSize.Height);
IntPtr hOld = SelectObject(hdcDest, hBitmap);
//if (TransparentBlt(hdcDest, 0, 0, winSize.Width, winSize.Height, hdc, 0, 0, winSize.Width, winSize.Height, ColorToUint(Color.Green))) {
if (BitBlt(hdcDest, 0, 0, winSize.Width, winSize.Height, hdc, 0, 0, TernaryRasterOperations.SRCCOPY)) {
SelectObject(hdcDest, hOld);
DeleteDC(hdcDest);
ReleaseDC(handle, hdc);
Image img = System.Drawing.Image.FromHbitmap(hBitmap);
DeleteObject(hBitmap);
return img;
}
}
return null;
}
I tried both BitBlt and TransparentBlt (Msimg32).
When saving the image, I've tried:
img.Save("file.png", ImageFormat.Png);
~
img = img.Clone(new Rectangle(new Point(0, 0), img.Size), PixelFormat.Format32bppArgb);
img.Save("file.png", ImageFormat.Png);
~
img.MakeTransparent(Color.Green);
img.Save("file.png", ImageFormat.Png);
I also tried saving using a filestream as suggested somewhere, but all these methods result in the same green being applied to transparent areas.