Print image in .Net winform
I am trying to allow my users to print the current page they are on in our WinForm program. First I take a screenshot of the app and save it to disk. Then I kick off a PrintPreviewDialog
and load it in there.
That all works, except for the fact that it goes off the page! I can't figure out how to change or allow the user to change the page layout for printing to landscaped and/or to "auto fit" the screenshot to 1 page.
private void printDetailsToolStripMenuItem_Click(object sender, EventArgs e)
{
HUD.ShellForm.SaveAsImage("CaseNoteDetails.jpg", ImageFormat.Jpeg);
printPreviewDialog1.PrintPreviewControl.AutoZoom = true;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.AutoSize = true;
printPreviewDialog1.ShowDialog();
}
void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
e.Graphics.DrawImage(Image.FromFile("c:\\CaseNoteDetails.jpg"), x, y);
}
I changed one line to this -->
e.Graphics.DrawImage(Image开发者_Python百科.FromFile("c:\\CaseNoteDetails.jpg"), x, y,1000,750);
and that works except I have almost 1/3 of the printed page as white space. How do I minimize the Padding/Margins so I can use the whole page?
The default scaling for PrintDocument is 1 pixel = 0.01 inch. A typical piece of paper is about 7.5 inches wide (usable space), an image of 750 pixels wide is going to fill it completely. Surely your screen is wider than that.
Use the Graphics.DrawImage(Image, Rectangle) overload instead so you can rescale the image to make it exactly wide enough. PageSettings.Landscape lets you rotate it.
To get landspace
printDocument1.DefaultPageSettings.Landscape = true
For purposes of resizing, have you tried Image.GetThumbnailImage. It looks to be a function that can resize an image to make it fit
Also, take a look at these examples
http://geekswithblogs.net/kakaiya/archive/2005/06/09/42656.aspx
http://www.devx.com/dotnet/Article/22079
The padding and margins of the print document are controled in the printDocument1.DefaultPageSettings value.
精彩评论