C#在線生成縮略圖

jopen 12年前發布 | 679 次閱讀

/// <summary>
       /// 獲取截圖
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       protected void btnGo_Click(object sender, EventArgs e)
       {
           HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(txtAddress.Text);
           HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
           Stream myStream = HttpWResp.GetResponseStream();
           System.Drawing.Image oImage = System.Drawing.Image.FromStream(myStream);
           string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Second.ToString() + ".jpg";
           oImage.Save(Server.MapPath("upFiles/" + fileName), System.Drawing.Imaging.ImageFormat.Gif);
           string webFilePath = Server.MapPath(txtAddress.Text);
           string webFilePath_s = Server.MapPath("smallFiles/" + fileName);
           MakeThumbnail(webFilePath, webFilePath_s, 400, 200, "W");
       }

   /// <summary>
   /// 設置裁剪圖片
   /// </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)
   {
       System.Drawing.Image originalImage = System.Drawing.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;
       }
       System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

       //新建一個畫板
       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(System.Drawing.Color.Transparent);

       //在指定位置并且按指定大小繪制原圖片的指定部分
       g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
       new System.Drawing.Rectangle(x, y, ow, oh),
       System.Drawing.GraphicsUnit.Pixel);

       try
       {
           using (MemoryStream ms = new MemoryStream())
           {

           }
           //以jpg格式保存縮略圖
           bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
       }
       catch (System.Exception e)
       {
           throw e;
       }
       finally
       {
           originalImage.Dispose();
           bitmap.Dispose();
           g.Dispose();
       }
   }

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!