Printing image maintaining aspect ratio C# .Net Winforms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alag20
    New Member
    • Apr 2007
    • 84

    Printing image maintaining aspect ratio C# .Net Winforms

    Hi,
    How can I print an while preserving its aspect ratio. Currently I am using the code below.
    Code:
    private void PrintClicked(object sender, EventArgs e)
            {
                PrintDocument doc = new PrintDocument();
                doc.PrintPage += this.Doc_PrintPage;
    
                PrintDialog dlgSettings = new PrintDialog();
                dlgSettings.Document = doc;
    
                if (dlgSettings.ShowDialog() == DialogResult.OK)
                {
                    doc.Print();
                }
            }
    
            private void Doc_PrintPage(object sender, PrintPageEventArgs e)
            {
    
                Bitmap img = GetScreenAsImage(); // Get the image of screen
                PageSettings ps = e.PageSettings;
                Rectangle tes = ps.Bounds;
                int x = tes.Location.X;
                int y = tes.Location.Y;
                //Reduce width and height by 50 as otherwise the image is outside printable area.
                int width = tes.Size.Width - 50;
                int height = tes.Size.Height - 50 ;
                Rectangle rect = new Rectangle(x, y, width, height);
    
                e.Graphics.DrawImage(img, rect);
    
    
            }
    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:)
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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)

    Comment

    • alag20
      New Member
      • Apr 2007
      • 84

      #3
      Originally posted by tlhintoq
      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

      Comment

      Working...