按比例缩放图片
2006-02-24 10:33
211 查看
/// <summary>
/// 按比例缩放图片
/// </summary>
/// <param name="imgUrl">图片的路径</param>
/// <param name="imgHeight">图片的高度</param>
/// <param name="imgWidth">图片的宽度</param>
/// <returns></returns>
public static string GetImageSize(string imgUrl,int imgHeight,int imgWidth)
{
string fileName = System.Web.HttpContext.Current.Server.MapPath(imgUrl);
string strResult = string.Empty;
if(System.IO.File.Exists(fileName) && imgHeight != 0 && imgWidth != 0)
{
decimal desWidth;decimal desHeight; //目标宽高
System.Drawing.Image objImage = System.Drawing.Image.FromFile(fileName);
decimal radioAct = (decimal)objImage.Width/(decimal)objImage.Height; //原始图片的宽高比
decimal radioLoc = (decimal)imgWidth/(decimal)imgHeight; //图片位的宽高比
if(radioAct > radioLoc) //原始图片比图片位宽
{
decimal dcmZoom = (decimal)imgWidth/(decimal)objImage.Width;
desHeight = objImage.Height*dcmZoom;
desWidth = imgWidth;
}
else
{
decimal dcmZoom = (decimal)imgHeight/(decimal)objImage.Height;
desWidth = objImage.Width*dcmZoom;
desHeight = imgHeight;
}
objImage.Dispose(); //释放资源
strResult = "width=/"" + Convert.ToString((int)desWidth) + "/" height=/""
+ Convert.ToString((int)desHeight) + "/" ";
}
return strResult;
}
/// 按比例缩放图片
/// </summary>
/// <param name="imgUrl">图片的路径</param>
/// <param name="imgHeight">图片的高度</param>
/// <param name="imgWidth">图片的宽度</param>
/// <returns></returns>
public static string GetImageSize(string imgUrl,int imgHeight,int imgWidth)
{
string fileName = System.Web.HttpContext.Current.Server.MapPath(imgUrl);
string strResult = string.Empty;
if(System.IO.File.Exists(fileName) && imgHeight != 0 && imgWidth != 0)
{
decimal desWidth;decimal desHeight; //目标宽高
System.Drawing.Image objImage = System.Drawing.Image.FromFile(fileName);
decimal radioAct = (decimal)objImage.Width/(decimal)objImage.Height; //原始图片的宽高比
decimal radioLoc = (decimal)imgWidth/(decimal)imgHeight; //图片位的宽高比
if(radioAct > radioLoc) //原始图片比图片位宽
{
decimal dcmZoom = (decimal)imgWidth/(decimal)objImage.Width;
desHeight = objImage.Height*dcmZoom;
desWidth = imgWidth;
}
else
{
decimal dcmZoom = (decimal)imgHeight/(decimal)objImage.Height;
desWidth = objImage.Width*dcmZoom;
desHeight = imgHeight;
}
objImage.Dispose(); //释放资源
strResult = "width=/"" + Convert.ToString((int)desWidth) + "/" height=/""
+ Convert.ToString((int)desHeight) + "/" ";
}
return strResult;
}
相关文章推荐
- 将图片缩放(质量和比例变化)
- Android 使用Glide加载网络图片等比例缩放的实现方法
- 图片根据DIV大小等比例缩放
- css3 来实现图片在指定容器内等比例缩放,并控制中上下左右居中
- ASP.NET C# 按原图片大小等比例缩放生成缩略图
- js同比例缩放图片
- php缩放图片(根据宽高的等比例缩放)实例介绍
- jquery图片等比例缩放程序
- C# 鼠标滚动事件比例缩放图片
- jQuery-图片预加载+等比例缩放
- js 图片缩放(按比例)控制代码
- Android ImageView(scaleType属性)图片按比例缩放
- css控制图片按等比例缩放实例兼容IE6/IE7/火狐/谷歌
- 用 JavaScript 实现网页图片等比例缩放
- 【JavaScript等比例缩放图片】
- 让图片等比例缩放大小
- JS控制图片显示的大小(图片等比例缩放)
- css 网站大背景(按比例缩放背景图片)
- 微信小程序开发之图片等比例缩放 获取屏幕尺寸图片尺寸 自适应
- JS控制图片显示的大小(图片等比例缩放)