中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#生成縮略圖的函數(shù),可直接調(diào)用

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

C#生成縮略圖的函數(shù),可直接調(diào)用,代碼非常簡單,直接嵌入你的項(xiàng)目,調(diào)用該函數(shù)即可。

  /// <summary>
  /// 生成縮略圖
 /// 轉(zhuǎn)自:http://www.sharejs.com
  /// </summary>
  /// <param name="originalImagePath">源圖路徑(物理路徑)</param>
  /// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>
  /// <param name="width">縮略圖寬度</param>
  /// <param name="height">縮略圖高度</param>
  /// <param name="mode">生成縮略圖的方式</param>  
  public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  {
      Image originalImage = Image.FromFile(originalImagePath);
        
      int towidth = width;
      int toheight = height;
    
      int x = 0;
      int y = 0;
      int ow = originalImage.Width;
      int oh = originalImage.Height;      
  
      switch (mode)
      {      
          case "HW"://指定高寬縮放(可能變形)              
              break;
          case "W"://指定寬,高按比例                  
              toheight = originalImage.Height * width/originalImage.Width;
              break;
          case "H"://指定高,寬按比例
              towidth = originalImage.Width * height/originalImage.Height;                  
 
              break;      
          case "Cut"://指定高寬裁減(不變形)              
              if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
              {
                  oh = originalImage.Height;
                  ow = originalImage.Height*towidth/toheight;
                  y = 0;
                  x = (originalImage.Width - ow)/2;
              }
              else
              {
                  ow = originalImage.Width;
                  oh = originalImage.Width*height/towidth;
                  x = 0;
                  y = (originalImage.Height - oh)/2;
              }
              break;                  
          default :
              break;
      }  
        
      //新建一個(gè)bmp圖片
      Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
  
      //新建一個(gè)畫板
      Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  
      //設(shè)置高質(zhì)量插值法
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  
      //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  
      //清空畫布并以透明背景色填充
      g.Clear(Color.Transparent);      
  
      //在指定位置并且按指定大小繪制原圖片的指定部分
      g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
          new Rectangle(x, y, ow,oh),
          GraphicsUnit.Pixel);
  
      try
      {          
          //以jpg格式保存縮略圖
          bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
      }
      catch(System.Exception e)
      {
          throw e;
      }
      finally
      {
          originalImage.Dispose();
          bitmap.Dispose();                      
          g.Dispose();
      }
  }

標(biāo)簽: isp 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:一個(gè)非常高效的提取內(nèi)容關(guān)鍵詞的python代碼

下一篇:python讓圖片按照exif信息里的創(chuàng)建時(shí)間進(jìn)行排序