保持比例图像缩放简易算法 <转>

public struct PicSize 
{  
  public int Width;  
  public int Height;  
}  
public static PicSize AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)  
{  
  PicSize size = new PicSize();  
  // 原始宽高在指定宽高范围内,不作任何处理  
  if (orgWidth <= spcWidth && orgHeight <= spcHeight)  
  {  
    size.Width = orgWidth;  
    size.Height = orgHeight;  
  }  
  else 
  {  
    // 取得比例系数  
    float w = orgWidth / (float)spcWidth;  
    float h = orgHeight / (float)spcHeight;  
    // 宽度比大于高度比  
    if (w > h)  
    {  
      size.Width = spcWidth;  
      size.Height = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));  
    }  
    // 宽度比小于高度比  
    else if (w < h)  
    {  
      size.Height = spcHeight;  
      size.Width = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));  
    }  
    // 宽度比等于高度比  
    else 
    {  
      size.Width = spcWidth;  
      size.Height = spcHeight;  
    }  
  }  
  return size;  

  原文http://blog.csdn.net/hanghwp/article/details/4517401

posted on   110440  阅读(376)  评论(0编辑  收藏  举报

导航

< 2012年7月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11

统计

点击右上角即可分享
微信分享提示