您的位置:首页 > 其它

按比例缩小图片

2008-01-04 15:39 190 查看
以前转载过一个,不过发现根本不能压缩,现在重新代码。现在可以定义width和height最大值并按照图片的横或者竖输出。

public static class ImageHelper
{
/// <summary>
/// 按Width或者Height最大值缩小图片,也就是with或这height都不会超过指定的最大值进行缩小
/// </summary>
/// <param name="srcPictureFilePath">被压缩文件路径</param>
/// <param name="tagPicturePath">目标路径</param>
/// <param name="maxWidth">width的最大值</param>
/// <param name="maxHeight">height的最大值</param>
public static void ReduceMaxScale(string srcPictureFilePath,
string tagPicturePath, int maxWidth, int maxHeight)
{
Bitmap srcBitmap = new Bitmap(srcPictureFilePath);
int tagWidth, tagHeight;

if (srcBitmap.Width > srcBitmap.Height)
{
tagWidth = maxWidth;
float scale = maxWidth/Convert.ToSingle(srcBitmap.Width);
tagHeight = Convert.ToInt32(scale*srcBitmap.Height);
}
else
{
tagHeight = maxHeight;
float scale = maxHeight/Convert.ToSingle(srcBitmap.Height);
tagWidth = Convert.ToInt32(scale*srcBitmap.Width);
}
Bitmap tagBitMap = new Bitmap(srcBitmap, tagWidth, tagHeight);
tagBitMap.Save(tagPicturePath);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: