亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

.Net 實現(xiàn)圖片縮略圖上傳通用方法

 更新時間:2018年08月02日 09:30:04   作者:lin山  
這篇文章主要介紹了.Net 實現(xiàn)圖片縮略圖上傳通用方法,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

日常開發(fā)中,經常碰到圖片上傳的需求,尤其在商城系統(tǒng)開發(fā)的時候,商品列表商品圖片展示如果使用高清原圖,由于高清原圖比較大,加載原圖時間會大大增加,直接導致系統(tǒng)性能底下,用戶體驗不好,并發(fā)量高的時候直接就掛掉了,這時候后臺上傳圖片的時候,就必須將原高清圖進行壓縮,生成高質量縮略圖,然后在商品列表讀取縮略圖可以大大減少加載時間,起到一個性能優(yōu)化的作用,當然在商品詳情的時候還是得用高清原圖!

以下代碼,可以在實際開發(fā)中使用將圖片高質量壓縮,話不多說,代碼貼下:

/// <summary>
    /// 生成縮略圖或質量壓縮
    /// </summary>
    /// <param name="sourcePath">源圖路徑(物理路徑)</param>
    /// <param name="targetPath">縮略圖路徑(物理路徑)</param>
    /// <param name="width">縮略圖寬度,如果寬度為0則不縮略</param>
    /// <param name="height">縮略圖高度,如果高度為0則不縮略</param>
    /// <param name="mode">生成縮略圖的方式,默認為空,為空則不縮略高寬[HW 指定高寬縮放(不變形);W 指定寬,高按比例;H 指定高,寬按比例;CUT 指定高寬裁減(不變形)]</param> 
    /// <param name="flag">壓縮質量(數字越小壓縮率越高)1-100</param>
    /// <param name="size">壓縮后圖片的最大大小,0為不限制大小</param>
    public static void MakeThumbnail(string sourcePath, string targetPath, int width = 0, int height = 0, string mode = "", int flag = 100, int size = 0)
    {
      Image sourceImage = null;
      Image bitmap = null;
      Graphics g = null;
      EncoderParameters ep = null;
      EncoderParameter eParam = null;
      try
      {
        sourceImage = Image.FromFile(sourcePath);
        int toWidth = 0;
        if (width > 0)
        {
          toWidth = width;
        }
        else
        {
          toWidth = sourceImage.Width;
        }
        int toHeight = 0;
        if (height > 0)
        {
          toHeight = height;
        }
        else
        {
          toHeight = sourceImage.Height;
        }
        int x = 0;
        int y = 0;
        int ow = sourceImage.Width;
        int oh = sourceImage.Height;
        if (width > 0 && height > 0 && !string.IsNullOrWhiteSpace(mode))
        {
          switch (mode.ToUpper())
          {
            case "HW"://指定高寬縮放(不變形)
              int tempheight = sourceImage.Height * width / sourceImage.Width;
              if (tempheight > height)
              {
                toWidth = sourceImage.Width * height / sourceImage.Height;
              }
              else
              {
                toHeight = sourceImage.Height * width / sourceImage.Width;
              }
              break;
            case "W"://指定寬,高按比例          
              toHeight = sourceImage.Height * width / sourceImage.Width;
              break;
            case "H"://指定高,寬按比例
              toWidth = sourceImage.Width * height / sourceImage.Height;
              break;
            case "CUT"://指定高寬裁減(不變形)        
              if ((double)sourceImage.Width / (double)sourceImage.Height > (double)toWidth / (double)toHeight)
              {
                oh = sourceImage.Height;
                ow = sourceImage.Height * toWidth / toHeight;
                y = 0;
                x = (sourceImage.Width - ow) / 2;
              }
              else
              {
                ow = sourceImage.Width;
                oh = sourceImage.Width * height / toWidth;
                x = 0;
                y = (sourceImage.Height - oh) / 2;
              }
              break;
          }
        }
        //新建一個bmp圖片
        bitmap = new Bitmap(toWidth, toHeight);
        //新建一個畫板
        g = Graphics.FromImage(bitmap);
        g.CompositingQuality = CompositingQuality.HighQuality;
        //設置高質量插值法
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //設置高質量,低速度呈現(xiàn)平滑程度
        g.SmoothingMode = SmoothingMode.HighQuality;
        //清空畫布并以透明背景色填充
        g.Clear(Color.Transparent);
        //在指定位置并且按指定大小繪制原圖片的指定部分
        g.DrawImage(sourceImage, new Rectangle(0, 0, toWidth, toHeight),
          new Rectangle(x, y, ow, oh),
          GraphicsUnit.Pixel);
        //以下代碼為保存圖片時,設置壓縮質量
        ep = new EncoderParameters();
        long[] qy = new long[1];
        qy[0] = flag;//設置壓縮的比例1-100
        eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
        ep.Param[0] = eParam;
        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//獲取圖像編碼器的信息
        ImageCodecInfo jpegICIinfo = null;
        for (int i = 0; i < arrayICI.Length; i++)
        {
          if (arrayICI[i].FormatDescription.Equals("JPEG"))
          {
            jpegICIinfo = arrayICI[i];
            break;
          }
        }
        if (jpegICIinfo != null)
        {
          bitmap.Save(targetPath, jpegICIinfo, ep);
          FileInfo fiTarget = new FileInfo(targetPath);
          if (size > 0 && fiTarget.Length > 1024 * size)
          {
            flag = flag - 10;
            MakeThumbnail(sourcePath, targetPath, width, height, mode, flag, size);
          }
        }
        else
        {
          //以jpg格式保存縮略圖
          bitmap.Save(targetPath, ImageFormat.Jpeg);
        }
      }
      catch (System.Exception ex)
      {
        throw ex;
      }
      finally
      {
        if (sourceImage != null)
        {
          sourceImage.Dispose();
        }
        if (bitmap != null)
        {
          bitmap.Dispose();
        }
        if (g != null)
        {
          g.Dispose();
        }
        if (ep != null)
        {
          ep.Dispose();
        }
        if (eParam != null)
        {
          eParam.Dispose();
        }
      }
    }

總結

以上所述是小編給大家介紹的.Net 實現(xiàn)圖片縮略圖上傳通用方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

最新評論