任何人都知道为什么这会一直返回空白图像?我发现这个功能
here.
我将句柄传递给记事本进程/窗口.
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,PRF_CLIENT)) { Console.WriteLine("Success!"); } else { Console.WriteLine("Error: " + Marshal.GetLastWin32Error()); }
解决方法
您可以使用Printwindow代替SendMessage
这是一个例子
public static Image DrawToBitmap(IntPtr handle) { RECT rect = new RECT(); GetwindowRect(handle,ref rect); Bitmap image = new Bitmap(rect.Right - rect.Left,rect.Bottom - rect.Top); using (Graphics graphics = Graphics.FromImage(image)) { IntPtr hDC = graphics.GetHdc(); Printwindow(new HandleRef(graphics,0); graphics.ReleaseHdc(hDC); } return image; } #region Interop [DllImport("USER32.DLL")] private static extern bool Printwindow(HandleRef hwnd,IntPtr hdcBlt,int nFlags); [DllImport("user32.dll",SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetwindowRect(IntPtr hWnd,ref RECT lpRect); [StructLayout(LayoutKind.Sequential)] private struct RECT { public int Left; public int Top; public int Right; public int Bottom; } #endregion
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。