print panel

How can I print a panel in windows form which include many other controls

regards



Answer this question

print panel

  • furmangg

    Isn't there a printpage dialog in the toolbox
    Try it.
    How more you try, how better you get

  • Cesar Francisco

    You can draw the panel into a bitmap, then draw the bitmap onto the paper. Something like this:

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)
    Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, Panel1.Width, Panel1.Height))
    e.Graphics.DrawImage(bmp, 0, 0)
    End Sub



  • Sai A

    Keep in mind the DrawToBitmap function has some caveats. Caveats that become an issue with container controls like Panels. See DrawToBitmap z-Order issue.

  • ryan.rogers

    This is off topic for the Visual C# General forum, moving to the Windows Forms General forum.

  • Marasma

    Here is another example.



    DllImport("gdi32.dll")]
    private static extern bool BitBlt(
    IntPtr hdcDest, // handle to destination DC
    int nXDest, // x-coord of destination upper-left corner
    int nYDest, // y-coord of destination upper-left corner
    int nWidth, // width of destination rectangle
    int nHeight, // height of destination rectangle
    IntPtr hdcSrc, // handle to source DC
    int nXSrc, // x-coordinate of source upper-left corner
    int nYSrc, // y-coordinate of source upper-left corner
    System.Int32 dwRop // raster operation code
    );

    private const Int32 SRCCOPY = 0xCC0020;

    private Bitmap memImage;

    private void PrepareImage()
    {
    Graphics graphic = this.CreateGraphics();
    Size s = this.Size;
    memImage = new Bitmap(s.Width, s.Height, graphic);
    Graphics memGraphic = Graphics.FromImage(memImage);
    IntPtr dc1 = graphic.GetHdc();
    IntPtr dc2 = memGraphic.GetHdc();
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width,
    this.ClientRectangle.Height,dc1, 0, 0, SRCCOPY);
    graphic.ReleaseHdc(dc1);
    memGraphic.ReleaseHdc(dc2);
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    PrepareImage();
    printDocument1.Print();
    }

    private void printDocument1_PrintPage(object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
    {
    e.Graphics.DrawImage(memImage,0,0);
    }



  • PedroCGD

    Hail!

    It did work great! Thank you!


  • print panel