Friday, 6 January 2012

How To Print Form Control


Using System.Runtime.InteropServices;
this utility is used to draw any control on form or whole form ..
this is used in Thermal printing card or barcode and I -card generation .




[DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap Imagedata;
private void CaptureScreen()
{
   Graphics graphicpanel = Panel1.CreateGraphics();
   Size pansize = this.Size;
   Imagedata = new Bitmap(pansize .Width, pansize .Height, graphicpanel );
   Graphics memoryGraphics = Graphics.FromImage(Imagedata);
   IntPtr dc1 = graphicpanel .GetHdc();
   IntPtr dc2 = Imagedata .GetHdc();
   BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
   graphicpanel .ReleaseHdc(dc1);
   Imagedata .ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(Imagedata, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
   CaptureScreen();
   printDocument1.Print();
}

No comments:

Post a Comment