2010-02-08 59 views
1

HI全部, 我確實有一個勝利的形式,它像一個售票機。 我有一個頁面,我將獲得所有的用戶信息,然後點擊一個名爲Generate Bill的按鈕,將會顯示一個帶有標籤控件的ht背景(如公司徽標名稱等)來自以前表單的信息。如何保存新表單(票證)?如何將圖像保存爲圖像/ pdf?

我該怎麼做?

回答

4

我的CC.Utilities增加了一個DrawToImage()擴展方法到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); 
} 

的命名空間是一點點從複製/粘貼搞砸但是如果您需要更多的東西,您可以查看完整的項目,或者這可能足以讓您指向正確的方向。

+0

謝謝Cory, 會嘗試,並回到你的... – nimi 2010-02-08 06:57:10

+0

聽起來不錯。請注意,'Control'有一個'DrawToBitmap()'方法,可能也適用於你。我寫了這個方法被破壞的情況,比如在一個'WebBrowser'控件上,它總是從'DrawToBitmap()' – 2010-02-08 07:01:36

+0

+1好答案返回一個純白色圖像。如果需要的話,可以使用iTextSharp或類似的工具將圖像添加到PDF文檔中。 – 2010-02-08 13:18:44