Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
100
Print a Windows Form
posted

Is there an easy way to print a Windows Form exactly as it appears on the screen using a WinPrintDocument?

  • 200
    posted

    Hi ,

    Try this out. I hope you will fix it

     

    Thanks and Regs

    Saravanan Nagarajan

     

     private static Bitmap memoryImage; 

      public static void InitFormPrintingInPortrait(UltraPrintDocument printDocument1, Graphics graphics, Size formSize,
                                                          int formRectWidth, int formRectHeight)
            {
                CaptureScreen(graphics, formSize, formRectWidth, formRectHeight);

                UltraPrintPreviewDialog printPreviewDialog1 = new UltraPrintPreviewDialog();
                printPreviewDialog1.Document = printDocument1;
                printPreviewDialog1.Show();
            }

     [System.Runtime.InteropServices.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
                );

     public static void CaptureScreen(Graphics graphics, Size formSize, int formRectWdith, int formRectHeight)
            {
                Graphics mygraphics = graphics;
                Size s = formSize;
                memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
                Graphics memoryGraphics = Graphics.FromImage(memoryImage);
                IntPtr dc1 = mygraphics.GetHdc();
                IntPtr dc2 = memoryGraphics.GetHdc();
                BitBlt(dc2, 0, 0, formRectWdith, formRectHeight, dc1, 0, 0, 13369376);
                mygraphics.ReleaseHdc(dc1);
                memoryGraphics.ReleaseHdc(dc2);
            }

     public static void PrintPage(PrintPageEventArgs e)
            {
                e.Graphics.DrawImage(memoryImage, 0, 0);
            }