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

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 2012-07-17 15:52  110440  阅读(376)  评论(0编辑  收藏  举报

导航