我正在使用此代码在后台捕获进程窗口:
IntPtr = Process.GetProcessByName("memu")[0].MainWindowHandle;
RECT rc;
GetClientRect(hwnd, out rc);
IntPtr hdcFrom = GetDC(hwnd);
IntPtr hdcTo = CreateCompatibleDC(hdcFrom);
int Width = rc.right;
int Height = rc.bottom;
Bitmap bmp = null;
IntPtr hBitmap = CreateCompatibleBitmap(hdcFrom, Width, Height);
if (hBitmap != IntPtr.Zero) {
IntPtr hLocalBitmap = SelectObject(hdcTo, hBitmap);
BitBlt(hdcTo, 0, 0, Width, Height, hdcFrom, 0, 0, CopyPixelOperation.SourceCopy);
SelectObject(hdcTo, hLocalBitmap);
DeleteDC(hdcTo);
ReleaseDC(hwnd, hdcFrom);
bmp = Image.FromHbitmap(hBitmap);
DeleteObject(hBitmap);
return bmp;
}
此代码捕获一个名为MEmu的Android模拟器,它使用DirectX来呈现内容。但是这个代码在Windows 10更新到版本16299之后停止工作(之前它正常工作),它仍然可以在启用了Aero模式的Windows 7上运行。
当我在Windows 10 Pro v16299.X中使用此方法时,它只返回一个白色图像,或者返回模拟器“加载屏幕”,而不是正在运行的内容。在Windows 7上,如果我删除Aero模式,它将采取相同的行动,捕获“加载屏幕”,因此看起来像新的Windows 10专业版更新中的透明度工作方式改变了。
我已尝试过所有内容,尝试安装一些模块以强制Aero Mode在Windows 10上运行,尝试使用PrintWindow在后台捕获屏幕,但仍然是相同的。
任何想法可能会发生什么?还是可能的解决方案?或者在最后一个可能破坏该代码的Windows 10 Pro版本中发生了哪些变化?
谢谢!
答案 0 :(得分:1)
希望这可以解决问题。有一种捕获屏幕的方法,该屏幕内置在.net框架中可能有效。不知道它是否可以捕获DirectX内容,但是值得一试。
请注意,此解决方案会捕获当前屏幕,但是您可能可以对其进行修改以仅捕获您感兴趣的区域。
我在这里找到了此解决方案:https://www.c-sharpcorner.com/UploadFile/2d2d83/how-to-capture-a-screen-using-C-Sharp/
private void CaptureMyScreen()
{
try
{
//Creating a new Bitmap object
Bitmap captureBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
//Bitmap captureBitmap = new Bitmap(int width, int height, PixelFormat);
//Creating a Rectangle object which will
//capture our Current Screen
Rectangle captureRectangle = Screen.AllScreens[0].Bounds;
//Creating a New Graphics Object
Graphics captureGraphics = Graphics.FromImage(captureBitmap);
//Copying Image from The Screen
captureGraphics.CopyFromScreen(captureRectangle.Left,captureRectangle.Top,0,0,captureRectangle.Size);
//Saving the Image File (I am here Saving it in My E drive).
captureBitmap.Save(@"E:\Capture.jpg",ImageFormat.Jpeg);
//Displaying the Successfull Result
MessageBox.Show("Screen Captured");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 1 :(得分:1)
对我来说,这适用于 Windows 10 11.02.2021:
static void hflipAndSwapRandB(unsigned char* data, int w, int h, int dim)
{
int y = 0;
int x = 0;
int d;
unsigned char swap = 0;
int hh = h / 2;
for (y = 0; y < hh; y++)
{
for (x = 0; x < w; x++)
{
for (d = 0; d < dim; d++)
{
swap = data[y * dim * w + dim * x + d];
data[y * dim * w + dim * x + d] = data[(h - 1 - y) * dim * w + dim * x + dim - 1 - d];
data[(h - 1 - y) * dim * w + dim * x + dim - 1 - d] = swap;
}
}
}
}
static void copyScreen(unsigned char* pixels_out, int x, int y, int width, int height)
{
HDC hScreenDC = GetDC(GetDesktopWindow());
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
if (width == -1 || height == -1)
{
width = GetDeviceCaps(hScreenDC, HORZRES);
height = GetDeviceCaps(hScreenDC, VERTRES);
}
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, x, y, width, height, hScreenDC, x, y, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width-x;
bi.biHeight = height-y;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
GetDIBits(hMemoryDC, hBitmap, 0, height-y, pixels_out, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
hflipAndSwapRandB(pixels_out, width, height, 3);
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
DeleteObject(hBitmap);
}