• 按比例缩放图片


    看到海东兄的按比例缩放图片 ,我就把我写的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了;
    注:等比例放缩的时候会出现抖动的情况,处理方法很简单,你在img的属性先设置它的widht和height,这样的话加载的时候绝对不会超过这个尺寸,等你js运行好之后就会调到你所规定的比例,绝对不会以下撑到很大。


    同时也附上C#版本的

    C#版本---海东兄 http://www.cnblogs.com/ghd258/archive/2005/11/07/270447.html

    1/// <summary>
     2        /// 按比例缩放图片
     3        /// </summary>
     4        /// <param name="imgUrl">图片的路径</param>
     5        /// <param name="imgHeight">图片的高度</param>
     6        /// <param name="imgWidth">图片的宽度</param>
     7        /// <returns></returns>

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

  • 相关阅读:
    浅析JNI
    网易云音乐歌词下载器
    如何用一个SQL“搞挂”一个服务模块
    SpingBoot 1.5.2,MultipartFile保存图片时的不稳定异常(好像和内置tomcat有关)
    double 去除小数点后的0
    项目中时间处理----今天:时分(10:15),昨天/前天:(昨天/前天),除此之外的本周(星期几),再往前年.月.日(2017.06.15)
    SpringMvc 静态内部类 封装请求数据
    jsp页面 ajax提交数组 到struts2的action
    Struts2 s:if test判断时遇到的问题
    Struts2中 iterator隔行变色
  • 原文地址:https://www.cnblogs.com/skylaugh/p/596084.html
Copyright © 2020-2023  润新知