Resize Panel To Print Full Page - c#

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.

Related

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

Print Preview for printing windows form not working in C#

I am trying to screen capture a windows form, provide a print preview and then print it. The form is printing on paper but the prin preview displays the message "Document does not contain any pages"/
Here are the relevant snippets from my code. Please let me know where I am going wrong.
Bitmap bmp; // Variable for Bitmap of actual page to print
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) //Click to Print
{
Graphics g = this.CreateGraphics();
bmp = new Bitmap(this.Size.Width, this.Size.Height, g);
Graphics mg = Graphics.FromImage(bmp);
mg.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
//printPreviewDialog1.ShowDialog();
//printDocument1.Print();
printPreviewDialog1.Document = printDocument1;
// printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bmp, 0, 0);
}

Trying to print a panel in 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);
}
}

print form on the full page instead of a part of it

i want to print my form on the whole page but instead the picture looks like this :
http://i.stack.imgur.com/JSXh2.jpg
it looks very small that's why i need the form to be printed on the whole full page
here is my code :
private void button5_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}
private Bitmap _memoryImage;
private void CaptureScreen()
{
// put into using construct because Graphics objects do not
// get automatically disposed when leaving method scope
using (var myGraphics = CreateGraphics())
{
var s = Size;
_memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
using (var memoryGraphics = Graphics.FromImage(_memoryImage))
{
memoryGraphics.CopyFromScreen(Location.X, Location.Y, 0, 0, s);
}
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;
// choose the smaller of the two scales
var scale = wScale < hScale ? wScale : hScale;
// apply scaling to the image
e.Graphics.ScaleTransform(scale, scale);
// print to default printer's page
e.Graphics.DrawImage(_memoryImage, 0, 0);
}
your help would be appreciated
try this to make a bitmap of your form:
Bitmap bm = new Bitmap(f1.Width, f1.Height);
this.DrawToBitmap(bm, new Rectangle(f1.Location, f1.Size));
maybe that's the problem! Report back if it works!
If you want to print it in landscape format than you have to rotate your bitmap using:
public Bitmap rotateInternalFunction(Bitmap bitmap)
{
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
return bitmap;
}

Categories