有人知道为什么这会返回一张空白图像吗?我在这里找到了这个函数。
我传入一个记事本进程/窗口的句柄。
public static Image DrawToBitmap(IntPtr handle)
{
Bitmap image = new Bitmap(500, 500, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(image))
{
IntPtr hDC = graphics.GetHdc();
SendMessage(new HandleRef(graphics, handle), WM_PRINT, hDC, PRF_CHILDREN);
graphics.ReleaseHdc(hDC);
}
return image;
}
我这样使用上面的内容:
Image myimage = DrawToBitmap(handle);
myimage.Save("C:\\here.png", ImageFormat.Png);
感谢所有帮助。
更新
我认为我已经成功从SendMessage获取错误代码,使用以下方法:
if (SendMessage(handle, WM_PRINT, hDC, PRF_CLIENT))
{
Console.WriteLine("Success!");
}
else
{
Console.WriteLine("Error: " + Marshal.GetLastWin32Error());
}
我遇到了错误代码8,查找后发现是内存不足?但我的可用内存超过500MB!也许我对此有误解?
this.Handle
时,链接的代码无法工作。 - pickypg