The issue is to size the picture to fit into given window without distorting it. I found many über complicated solutions on the internet so I ended up writing my own. Here's the algorithm in prose:

Find the factor by which you need to multiply your picture's width and height. Try using outer height / inner height and if the width doesn't fit, use outer width / inner width.
Here's the code fragment.
  1. private float ScaleFactor(Rectangle outer, Rectangle inner)  
  2. {  
  3.     float factor = (float)outer.Height / (float)inner.Height;  
  4.     if ((float)inner.Width * factor > outer.Width) // Switch!  
  5.         factor = (float)outer.Width / (float)inner.Width;  
  6.     return factor;  
  7. }  
To fit picture rectangle (pctRect) to window rectangle (wndRect) call it like this
  1. float factor=ScaleFactor(wndRect, pctRect); // Outer, inner  
  2. RectangleF resultRect=new RectangleF(0,0,pctRect.Width*factor,pctRect.Height*Factor)  

0 comment(s) :

Newer Post Older Post Home

Blogger Syntax Highliter