0

I want my program to show the print preview of the panel only but instead its showing the whole paper size. How to set the print preview of this?

    Bitmap MemoryImage;
    public void GetPrintArea(Panel pnl)
    {
        MemoryImage = new Bitmap(pnl.Width, pnl.Height);
        Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
        pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (MemoryImage != null)
        {
            e.Graphics.DrawImage(MemoryImage, 0, 0);
            base.OnPaint(e);
        }
    }
    public void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
    {
        Rectangle pagearea = e.PageBounds;
        e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel1.Width / 2), this.panel1.Location.Y);
    }
    public void Print(Panel pnl)
    {        
        //printDocument1 
        pannel = pnl;
        GetPrintArea(pnl);
        previewdlg.Document = printdoc1;
        previewdlg.ShowDialog();
    }

    private void btnPrintCheque_Click(object sender, EventArgs e)
    {
        Print(panel1); 
    }       
ViFer
  • 283
  • 1
  • 6
  • 23
  • I added this code to my Print() method and it gets the panel size but now half only of the panel appears: printdoc1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Paper Size", CentimeterToPixel(Convert.ToDouble(txtWidth.Text)), CentimeterToPixel(Convert.ToDouble(txtHeight.Text))); – ViFer May 21 '13 at 07:15
  • I got it! I change e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel1.Width / 2), this.panel1.Location.Y); TO e.Graphics.DrawImage(MemoryImage, 0,0); FROM printdoc1_PrintPage FUNCTION. – ViFer May 21 '13 at 07:47
  • Why don't you use a real printer before you commit to doing this? Feeding little bits of paper through the printer tends to not work so well. Best to also take a look at the print-out you get and note the grainy appearance. – Hans Passant May 21 '13 at 08:18
  • Yah it has. :( So how to do it? – ViFer May 21 '13 at 08:58
  • And also I need to rotate it to (-90). I added this code and its successful but it didnt prints. It gives me the error: A generic error occurred in GDI+. :( Here's the code and I added to printdoc1_PrintPage event: e.Graphics.RotateTransform(-90); e.Graphics.DrawImage(MemoryImage, yPrintCoordinate - 750, 0); – ViFer May 21 '13 at 09:00
  • Problem Solved! Now my error is: A generic error occurred in GDI+. :( – ViFer May 21 '13 at 15:12

0 Answers0