473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Printing image maintaining aspect ratio C# .Net Winforms

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
2 5395
tlhintoq
3,525 Expert 2GB
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
alag20
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

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

Similar topics

7
by: Useko Netsumi | last post by:
Hi, suppose I'd like to convert my JPG images(in a directory) into a lower resolution and uniform size(while maintaining its aspect ratio), I would I do that in PHP? I've seen code with PHP/GD...
10
by: David W. Simmonds | last post by:
I have a DataList control that has an Image control in the ItemTemplate. I would like to resize the image that goes into that control. I have a series of jpg files that are full size, full...
5
by: Arthur Hsu | last post by:
Hello, I have an ImageButton that refers to an external image. How can I keep that image's aspect ratio when I set the ImageButton's size to 120x120? TIA, Arthur
2
by: Carl Gilbert | last post by:
Hi I am looking for either a component or technique to allow me to do the following: * Provide a panel with a background image * Resize the image to best fit the panel to maintain aspect...
2
by: Farce Milverk | last post by:
Hi, I'm looking for an algorithm to resize an image of arbitrary size to a "fixed" / required width and height. For example, my application requires that images be no larger than 440 pixel...
0
by: mharness | last post by:
Hello All, Does anyone know how to dynamically change the width and height properties of an image in a datalist in order to maintain the original aspect ratio? I've managed to do this with a...
9
by: tshad | last post by:
Is there a way to display images (imageButtons or linkbuttons for instance) as a max size (200px by 50px) and not have it stretch the image? What I want to be able to do is limit the real estate...
8
by: Frank Rizzo | last post by:
I am trying to print huge images (much bigger than target paper). I try and use e.PageSettings.HardMarginX and e.PageSettings.HardMarginY in the PrintDocument's PrintPage event to try and...
3
by: Danny Ni | last post by:
Hi, I am looking for a way to display images with different aspect ratio into frames with fixed width and height, the problem is some images will look distorted if they are forced into fixed...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.