(转)按比例缩放图片
2006-12-19 13:03
218 查看
看到海东兄的按比例缩放图片 ,我就把我写的js版本的也拿出来show一下,哈哈!
js版本:
function resizeImage(obj, MaxW, MaxH)
{
var imageObject = obj;
var state = imageObject.readyState;
if(state!='complete')
{
setTimeout("resizeImage("+imageObject+","+MaxW+","+MaxH+")",50);
return;
}
var oldImage = new Image();
oldImage.src = imageObject.src;
var dW = oldImage.width;
var dH = oldImage.height;
if(dW>MaxW || dH>MaxH)
{
a = dW/MaxW; b = dH/MaxH;
if( b>a ) a = b;
dW = dW/a; dH = dH/a;
}
if(dW > 0 && dH > 0)
{
imageObject.width = dW;
imageObject.height = dH;
}
}
使用很简单:<img src="../pic.jpg" onload='resizeImage(this,60,90)> 就OK了;同时也附上C#版本的
C#版本---海东兄 /article/4696596.html
1 public static string GetImageSize(string imgUrl,int imgHeight,int imgWidth)
9 {
10 string fileName = System.Web.HttpContext.Current.Server.MapPath(imgUrl);
11 string strResult = string.Empty;
12 if(System.IO.File.Exists(fileName) && imgHeight != 0 && imgWidth != 0)
13 {
14 decimal desWidth;decimal desHeight; //目标宽高
15 System.Drawing.Image objImage = System.Drawing.Image.FromFile(fileName);
16 decimal radioAct = (decimal)objImage.Width/(decimal)objImage.Height; //原始图片的宽高比
17 decimal radioLoc = (decimal)imgWidth/(decimal)imgHeight; //图片位的宽高比
18 if(radioAct > radioLoc) //原始图片比图片位宽
19 {
20 decimal dcmZoom = (decimal)imgWidth/(decimal)objImage.Width;
21 desHeight = objImage.Height*dcmZoom;
22 desWidth = imgWidth;
23 }
24 else
25 {
26 decimal dcmZoom = (decimal)imgHeight/(decimal)objImage.Height;
27 desWidth = objImage.Width*dcmZoom;
28 desHeight = imgHeight;
29 }
30 objImage.Dispose(); //释放资源
31 strResult = "width=\"" + Convert.ToString((int)desWidth) + "\" height=\""
32 + Convert.ToString((int)desHeight) + "\" ";
33 }
34 return strResult;
35 }
<script language="javascript">
function changeImg(mypic){
var xw=160;
var xl=180;
var width = mypic.width;
var height = mypic.height;
var bili = width/height;
var A=xw/width;
var B=xl/height;
if(!(A>1&&B>1))
{
if(A<B)
{
mypic.width=xw;
mypic.height=xw/bili;
}
if(A>B)
{
mypic.width=xl*bili;
mypic.height=xl;
}
}
}
</script>
<img src="sh180.jpg" onload="changeImg(this)">
js版本:
function resizeImage(obj, MaxW, MaxH)
{
var imageObject = obj;
var state = imageObject.readyState;
if(state!='complete')
{
setTimeout("resizeImage("+imageObject+","+MaxW+","+MaxH+")",50);
return;
}
var oldImage = new Image();
oldImage.src = imageObject.src;
var dW = oldImage.width;
var dH = oldImage.height;
if(dW>MaxW || dH>MaxH)
{
a = dW/MaxW; b = dH/MaxH;
if( b>a ) a = b;
dW = dW/a; dH = dH/a;
}
if(dW > 0 && dH > 0)
{
imageObject.width = dW;
imageObject.height = dH;
}
}
使用很简单:<img src="../pic.jpg" onload='resizeImage(this,60,90)> 就OK了;同时也附上C#版本的
C#版本---海东兄 /article/4696596.html
1 public static string GetImageSize(string imgUrl,int imgHeight,int imgWidth)
9 {
10 string fileName = System.Web.HttpContext.Current.Server.MapPath(imgUrl);
11 string strResult = string.Empty;
12 if(System.IO.File.Exists(fileName) && imgHeight != 0 && imgWidth != 0)
13 {
14 decimal desWidth;decimal desHeight; //目标宽高
15 System.Drawing.Image objImage = System.Drawing.Image.FromFile(fileName);
16 decimal radioAct = (decimal)objImage.Width/(decimal)objImage.Height; //原始图片的宽高比
17 decimal radioLoc = (decimal)imgWidth/(decimal)imgHeight; //图片位的宽高比
18 if(radioAct > radioLoc) //原始图片比图片位宽
19 {
20 decimal dcmZoom = (decimal)imgWidth/(decimal)objImage.Width;
21 desHeight = objImage.Height*dcmZoom;
22 desWidth = imgWidth;
23 }
24 else
25 {
26 decimal dcmZoom = (decimal)imgHeight/(decimal)objImage.Height;
27 desWidth = objImage.Width*dcmZoom;
28 desHeight = imgHeight;
29 }
30 objImage.Dispose(); //释放资源
31 strResult = "width=\"" + Convert.ToString((int)desWidth) + "\" height=\""
32 + Convert.ToString((int)desHeight) + "\" ";
33 }
34 return strResult;
35 }
<script language="javascript">
function changeImg(mypic){
var xw=160;
var xl=180;
var width = mypic.width;
var height = mypic.height;
var bili = width/height;
var A=xw/width;
var B=xl/height;
if(!(A>1&&B>1))
{
if(A<B)
{
mypic.width=xw;
mypic.height=xw/bili;
}
if(A>B)
{
mypic.width=xl*bili;
mypic.height=xl;
}
}
}
</script>
<img src="sh180.jpg" onload="changeImg(this)">
相关文章推荐
- 图片按比例缩放
- jQuery-图片预加载+等比例缩放
- 将图片缩放(质量和比例变化)
- js 图片缩放(按比例)控制代码
- Android 使用Glide加载网络图片等比例缩放的实现方法
- 图片根据DIV大小等比例缩放
- css3 来实现图片在指定容器内等比例缩放,并控制中上下左右居中
- js同比例缩放图片
- 用 JavaScript 实现网页图片等比例缩放
- php缩放图片(根据宽高的等比例缩放)实例介绍
- jquery图片等比例缩放程序
- C# 鼠标滚动事件比例缩放图片
- Android ImageView(scaleType属性)图片按比例缩放
- css控制图片按等比例缩放实例兼容IE6/IE7/火狐/谷歌
- 【JavaScript等比例缩放图片】
- 让图片等比例缩放大小
- JS控制图片显示的大小(图片等比例缩放)
- css 网站大背景(按比例缩放背景图片)
- 微信小程序开发之图片等比例缩放 获取屏幕尺寸图片尺寸 自适应
- ASP.NET C# 按原图片大小等比例缩放生成缩略图