欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net 图片超过指定大小后等比例压缩图片的方法】,下面是详细的讲解!
asp.net 图片超过指定大小后等比例压缩图片的方法
/// <summary>
/// 压缩图片
/// </summary>
/// <returns></returns>
public string ResizePic()
{
#region 压缩图片开始
bool IsImgFile=true; //判断是否为图片文件
string filePathName="123"; //文件存储的路径(文件夹名称)
string fileName="a.jpg"; //上传文件的原始名称
string fileSysName=DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + fileName; //修改后的文件名称
string filePath=""; //文件路径
string strImgPath="/fileupload/"; //上传路径
if (IsImgFile)
{
int maxWidth=600; //图片宽度最大限制
int maxHeight=400; //图片高度最大限制
System.Drawing.Image imgPhoto=
System.Drawing.Image.FromFile(Server.MapPath(strImgPath) + filePathName + "/" + fileSysName);
int imgWidth=imgPhoto.Width;
int imgHeight=imgPhoto.Height;
if (imgWidth > imgHeight) //如果宽度超过高度以宽度为准来压缩
{
if (imgWidth > maxWidth) //如果图片宽度超过限制
{
float toImgWidth=maxWidth; //图片压缩后的宽度
float toImgHeight=imgHeight / (float)(imgWidth / toImgWidth); //图片压缩后的高度
System.Drawing.Bitmap img=new System.Drawing.Bitmap(imgPhoto,
int.Parse(toImgWidth.ToString()),
int.Parse(toImgHeight.ToString()));
string strResizePicName=Server.MapPath(strImgPath) + filePathName + "/_small_" + fileSysName;
img.Save(strResizePicName); //保存压缩后的图片
filePath=strImgPath + filePathName + "/_small_" + fileSysName; //返回压缩后的图片路径
}
}
else
{
if (imgHeight > maxHeight)
{
float toImgHeight1=maxHeight;
float toImgWidth1=imgWidth / (float)(imgHeight / toImgHeight1);
System.Drawing.Bitmap img=new System.Drawing.Bitmap(imgPhoto,
int.Parse(toImgWidth1.ToString()),
int.Parse(toImgHeight1.ToString()));
string strResizePicName=Server.MapPath(strImgPath) + filePathName + "/_small_" + fileSysName;
img.Save(strResizePicName);
filePath=strImgPath + filePathName + "/_small_" + fileSysName;
}
}
}
return filePath;
#endregion
}
关于asp.net 图片超过指定大小后等比例压缩图片的方法的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 【asp】asp.net开发中常见公共捕获异常方式总结(
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
