C#学习教程:如何将win form保存为image / pdf?分享


如何将win form保存为image / pdf?

嗨所有,我确实有一个胜利forms,它像一个售票机。 我有一个页面,我将获取所有用户信息,然后单击一个名为Generate Bill的按钮将显示一个winform,其中一些图像为ht背景(例如公司徽标名称等),标签控件放置在适当位置,将显示来自上一表格的信息。 如何保存新表格(票证)?

我怎样才能做到这一点?

我的CC.Utilities向Control类添加了DrawToImage()扩展方法,可能对您有所帮助。 我会在一秒内发布一些代码片段。

辅助方法:

 ///  /// Captures an  of the specified window ///  /// The handle of the window to capture /// An  of the specified window public static Image CaptureWindow(IntPtr handle) { IntPtr hdcSrc = User32.GetWindowDC(handle); RECT windowRect = new RECT(); User32.GetWindowRect(handle, 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, InteropConstants.SRCCOPY); Gdi32.SelectObject(hdcDest, hOld); Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); Image image = Image.FromHbitmap(hBitmap); Gdi32.DeleteObject(hBitmap); return image; } 

P / Invokes:

 [DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetWindowRect(IntPtr hWnd, [Out] RECT rect); [DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject); [DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Auto)] public class RECT { public int left; public int top; public int right; public int bottom; } 

扩展方法:

 ///  /// Draws the  to an  ///  /// The  to draw. /// An  of the  public static Image DrawToImage(this Control control) { return Utilities.CaptureWindow(control.Handle); } 

命名空间与复制/粘贴有点混乱,但如果需要更多,则可以查看整个项目,或者这可能足以指向正确的方向。

上述就是C#学习教程:如何将win form保存为image / pdf?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/960153.html

发表评论

邮箱地址不会被公开。 必填项已用*标注