How to print a panel in WinFormc#? - c#

I have a panel which have labels and a datagridview. I'm trying to print the panel with its contents using this code
PrintDialog myPrintDialog = new PrintDialog();
System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap(panel2.Width, panel2.Height);
panel2.DrawToBitmap(memoryImage, panel2.ClientRectangle);
myPrintDialog.ShowDialog();
System.Drawing.Printing.PrinterSettings values;
values = myPrintDialog.PrinterSettings;
myPrintDialog.Document = printDocument1;
printDocument1.PrintController = new StandardPrintController();
printDocument1.Print();
printDocument1.Dispose();
But it prints nothing. I remove the datagridview but still prints nothing. Then I change the panel back color but again it prints white page. Kindly Guide me How I do this?

Add this code to PrintPage event and call the Print() method from PrintDocument class object
private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
Bitmap bmp = new Bitmap(panel2.Width, panel2.Height);
panel2.DrawToBitmap(bmp, new Rectangle(0, 0, panel2.Width, panel2.Height));
e.Graphics.DrawImage((Image)bmp, x, y);
}
Call the method like this
PrintDocument doc = new PrintDocument();
doc.PrintPage += this.doc_PrintPage;
PrintDialog dlg = new PrintDialog();
dlg.Document = doc;
if (dlg.ShowDialog() == DialogResult.OK)
{
doc.Print();
}

Related

How can I print image big size in A4 paper c#

I want to print image from picturebox in big size in my current code its print in original size ,
I tried the following code :
private void btnID_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
PrintDocument pdoc = new PrintDocument();
pdoc.PrintPage += doc_printID;
pd.Document = pdoc;
if (pd.ShowDialog() == DialogResult.OK)
pdoc.Print();
}
private void doc_printID(object sender, PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(pictureIDIQAMA.Width, pictureIDIQAMA.Height);
pictureIDIQAMA.DrawToBitmap(bm, new Rectangle(0, 0, pictureIDIQAMA.Width, pictureIDIQAMA.Height));
e.Graphics.DrawImage(bm, 200,400);
bm.Dispose();
}
How can I print the image in bigger size at lease double original size ?
To draw the image that is within the margin of your page
e.Graphics.DrawImage(bm, args.MarginBounds);
or
To draw the image across total area of the page
e.Graphics.DrawImage(bm, args.PageBounds);

How can I check if user has cancelled printing from printing queue?

I am trying to print a WPF component through XPS document writer, There are lots of pages (in 100). It takes lot of time to print. That is why I used Thread.Sleep to simulate that. While printing those pages. User can cancel the print from print queue of windows. I can print it easily but if user cancel it. It throws an exception at
collator.EndBatchWrite();
Exception Message
private void Print_Click(object sender, RoutedEventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
PrintQueue printQueue = printDialog.PrintQueue;
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printQueue);
SerializerWriterCollator collator = writer.CreateVisualsCollator();
collator.BeginBatchWrite();
collator.Write(VisualLE());
Thread.Sleep(20000);
collator.Write(VisualLE());
collator.EndBatchWrite();
}
}
private static ContainerVisual VisualLE()
{
ContainerVisual newPage = new ContainerVisual();
FixedPage fixedPage;
Border border = new Border();
border.Height = 400;
border.Width = 400;
border.BorderThickness = new Thickness(1);
border.BorderBrush = new SolidColorBrush(Color.FromRgb(1, 1, 1));
fixedPage = new FixedPage();
Size size = new Size(600, 600);
fixedPage.Height = size.Height;
fixedPage.Width = size.Width;
fixedPage.Children.Add(border);
fixedPage.Measure(size);
fixedPage.Arrange(new Rect(new Point(), size));
fixedPage.UpdateLayout();
newPage.Children.Add(fixedPage);
return newPage;
}
Printing Queue

Print an image multiple times on the same sheet c#

Hi I would like to print a product barcode in a sheet but if the user write 6 products on stock it must print 6 times this code on the sheet, I have a code but it prints just one code per image, please see it below.
private void generar_codigo_btn_Click(object sender, EventArgs e)
{
string codigo_generado = nom_prd_txt.Text;
BarcodeLib.Barcode Codigo = new BarcodeLib.Barcode();
Codigo.IncludeLabel = true;
codigo_pic.BackgroundImage = Codigo.Encode(BarcodeLib.TYPE.CODE128, codigo_generado, Color.Black, Color.White, 173, 102);
PrintDialog pd = new PrintDialog();
PrintDocument doc = new PrintDocument();
doc.PrintPage += Doc_PrintPage;
pd.Document = doc;
if (pd.ShowDialog() == DialogResult.OK)
doc.Print();
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(codigo_pic.Width, codigo_pic.Height);
codigo_pic.DrawToBitmap(bm, new Rectangle(5, 5, codigo_pic.Width, codigo_pic.Height));
e.Graphics.DrawImage(bm, 0, 0);
bm.Dispose();
}
You have to draw your bar-code multiple times in bitmap
int NoOfTimesToPrint = 5;
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(codigo_pic.Width, codigo_pic.Height);
for(int i=1;i<=NoOfTimesToPrint;i++)
{
codigo_pic.DrawToBitmap(bm, new Rectangle(5, i*5, codigo_pic.Width, codigo_pic.Height));
}
e.Graphics.DrawImage(bm, 0, 0);
bm.Dispose();
}
Please Note you have to Change NoOfTimesToPrint variable in order to work this code as you need
int NumOfLabel = Convert.ToInt16(textBox_StockAddQuntity_StockEntry.Text); /* here for example i set to total copy */
PrintDialog pDlg = new PrintDialog();
pDlg.Document = printDocument_Barcode;
pDlg.AllowSelection = true;
pDlg.AllowSomePages = true;
pDlg.PrinterSettings.Copies = (short)NumOfLabel;
printDocument_Barcode.Print();

Showing Print Preview in C#

Right now, I'm trying to build my form on a PrintDocument, but the only way for me to see where stuff is actually appearing on the page is to print a paper. It works, but I have a lot of stuff I need to add, and I'd rather not waste tons of paper. Right now I have a print dialogue come up, but theres no print preview button. Is there a way I can make one appear? Thanks!
public partial class Form4 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
public Form4()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
}
// The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
PrintDialog pdi = new PrintDialog();
pdi.Document = pd;
if (pdi.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Single yPos = 0;
Single leftMargin = e.MarginBounds.Left;
Single topMargin = e.MarginBounds.Top;
Image img = Image.FromFile("logo.bmp");
Rectangle logo = new Rectangle(40, 40, 50, 50);
using (Font printFont = new Font("Arial", 20.0f))
{
e.Graphics.DrawImage(img, logo);
e.Graphics.DrawString("Header", printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
}
using (SolidBrush blueBrush = new SolidBrush(Color.Black))
{
Rectangle rect = new Rectangle(100, 100, 500, 120);
e.Graphics.FillRectangle(blueBrush, rect);
}
}
// The Windows Forms Designer requires the following procedure.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.printButton = new System.Windows.Forms.Button();
this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example";
printButton.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
printButton.Location = new System.Drawing.Point(32, 110);
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.TabIndex = 0;
printButton.Text = "Print the file.";
printButton.Size = new System.Drawing.Size(136, 40);
printButton.Click += new System.EventHandler(printButton_Click);
this.Controls.Add(printButton);
}
}
You can use a PrintPreviewDialog for this:
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
PrintDialog printdlg = new PrintDialog();
PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();
// preview the assigned document or you can create a different previewButton for it
printPrvDlg.Document = pd;
printPrvDlg.ShowDialog(); // this shows the preview and then show the Printer Dlg below
printdlg.Document = pd;
if (printdlg.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}

Print scaled image c#

I'm trying to print image with this code.
private void Print()
{
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += PrintDocument_PrintPage;
PrintPreviewDialog printDialog = new PrintPreviewDialog();
printDialog.Document = printDocument;
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK) printDocument.Print();
printDocument.PrintPage -= PrintDocument_PrintPage;
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
//e.Graphics.DrawImage(img, e.PageBounds.X, e.PageBounds.Y);
e.Graphics.DrawImage(img, e.PageBounds.X, e.PageBounds.Y,
e.PageBounds.Width, e.PageBounds.Height);
}
When image is small it prints fine, but when its hi-rez (I have image 992*1403) it draws with wrong size.
When I view PreviewDialog or draw it to the pdf file it prints fine, but when I print it on printer it prints wrong sized.
Fixed my problem with this code:
e.Graphics.DrawImage(img, 0, 0, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);
Just a guess, but how will the application know the PageBounds without knowing what type of paper you are printing on?
Have you tried setting your paper size to a specific size using something like this:
printDocument .DefaultPageSettings.PaperSize =
new PaperSize("Custom", someWidth, someHeight);

Categories