Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How Can printing Contents of Panel Control using Print Dialog Windows Applications C#
Posted
Comments
Krunal Rohit 18-Feb-14 10:29am    
sorry, what ?

1 solution

I might be wrong, but take a look here,
http://rkinfopedia.blogspot.in/2008/07/printing-contents-of-panel-control.html[^]
C#
//Make reference to following Libraries
using System.Drawing;
using System.Drawing.Printing ; 
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging; 

//Declare following Object Variables
PrintDocument printdoc1 = new PrintDocument();
PrintPreviewDialog previewdlg = new PrintPreviewDialog();
Panel pannel = null;

//Declare event handler for printing in constructor
printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage); 

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)
{
    e.Graphics.DrawImage(MemoryImage, 0, 0);
    base.OnPaint(e);
}
void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
{
    Rectangle pagearea = e.PageBounds; 
    e.Graphics.DrawImage(MemoryImage,(pagearea.Width /2 )-(pannel.Width /2) ,pannel.Location.Y);
}

public void Print(Panel pnl)
{
    pannel = pnl;
    GetPrintArea(pnl); 
    previewdlg.Document = printdoc1;
    previewdlg.ShowDialog(); 
}


-KR
 
Share this answer
 
v2
Comments
krishna97 18-Feb-14 10:41am    
Link Will Given Error Not working ..??
Junaid 21-May-17 3:19am    
I have tried this code but I am getting blank printpreviewdialog. Why? Please Help..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900