.net上传图片按比例自动缩小或放大
2010-10-31 22:03
661 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Common.Utility
{
/// <summary>
/// 上传图片按比例自动缩小或放大
/// </summary>
public class ImageBigOrSmall
{
//// <summary>
/// 按比例缩小图片,自动计算宽度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intHeight">缩小至高度</param>
public static void SmallPicWidth(string strOldPic, string strNewPic, int intHeight)
{
System.Drawing.Bitmap objPic, objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intWidth = (intHeight / objPic.Height) * objPic.Width;
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
objNewPic.Save(strNewPic);
}
catch (Exception exp) { throw exp; }
finally
{
objPic = null;
objNewPic = null;
}
}
/// <summary>
/// 按比例缩小图片,自动计算高度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
public static void SmallPic(string strOldPic, string strNewPic, int intWidth)
{
System.Drawing.Bitmap objPic, objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intHeight = (intWidth / objPic.Width) * objPic.Height;
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
objNewPic.Save(strNewPic);
}
catch (Exception exp) { throw exp; }
finally
{
objPic = null;
objNewPic = null;
}
}
/**/
/// <summary>
/// 缩小图片
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
/// <param name="intHeight">缩小至高度</param>
public static void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
{
System.Drawing.Bitmap objPic, objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
objNewPic.Save(strNewPic);
}
catch (Exception exp)
{ throw exp; }
finally
{
objPic = null;
objNewPic = null;
}
}
/*
以上两个方法注意:
1:当小于1大于0值转化为int型时值会为零,以至上方法中intWidth 为零,即出现Bitmap方法中参数无效现象,故以上方法仅供参考,还需自己数据类型转化;
2:当strOldPic与strNewPic值一致时,即用新大小图片替换旧图片时,原图片对象objPic 需释放,即:objPic.Dispose();
*/
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Common.Utility
{
/// <summary>
/// 上传图片按比例自动缩小或放大
/// </summary>
public class ImageBigOrSmall
{
//// <summary>
/// 按比例缩小图片,自动计算宽度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intHeight">缩小至高度</param>
public static void SmallPicWidth(string strOldPic, string strNewPic, int intHeight)
{
System.Drawing.Bitmap objPic, objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intWidth = (intHeight / objPic.Height) * objPic.Width;
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
objNewPic.Save(strNewPic);
}
catch (Exception exp) { throw exp; }
finally
{
objPic = null;
objNewPic = null;
}
}
/// <summary>
/// 按比例缩小图片,自动计算高度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
public static void SmallPic(string strOldPic, string strNewPic, int intWidth)
{
System.Drawing.Bitmap objPic, objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
int intHeight = (intWidth / objPic.Width) * objPic.Height;
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
objNewPic.Save(strNewPic);
}
catch (Exception exp) { throw exp; }
finally
{
objPic = null;
objNewPic = null;
}
}
/**/
/// <summary>
/// 缩小图片
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
/// <param name="intHeight">缩小至高度</param>
public static void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
{
System.Drawing.Bitmap objPic, objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
objNewPic.Save(strNewPic);
}
catch (Exception exp)
{ throw exp; }
finally
{
objPic = null;
objNewPic = null;
}
}
/*
以上两个方法注意:
1:当小于1大于0值转化为int型时值会为零,以至上方法中intWidth 为零,即出现Bitmap方法中参数无效现象,故以上方法仅供参考,还需自己数据类型转化;
2:当strOldPic与strNewPic值一致时,即用新大小图片替换旧图片时,原图片对象objPic 需释放,即:objPic.Dispose();
*/
}
}
相关文章推荐
- .Net实现上传图片按比例自动缩小或放大的方法
- .Net实现上传图片按比例自动缩小或放大的方法
- .Net上传图片按比例自动缩小或放大
- .Net中上传图片按比例自动缩小或放大
- .Net中上传图片按比例自动缩小或放大
- C#中上传图片按比例自动缩小或放大
- .net上传图片并按照宽度高度比例缩小显示
- PS 在PS中如何等比例放大缩小图片
- jQuery-图片上传裁剪插件--imgAreaSelect(分析一) 放大缩小
- 图片自动按比例缩小代码(防止页面被图片撑破)
- js+css图片自动等比例缩小且垂直居中[v20071208]
- 利用PHP实现图片等比例放大和缩小的方法详解
- 图片自动按比例缩小代码(防止页面被图片撑破)
- .net(C#)-放大缩小图片尺寸
- css+javascript 图片自动同比例缩小并且实现垂直居中
- js+css图片自动等比例缩小且垂直居中
- 图片会自动按比例缩小(js版和css版)
- 图片自动按比例缩小代码(防止页面被图片撑破)
- 基于jQuery的图片不完全按比例自动缩小