按比例縮放圖片,C#代碼,在實際的圖片處理或網頁上傳程序中,我想這個比較實用,希望對大家有用,費話少說,看代碼:
01private static Size NewSize(int maxWidth, int maxHeight, int width, int height)
02{
03 double w = 0.0;
04 double h = 0.0;
05 double sw = Convert.ToDouble(width);
06 double sh = Convert.ToDouble(height);
07 double mw = Convert.ToDouble(maxWidth);
08 double mh = Convert.ToDouble(maxHeight);
09 if (sw < mw && sh < mh)
10 {
11 w = sw;
12 h = sh;
13 }
14 else if ((sw / sh) > (mw / mh))
15 {
16 w = maxWidth;
17 h = (w * sh) / sw;
18 }
19 else
20 {
21 h = maxHeight;
22 w = (h * sw) / sh;
23 }
24 return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
25}
從書中學來的,懂C#的應該知道怎麼用。