按比例缩小图片
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);
}
}
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);
}
}
相关文章推荐
- JS等比例缩小图片尺寸
- 利用CSS按比例缩小图片
- 判断图片大小,超过就等比例缩小
- JS等比例缩小图片尺寸
- 指定区域的图片自动按比例缩小的js代码(防止页面被图片撑破)
- jQuery: 图片不完全按比例自动缩小
- IOS 图片按比例缩小
- div+css布局中CSS图片大小自动按比例等比例缩小图片不变形解决技巧
- [置顶] 图片不完全按比例自动缩小
- 【CSS实现图片等比例缩小不变形】
- C#中上传图片按比例自动缩小或放大
- Python批量按比例缩小图片脚本分享
- Python批量按比例缩小图片脚本分享
- 图片等比例缩小
- 保持页面美观,同时使图片保持原比例缩小想法!
- 图片加载并按比例缩小到合适大小(非原创)
- JS等比例缩小图片尺寸的实例
- js+css图片自动等比例缩小且垂直居中[v20071208]
- 利用CSS按比例缩小图片
- 图片自动按比例缩小代码(防止页面被图片撑破)