限制图片 大小 生成等比例缩略图 asp.net和js代码

///说明:在图片加载前显示一个加载标志,当图片下载完毕后显示图片出来
//可对图片进行是否自动缩放功能
//此插件使用时可让页面先加载,而图片后加载的方式,
//解决了平时使用时要在图片显示出来后才能进行缩放时撑大布局的问题
///参数设置:
//scaling     是否等比例自动缩放
//width       图片最大高
//height      图片最大宽
//loadpic     加载中的图片路径
jQuery.fn.LoadImage=function(scaling,width,height,loadpic){
    if(loadpic==null)loadpic="load3.gif";
    return this.each(function(){
        var t=$(this);
        var src=$(this).attr("src")
        var img=new Image();
        //alert("Loading")
        img.src=src;
        //自动缩放图片
        var autoScaling=function(){
            if(scaling){
                if(img.width>0 && img.height>0){
                    if(img.width/img.height>=width/height){
                        if(img.width>width){
                            t.width(width);
                            t.height((img.height*width)/img.width);
                        }else{
                            t.width(img.width);
                            t.height(img.height);
                        }
                    }
                    else{
                        if(img.height>height){
                            t.height(height);
                            t.width((img.width*height)/img.height);
                        }else{
                            t.width(img.width);
                            t.height(img.height);
                        }
                    }
                }
            }   
        }
        //处理ff下会自动读取缓存图片
        if(img.complete){
            //alert("getToCache!");
            autoScaling();
            return;
        }
        $(this).attr("src","");
        var loading=$("<img alt=\"加载中\" title=\"图片加载中\" src=\""+loadpic+"\" />");
        t.hide();
        t.after(loading);
        $(img).load(function(){
            autoScaling();
            loading.remove();
            t.attr("src",this.src);
            loading.remove();
            t.show();
            //alert("finally!")
        });
    });
}

$("li img").LoadImage(true,120,120);

 

 

 

asp.net代码 生成等比例缩略图 高质量

///   <summary>
       ///   生成等比例高清晰缩略图
       ///   </summary>
       ///   <param   name= "sourcePath "> 要生成的图片的路径(上传到服务器上后的虚拟路径) </param>
       ///   <param   name= "newPath "> 生成的缩略图的路径(虚拟路径) </param>
       ///   <param   name= "width "> 要生成的缩略图的宽度 </param>
       ///   <param   name= "height "> 要生成的缩略图的高度 </param>
       ///   <returns> 返回是否成功 </returns>
       public static bool CreateMiniature(string sourcePath, string newPath, int width, int height)
       {
           bool result = false;
           double w = 0.0;
           double h = 0.0;
           double mw = Convert.ToDouble(width);
           double mh = Convert.ToDouble(height);
           string sourcePathPhysical = System.Web.HttpContext.Current.Server.MapPath(sourcePath);
           string newPathPhysical = System.Web.HttpContext.Current.Server.MapPath(newPath);
           try
           {
               System.IO.FileInfo myFile = new System.IO.FileInfo(sourcePathPhysical);

               if (myFile.Exists)
               {
                   //从文件取得图片对象
                   System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePathPhysical);
                   double sw = Convert.ToDouble(image.Width);
                   double sh = Convert.ToDouble(image.Height);

                   if (sw < mw && sh < mh)
                   {
                       w = sw;
                       h = sh;
                   }
                   else if ((sw / sh) > (mw / mh))
                   {
                       w = width;
                       h = (w * sh) / sw;
                   }
                   else
                   {
                       h = height;
                       w = (h * sw) / sh;
                   }

                   //取得图片大小
                   System.Drawing.Size size = new Size(Convert.ToInt32(w), Convert.ToInt32(h));
                   //新建一个bmp图片
                   System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
                   //新建一个画板
                   System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                   //设置高质量插值法
                   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                   //设置高质量,低速度呈现平滑程度
                   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                   //清空一下画布
                   g.Clear(Color.Blue);
                   //在指定位置画图
                   g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                   new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
                   System.Drawing.GraphicsUnit.Pixel);
                   //保存高清晰度的缩略图
                   bitmap.Save(newPathPhysical, System.Drawing.Imaging.ImageFormat.Jpeg);
                   g.Dispose();
                   image.Dispose();
                   //myFile.Delete();     //删除原图片
                   result = true;
               }
           }
           catch
           {
               result = false;
           }

           return result;
       }

 

 

 

asp.net生成固定大小 缩略图 不够的地方 填充白色

 

 

/// <summary>
       /// 生成缩略图 静态方法
       /// </summary>
       /// <param name="pathImageFrom"> 源图的路径(含文件名及扩展名 物理路径) </param>
       /// <param name="pathImageTo"> 生成的缩略图所保存的路径(含文件名及扩展名 物理路径)
       /// 注意:扩展名一定要与生成的缩略图格式相对应 </param>
       /// <param name="width"> 欲生成的缩略图 "画布" 的宽度(像素值) </param>
       /// <param name="height"> 欲生成的缩略图 "画布" 的高度(像素值) </param>
       public static void GenThumbnail(string pathImageFrom, string pathImageTo, int width, int height)
       {
           Image imageFrom = null;

           try
           {
               imageFrom = Image.FromFile(pathImageFrom);
           }
           catch
           {
               //throw;
           }

           if (imageFrom == null)
           {
               return;
           }

           // 源图宽度及高度
           int imageFromWidth = imageFrom.Width;
           int imageFromHeight = imageFrom.Height;

           // 生成的缩略图实际宽度及高度
           int bitmapWidth = width;
           int bitmapHeight = height;

           // 生成的缩略图在上述"画布"上的位置
           int X = 0;
           int Y = 0;

           // 根据源图及欲生成的缩略图尺寸,计算缩略图的实际尺寸及其在"画布"上的位置
           if (bitmapHeight * imageFromWidth > bitmapWidth * imageFromHeight)
           {
               bitmapHeight = imageFromHeight * width / imageFromWidth;
               Y = (height - bitmapHeight) / 2;
           }
           else
           {
               bitmapWidth = imageFromWidth * height / imageFromHeight;
               X = (width - bitmapWidth) / 2;
           }

           // 创建画布
           Bitmap bmp = new Bitmap(width, height);
           Graphics g = Graphics.FromImage(bmp);

           // 用白色清空
           g.Clear(Color.White);

           // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。
           g.InterpolationMode = InterpolationMode.HighQualityBicubic;

           // 指定高质量、低速度呈现。
           g.SmoothingMode = SmoothingMode.HighQuality;

           // 在指定位置并且按指定大小绘制指定的 Image 的指定部分。
           g.DrawImage(imageFrom, new Rectangle(X, Y, bitmapWidth, bitmapHeight), new Rectangle(0, 0, imageFromWidth, imageFromHeight), GraphicsUnit.Pixel);

           try
           {
               //经测试 .jpg 格式缩略图大小与质量等最优
               bmp.Save(pathImageTo, ImageFormat.Jpeg);
           }
           catch
           {
           }
           finally
           {
               //显示释放资源
               imageFrom.Dispose();
               bmp.Dispose();
               g.Dispose();
           }
       }

你可能感兴趣的:(asp.net)