您的位置:首页 > 其它

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

2009-09-04 09:13 465 查看
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;
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hanghwp/archive/2009/09/04/4517401.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: