BitBlt not capturing windows in Hardware accelerated mode - c#

I'm currently working on capturing window snapshots using GDI32.dll though I'm having an issue with Hardware Accelerated Windows that I was wondering if there was a way to circumvent.
I found this amazing bit of code here:
public static Image CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = User32.GetWindowDC(handle);
Rect windowRect = new Rect();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.Right - windowRect.Left;
int height = windowRect.Bottom - windowRect.Top;
IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);
Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
Gdi32.SelectObject(hdcDest, hOld);
Gdi32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
Image image = Image.FromHbitmap(hBitmap);
Gdi32.DeleteObject(hBitmap);
return image;
}
which works for all windows except my chrome windows. Disabling Hardware Acceleration in chrome fixed this though I would prefer not to have to do this.
Anyone have any suggestions/ solutions for this problem?
Thanks for any and all help,
-Paul

Related

PrintScreen and BitBlt causing screen flicker

I'm trying to take a screenshot of another window specified by a valid hwnd. The procedure works, however the window flickers when the screenshot is taken.
The first function uses PrintScreen, the second BitBlt. Each is called from a routine that takes a screenshot every 10 seconds.
Is there a way to avoid a flicker when using the PrintScreen or BitBlt functions?
Any help would be appreciated.
public Bitmap GetScreenshot(IntPtr ihandle)
{
IntPtr hwnd = ihandle;//handle here
RECT rc;
FocusWindow(ihandle);
GetWindowRect(hwnd, out rc);
Bitmap bmp = new Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top, PixelFormat.Format32bppArgb);
Graphics gfxBmp = Graphics.FromImage(bmp);
IntPtr hdcBitmap;
try
{
hdcBitmap = gfxBmp.GetHdc();
}
catch
{
return null;
}
bool succeeded = PrintWindow(hwnd, hdcBitmap, 0);
gfxBmp.ReleaseHdc(hdcBitmap);
if (!succeeded)
{
gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size));
}
IntPtr hRgn = CreateRectRgn(0, 0, 0, 0);
GetWindowRgn(hwnd, hRgn);
Region region = Region.FromHrgn(hRgn);//err here once
if (!region.IsEmpty(gfxBmp))
{
gfxBmp.ExcludeClip(region);
gfxBmp.Clear(Color.Transparent);
}
gfxBmp.Dispose();
return bmp;
}
public Bitmap CaptureWindowImage(IntPtr hWnd) //, System.Drawing.Rectangle wndRect)
{
IntPtr hWndDc = GetDC(hWnd);
IntPtr hMemDc = CreateCompatibleDC(hWndDc);
RECT rc;
GetWindowRect(hWnd, out rc);
IntPtr hBitmap = CreateCompatibleBitmap(hWndDc, rc.Width, rc.Height);
SelectObject(hMemDc, hBitmap);
BitBlt(hMemDc, 0, 0, rc.Width, rc.Height, hWndDc, 0, 0, TernaryRasterOperations.SRCCOPY);
Bitmap bitmap = Bitmap.FromHbitmap(hBitmap);
DeleteObject(hBitmap);
ReleaseDC(hWnd, hWndDc);
ReleaseDC(IntPtr.Zero, hMemDc);
DeleteDC(hMemDc);
return bitmap;
}

Capture a image in specified region in a window

So my goal is to make my program that can take a picture in a rectangular region in a specific window.
Eg.) If I want to capture the black square (full) and only the square (not the any part of the window Y). How would I achieve this ?.
I have tried to use GDI32.dll but I can't seem to be able to configure it to capture only the black square.
public static Bitmap CaptureWindow(IntPtr handle) {
IntPtr hdcSrc = User32.GetWindowDC(handle);
User32.RECT windowRect = new User32.RECT {
left = 467,
right = 1432,
top = 381,
bottom = 994
};
int width = 965;
int height = 613;
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, windowRect.right, windowRect.bottom, width, height, hdcSrc, windowRect.left, windowRect.top, GDI32.SRCCOPY);
GDI32.SelectObject(hdcDest, hOld);
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
Image img = Image.FromHbitmap(hBitmap);
GDI32.DeleteObject(hBitmap);
return (Bitmap) img;
}

Window capture with embeded (setparent) directx game C#

Goal:
- Capture a winform window containing a directx game window (like screenshot, but specific handle).
Scenario:
- Inside a winform I insert a panel1 normally.
- I used the winapi SETPARENT to insert a directx main game window inside the penel1
Problem:
When capturing the winform window (this.handle), the image of the embedded game in the panel1 does not appear. Only the panel appears.
I can't use the ScreenShot method, because it will get another window over.
Example:
public Image CaptureWindow(IntPtr handle, int imgX = 0, int imgY = 0, int largura = 0, int altura = 0)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
if(largura == 0 || altura == 0)
{
largura = windowRect.right - windowRect.left;
altura = windowRect.bottom - windowRect.top;
}
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, largura, altura);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
//GDI32.BitBlt(hdcDest, 0, 0, largura, altura, hdcSrc, 0, 0, GDI32.SRCCOPY);
GDI32.BitBlt(hdcDest, 0, 0, largura, altura, hdcSrc, imgX, imgY, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
After use SetParent with Directx window, need to refresh the winform.
Just: this.Refresh();

Windows7 Winapi Image returns black clientrect

I am having a problem with capturing the graphics of another proccess, because for some users it is just a pure black screen which I capture. Unfortunately I have no idea why this is happening for some users only. I am using the sub window directly instead of the window handle and I ensured by posting the address of the windowhandle and checking with spy++ that this window handle is actually the right one.
const string className = "BlueStacksApp";
const string windowName = "_ctl.Window";
processMainHWND = process.MainWindowHandle;
clientRectangleHandle = User32.FindWindowEx(processMainHWND, 0, className, windowName);
internal Bitmap MakeSnapshot(IntPtr AppWndHandle, RECT rect)
{
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
IntPtr hdcTo = IntPtr.Zero;
IntPtr hdcFrom = IntPtr.Zero;
IntPtr hBitmap = IntPtr.Zero;
try
{
Bitmap clsRet = null;
// get device context of the window...
hdcFrom = User32.GetWindowDC(AppWndHandle);
// create dc that we can draw to...
hdcTo = GDI32.CreateCompatibleDC(hdcFrom);
hBitmap = GDI32.CreateCompatibleBitmap(hdcFrom, width, height);
// validate
if (hBitmap != IntPtr.Zero)
{
// adjust and copy
IntPtr hLocalBitmap = GDI32.SelectObject(hdcTo, hBitmap);
bool result = GDI32.BitBlt(hdcTo, 0, 0, width, height, hdcFrom, 0, 0, GDI32.SRCCOPY);
GDI32.SelectObject(hdcTo, hLocalBitmap);
// create bitmap for window image...
clsRet = Image.FromHbitmap(hBitmap);
}
return clsRet;
}
finally
{
// release the unmanaged resources
if (hdcFrom != IntPtr.Zero)
User32.ReleaseDC(AppWndHandle, hdcFrom);
if (hdcTo != IntPtr.Zero)
GDI32.DeleteDC(hdcTo);
if (hBitmap != IntPtr.Zero)
GDI32.DeleteObject(hBitmap);
}
}

How to do Alpha Blending at design time for a Compact Framework Control

I'm designing a Compact Framework Control that supports transparency and for the runtime version of the control everything works just find doing the platform invoke on this API:
[DllImport("coredll.dll")]
extern public static Int32 AlphaBlend(IntPtr hdcDest, Int32 xDest, Int32 yDest, Int32 cxDest, Int32 cyDest, IntPtr hdcSrc, Int32 xSrc, Int32 ySrc, Int32 cxSrc, Int32 cySrc, BlendFunction blendFunction);
Obviously a call to "coredll.dll" isn't going to work in the desktop design time experience and for now, when the painting happens, I'm simply detecting that the control is being designed and painting it without any transparency. I would like to be able to give a better design time experience and show that same transparency in the Visual Studio Designer.
I've tried making this platform call:
[DllImport("gdi32.dll", EntryPoint = "GdiAlphaBlend")]
public static extern bool AlphaBlendDesktop(IntPtr hdcDest, int nXOriginDest, int nYOriginDest,
int nWidthDest, int nHeightDest,
IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
BlendFunction blendFunction);
but while it returns true, the result is nothing at all is painted to the design time view.
Any thoughts?
This worked for me to fool the designer to perform AlphaBlend operations.
// PixelFormatIndexed = 0x00010000, // Indexes into a palette
// PixelFormatGDI = 0x00020000, // Is a GDI-supported format
// PixelFormatAlpha = 0x00040000, // Has an alpha component
// PixelFormatPAlpha = 0x00080000, // Pre-multiplied alpha
// PixelFormatExtended = 0x00100000, // Extended color 16 bits/channel
// PixelFormatCanonical = 0x00200000,
// PixelFormat32bppARGB = (10 | (32 << 8) | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical),
// cheat the design time to create a bitmap with an alpha channel
using (Bitmap bmp = new Bitmap(image.Width, image.Height, (PixelFormat)
PixelFormat32bppARGB))
{
// copy the original image
using(Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(image,0,0);
}
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
(PixelFormat)PixelFormat32bppARGB);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 32 bits per pixels.
int bytes = bmp.Width * bmp.Height * 4;
byte[] argbValues = new byte[bytes];
// Copy the ARGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, bytes);
// Set every alpha value to the given transparency.
for (int counter = 3; counter < argbValues.Length; counter += 4)
argbValues[counter] = transparency;
// Copy the ARGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(argbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
gx.DrawImage(bmp, x, y);
}
Sorry that this is not exactly answering your question...but you should check out the UI Framework for .Net Compact Framework, which does alpha blending already.
http://code.msdn.microsoft.com/uiframework

Categories

Resources