By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

Printing image maintaining aspect ratio C# .Net Winforms

P: 84
Hi,
How can I print an while preserving its aspect ratio. Currently I am using the code below.
Expand|Select|Wrap|Line Numbers
  1.  
  2. private void PrintClicked(object sender, EventArgs e)
  3.         {
  4.             PrintDocument doc = new PrintDocument();
  5.             doc.PrintPage += this.Doc_PrintPage;
  6.  
  7.             PrintDialog dlgSettings = new PrintDialog();
  8.             dlgSettings.Document = doc;
  9.  
  10.             if (dlgSettings.ShowDialog() == DialogResult.OK)
  11.             {
  12.                 doc.Print();
  13.             }
  14.         }
  15.  
  16.         private void Doc_PrintPage(object sender, PrintPageEventArgs e)
  17.         {
  18.  
  19.             Bitmap img = GetScreenAsImage(); // Get the image of screen
  20.             PageSettings ps = e.PageSettings;
  21.             Rectangle tes = ps.Bounds;
  22.             int x = tes.Location.X;
  23.             int y = tes.Location.Y;
  24.             //Reduce width and height by 50 as otherwise the image is outside printable area.
  25.             int width = tes.Size.Width - 50;
  26.             int height = tes.Size.Height - 50 ;
  27.             Rectangle rect = new Rectangle(x, y, width, height);
  28.  
  29.             e.Graphics.DrawImage(img, rect);
  30.  
  31.  
  32.         }
  33.  
  34.  
This prints perfectly except that it losses its aspect ratio. I would be grateful if someone could kindly help me or tell me what to modify?


Thanks in advance:)
Aug 18 '08 #1
Share this Question
Share on Google+
2 Replies


tlhintoq
Expert 2.5K+
P: 3,525
Rather than just reducing the image by an arbitrary 50 percent I think you want to get the printer caps and calculate the reduction to match.

50 percent might work for this one example, for this one printer, but as soon as you shift from a 300dpi printer to a 600dpi printer your output would become half the physical size.

This would also let you set up a ratio to resize the image to. If your image is 4:3 but your printer is set to landscape with legal size in it... you can see where this is going.

With a little work you could even look at the aspect ratio of the image, compare it to the printer and rotate if needed to make fullest use of the paper (landscape image but portrait paper)
Aug 19 '08 #2

P: 84
Rather than just reducing the image by an arbitrary 50 percent I think you want to get the printer caps and calculate the reduction to match.

50 percent might work for this one example, for this one printer, but as soon as you shift from a 300dpi printer to a 600dpi printer your output would become half the physical size.

This would also let you set up a ratio to resize the image to. If your image is 4:3 but your printer is set to landscape with legal size in it... you can see where this is going.

With a little work you could even look at the aspect ratio of the image, compare it to the printer and rotate if needed to make fullest use of the paper (landscape image but portrait paper)

Hi,
Thanks! Would it be possible for you to post some sample code or advise me on what things i need to change. I ideally want to maintain image aspect but resize it to best fit the page and printer without distorting the actual image.


Any help would be appreciated as I am new at prints in .Net:P

Cheers
Aug 20 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.