gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:23:38 25 4
gpt4 key购买 nike

大家好,我确实有一个获胜表格,它有点像售票机。我有一个页面,我将在其中获取所有用户信息,然后单击一个名为 Generate Bill 的按钮将显示一个 winform,其中包含一些图像作为 ht 背景(例如公司 Logo 名称等),标签控件放置在将显示的位置以前表格中的信息。如何保存新表格(工单)?

我该怎么做?

最佳答案

我的 CC.UtilitiesDrawToImage() 扩展方法添加到可能对您有帮助的 Control 类。稍后我会发布一些代码片段。

辅助方法:

/// <summary>
/// Captures an <see cref="Image"/> of the specified window
/// </summary>
/// <param name="handle">The handle of the window to capture</param>
/// <returns>An <see cref="Image"/> of the specified window</returns>
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/调用:

[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;
}

扩展方法:

/// <summary>
/// Draws the <see cref="Control"/> to an <see cref="Image"/>
/// </summary>
/// <param name="control">The <see cref="Control"/> to draw.</param>
/// <returns>An <see cref="Image"/> of the <see cref="Control"/></returns>
public static Image DrawToImage(this Control control)
{
return Utilities.CaptureWindow(control.Handle);
}

命名空间因复制/粘贴而有些困惑,但如果您需要更多信息,您可以查看完整的项目,或者这可能足以为您指明正确的方向。

关于c# - 如何将 win form 保存为 image/pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2219985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com