views:

241

answers:

3

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.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?

+1  A: 

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

JDMX
The landscape part works great. I will give the other portion a try though as even on landscape it is a little too wide.
Refracted Paladin
another link to look athttp://www.devx.com/dotnet/Article/22079
JDMX
+2  A: 

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.

Hans Passant
? I will give that a try but to be clear, the image is TOO BIG and as such is chopped off at the edge of the page in printer preview and ultimitaly, the printed page.
Refracted Paladin
No, your image is not too big. No screen shot could ever fill up a printed page, printers have very high resolutions. You just need to draw it smaller. That will make it look better too, the fat pixels you are getting now aren't that pretty.
Hans Passant
I have the image resized now I need to minimize the Padding on the printed page as my image starts a 1/3 of the way across it.
Refracted Paladin
Tweak the rectangle you pass to DrawImage()
Hans Passant
A: 

The padding and margins of the print document are controled in the printDocument1.DefaultPageSettings value.

JDMX