Reputation: 5507
HI All, I do have a win form, its something like a ticket machine. i have one page where i will be getting all the user information and then click of a button called Generate Bill will show a winform with some image as ht background(say company logo name etc.) with label controls placed in position which will display the information from the previous form. How to save the new form(ticket)?
how can i do this?
Upvotes: 1
Views: 2813
Reputation: 8938
My CC.Utilities adds a DrawToImage()
extension method to the Control
class that might help you. I'll post some code snippets in a sec.
The helper method:
/// <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;
}
The 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;
}
The extension method:
/// <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);
}
The namespace is a little messed up from the copy/paste but you can look at the full project if you need more or this might be enough to point you in the right direction.
Upvotes: 4