Trying to print a panel in C# - c#

I'm trying to print a panel that has pictureboxes, labels and textboxes. I want to print it on A4 paper so the panel has 595x842.
I have tried:
private void print_Click(object sender, EventArgs e)
{
PrintPanel(panel1);
}
private void PrintPanel(Panel pnl)
{
PrintDialog myPrintDialog = new PrintDialog();
System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap(pnl.Width, pnl.Height);
pnl.DrawToBitmap(memoryImage, pnl.ClientRectangle);
if (myPrintDialog.ShowDialog() == DialogResult.OK)
{
System.Drawing.Printing.PrinterSettings values;
values = myPrintDialog.PrinterSettings;
myPrintDialog.Document = printDocument1;
printDocument1.PrintController = new StandardPrintController();
printDocument1.Print();
}
printDocument1.Dispose();
}
When I try to print it I go to the One Note and all I get is a white page. What seems to be the problem?
I modified the code a bit. Now I get some output but it's still not good...
This is the code I used:
private void print_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
doc.Print();
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Panel grd = new Panel();
Bitmap bmp = new Bitmap(grd.Width, grd.Height, grd.CreateGraphics());
grd.DrawToBitmap(bmp, new Rectangle(0, 0, grd.Width, grd.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
}
And I get this:
http://i.stack.imgur.com/HB1EH.png
Can someone help?

You are printing the wrong panel. Your code is creating a new empty panel and trying to print that (which it successfully does). You need to reference the existing panel you want to print:
void doc_PrintPage(object sender, PrintPageEventArgs e) {
using (Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height)) {
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top,
bounds.Width, factor * bounds.Width);
}
}

Related

Getting Printing issue in C# windows forms

I'm trying to print windows forms and it is printing well but the issue is I want to print the form to the whole page. As you guys can see the the end of the page is empty. Is there a way to stretch is or something as solution. I'm new to C# and windows forms and don't know much, kindly anyone help !!. Explanation which is easy to understand.
here is my code:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void Print_Click(object sender, EventArgs e)
{
Panel panel = new Panel();
this.Controls.Add(panel);
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
Bitmap bitmap;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
bitmap = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(bitmap);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}

How to print panel with picturebox and labels in it?

I have a Panel that has exactly 4 picture boxes, and labels inside. I want to print all the contents inside the Panel but it is only showing the two picture boxes - in the preview.
I have this:
But it is only showing this (when I click the print button):
Here is my code for printing:
private void BtnPrint_Click(object sender, EventArgs e)
{
Print(this.pnlID);
}
public void Print(Panel pnl)
{
PrinterSettings ps = new PrinterSettings();
pnlID = pnl;
GetPrintArea(pnl);
prntprvw.Document = printdoc;
printdoc.PrintPage += new PrintPageEventHandler(printdoc_printpage);
prntprvw.ShowDialog();
}
public void printdoc_printpage(Object sender, PrintPageEventArgs e)
{
Rectangle pagearea = e.PageBounds;
e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.pnlID.Width / 2), this.pnlID.Location.Y);
}
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));
}
How can I print all the contents inside the panel?

How to print a group box using print preview?

The code below is showing me half part of the group box in print preview:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
// Bitmap bmp = new Bitmap(groupBox1.ClientRectangle.Width, groupBox1.ClientRectangle.Height);
// groupBox1.DrawToBitmap(bmp, groupBox1.ClientRectangle);
// e.Graphics.DrawImage(bmp, 0, 0);
Bitmap bmp = new Bitmap(groupBox1.Width, groupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
groupBox1.DrawToBitmap(bmp, new Rectangle(0, 0, groupBox1.Width, groupBox1.Height));
e.Graphics.DrawImage((Image) bmp, 0, 0);
}
private void button1_Click(object sender, EventArgs e) {
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument Pd = new PrintDocument();
Pd.PrintPage += this.printDocument1_PrintPage;
ppd.Document = Pd;
ppd.ShowDialog();
}
You need to draw within the bounds of the page.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var g = e.Graphics;
var srcRect = new Rectangle(0, 0, groupBox1.Width, groupBox1.Height);
var desRect = new Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, srcRect.Height);
//Or to draw within the margin
//var desRect = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, srcRect.Height);
using (var bmp = new Bitmap(srcRect.Width, srcRect.Height))
{
groupBox1.DrawToBitmap(bmp, srcRect);
g.DrawImage(bmp, desRect, srcRect, GraphicsUnit.Pixel);
}
}
Also, create the PrintDocument object and register its PrintPage event in the Form's constructor or Load event to avoid repeating that again and again in the button1_Click event. And don't forget to dispose the disposable objects.

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();

Resize Panel To Print Full Page

I have a panel on a tabcontrolpanel that takes up roughly 35% of the screen. I am printing the panel, and it prints the exact size it is on the screen. Is it possible in C# and winforms to scale the image to print on a 8.5x11 peice of paper (even if I have to print it landscape)?
This is my current code --
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += new SPrintPageEventHandler(doc_PrintPage);
doc.Print();
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap image = new Bitmap(panelToPrint.Width, panelToPrint.Height, panelToPrint.CreateGraphics());
panelToPrint.DrawToBitmap(image, new Rectangle(0, 0, panelToPrint.Width, panelToPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)image.Height / (float)image.Width);
e.Graphics.DrawImage(image, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
}
Edit
I tried to modify my syntax to by default print in landscape mode, but I am still getting the same output as before, just the image printing roughly on the top 15% of the page
private void btnPrint_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.DefaultPageSettings.Landscape = true;
PrintDialog pdi = new PrintDialog();
pdi.Document = doc;
if (pdi.ShowDialog() == DialogResult.OK)
{
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
doc.Print();
}
else
{
MessageBox.Show("User cancelled the print job");
}
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap image = new Bitmap(panelToPrint.Width, panelToPrint.Height, panelToPrint.CreateGraphics());
panelToPrint.DrawToBitmap(image, new Rectangle(0, 0, panelToPrint.Width, panelToPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)image.Height / (float)image.Width);
e.Graphics.DrawImage(image, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
}
control.DrawToBitmap does exactly that. It prints the control (as you see it on the screen) to a bitmap. If you want to print it in a different size you have to resize the panel before printing.
A better approach is to draw your data that is contained into the panel directly to the page using graphics methods like DrawString etc.

Categories