按比例缩小图片
来源:互联网 发布:重庆时时彩组六软件 编辑:程序博客网 时间:2024/05/01 07:45
以前转载过一个,不过发现根本不能压缩,现在重新代码。现在可以定义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);
}
}
{
/// <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按比例缩小图片
- 按比例缩小图片
- IOS 图片按比例缩小
- 利用CSS按比例缩小图片
- 利用CSS按比例缩小图片
- css控制图片按比例缩小
- 图片不完全按比例自动缩小
- 将图片按比例缩小显示
- JS 图片按比例缩小
- js按比例放大缩小图片
- 按比例缩小图片类(无组件)
- 图片按比例缩小,并压缩保存
- js让图片自动按比例缩小代码
- Web标准化制作:利用CSS按比例缩小图片
- C#中上传图片按比例自动缩小或放大
- .Net中上传图片按比例自动缩小或放大
- 今天终于注册了,,,,望大家多多捧场
- Sql Server将字符串转换为日期
- 《艺术人生:士兵突击》专访中震撼心灵的几段话
- 设计模式之Strategy模式简析-读书笔记
- Flash 新闻效果(js实现)
- 按比例缩小图片
- 各种车标的来历
- weblogic 设置debug
- Dom4j的入门
- 彩云之巅(一) 回忆的牵绊
- tomcat5.5数据源配置
- asp.net程序模块化
- 北京十大不可不去的隐秘餐厅
- java与模式