473,786 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing image maintaining aspect ratio C# .Net Winforms

84 New Member
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 5451
tlhintoq
3,525 Recognized Expert Specialist
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 New Member
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
2009
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 but can't yet figure out how to do it and apply the result into a new name. Thanks
10
4163
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 resolution (ie. large). I have a database that contains references to the pictures. Currently I have to resize the jpgs manually, and then point the ImageUrl property at that jpg using databinding. This works fine. I would like to avoid the resizing step...
5
3867
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
9464
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 ratio * Provide white (or other color) borders at the sides or the top/bottom The last point would be used to allow users to resize the panel to any ratio
2
19376
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 (height) x 780 (wide) So the "preferred size" would be 440 x 780
0
2006
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 single image (see below) but don't have a clue how to apply the same approach in a datalist, perhaps with a custom databinding expression (or what?). I would also prefer not displaying the image if it's blank so being able to
9
4420
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 an image can take up but not stretch the image if it happens to be 150px by 40. I am doing commands such as: <asp:ImageButton ID="CompanyLogo" onClick="Image_Click" Width="200px" Height="50px" runat="server"/>
8
9021
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 determine the maximum area that I can print on. However, the edge of the image invariably gets cut off, as if the HardMargin info is wrong. I posted the code below as I can't understand what I am doing wrong. Is information in e.PageSettings reliable?...
3
14123
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 frame due to differnt aspect ratio. Some graphic designer suggests me to keep the aspect ratio of the original graphic and pad the graphic with empty space to fit into the frame. One example, the fixed frame is 100x60 and the image is 120x120, I...
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.