Home > Web Front-end > JS Tutorial > JS method to achieve proportional scaling of images (with C# version code)_javascript skills

JS method to achieve proportional scaling of images (with C# version code)_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:27:05
Original
1209 people have browsed it

The example in this article describes the method of scaling images proportionally in JS. Share it with everyone for your reference, the details are as follows:

js version:

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;
 }
}

Copy after login

It’s very simple to use: /// <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; }

Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

Related labels:
js
Previous article:jquery plug-in ajaxupload implements file upload operation_jquery Next article:Summary of JS operation XML examples (loading and parsing XML files and strings)_javascript skills
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template