C# PrintWindow返回空数据,使用C++; 我已经成功地获得了PrimTundOW/CODE>在C++中工作,但是当我尝试在C语言中完成时,我得到了一个带有空数据的位图。使用VS 2012 Pro,在.NET 4.0上。这是我的密码: [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags); [DllImport("user32.dll")] public static extern ushort getLastError(); static int count = 0; public Bitmap getWindow(IntPtr windowHandle) { RECT rect; Window.GetWindowRect(windowHandle, out rect); Console.WriteLine(rect.X); Console.WriteLine(rect.Y); Console.WriteLine(rect.Width); Console.WriteLine(rect.Height); Console.WriteLine(); Bitmap bmp = new Bitmap(rect.Width, rect.Height); Graphics gfxBmp = Graphics.FromImage(bmp); IntPtr hdcBitmap = gfxBmp.GetHdc(); bool success = PrintWindow(windowHandle, hdcBitmap, 0); if (!success) { Console.WriteLine("Error copying image"); Console.WriteLine(getLastError()); } bmp.Save("image_" + count++ + ".bmp"); gfxBmp.ReleaseHdc(hdcBitmap); gfxBmp.Dispose(); return bmp; }

C# PrintWindow返回空数据,使用C++; 我已经成功地获得了PrimTundOW/CODE>在C++中工作,但是当我尝试在C语言中完成时,我得到了一个带有空数据的位图。使用VS 2012 Pro,在.NET 4.0上。这是我的密码: [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags); [DllImport("user32.dll")] public static extern ushort getLastError(); static int count = 0; public Bitmap getWindow(IntPtr windowHandle) { RECT rect; Window.GetWindowRect(windowHandle, out rect); Console.WriteLine(rect.X); Console.WriteLine(rect.Y); Console.WriteLine(rect.Width); Console.WriteLine(rect.Height); Console.WriteLine(); Bitmap bmp = new Bitmap(rect.Width, rect.Height); Graphics gfxBmp = Graphics.FromImage(bmp); IntPtr hdcBitmap = gfxBmp.GetHdc(); bool success = PrintWindow(windowHandle, hdcBitmap, 0); if (!success) { Console.WriteLine("Error copying image"); Console.WriteLine(getLastError()); } bmp.Save("image_" + count++ + ".bmp"); gfxBmp.ReleaseHdc(hdcBitmap); gfxBmp.Dispose(); return bmp; },c#,screen-capture,printscreen,C#,Screen Capture,Printscreen,以下是输出: 182 182 664 533 没有打印错误消息,并且由于RECT边界正确,句柄看起来是有效的。我用C++程序在C++中工作,但是与计算器的代码相同,在C<< P/>中不起作用。 我需要它在C#中工作。按要求:-) 尝试将ReleaseHdc直接放在PrintWinow()调用之后 我不得不猜测原因,但这可能类似于“只要HDC被非托管代码锁定,托管代码就无法访问它。因此必须释放HDC才能在C#中看到位图”尝试将ReleaseHdc直接放在PrintWinow()调用之后。()W

以下是输出:

182
182
664
533
没有打印错误消息,并且由于RECT边界正确,句柄看起来是有效的。我用C++程序在C++中工作,但是与计算器的代码相同,在C<< P/>中不起作用。 我需要它在C#中工作。

按要求:-)
尝试将ReleaseHdc直接放在PrintWinow()调用之后


我不得不猜测原因,但这可能类似于“只要HDC被非托管代码锁定,托管代码就无法访问它。因此必须释放HDC才能在C#中看到位图”

尝试将ReleaseHdc直接放在PrintWinow()调用之后。()WTF???那完全奏效了!!把它写下来作为答案,我会接受它来给你分数!