溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET中怎么實現(xiàn)上傳圖片并生成縮略圖

發(fā)布時間:2021-07-15 16:20:11 來源:億速云 閱讀:134 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)ASP.NET中怎么實現(xiàn)上傳圖片并生成縮略圖,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

具體如下:

protected void bt_upload_Click(object sender, EventArgs e)
{
  //檢查上傳文件的格式是否有效 
  if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
  {
   Response.Write("上傳圖片格式無效!");
   return;
  }
  //生成原圖 
  Byte[] oFileByte = new byte[this.UploadFile.PostedFile.ContentLength];
  System.IO.Stream oStream = this.UploadFile.PostedFile.InputStream;
  System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
  int oWidth = oImage.Width; //原圖寬度 
  int oHeight = oImage.Height; //原圖高度 
  int tWidth = 100; //設(shè)置縮略圖初始寬度 
  int tHeight = 100; //設(shè)置縮略圖初始高度 
  //按比例計算出縮略圖的寬度和高度 
  if (oWidth >= oHeight)
  {
   tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
  }
  else
  {
   tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
  }
  //生成縮略原圖 
  Bitmap tImage = new Bitmap(tWidth, tHeight);
  Graphics g = Graphics.FromImage(tImage);
  g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設(shè)置高質(zhì)量插值法 
  g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 
  g.Clear(Color.Transparent); //清空畫布并以透明背景色填充 
  g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
  string oFullName = Server.MapPath(".") + "/image/" + "o" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原圖的物理路徑 
  string tFullName = Server.MapPath(".") + "/image/" + "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存縮略圖的物理路徑 
  try
  {
   //以JPG格式保存圖片 
   oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
   tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
  }
  catch (Exception ex)
  {
   throw ex;
  }
  finally
  {
   //釋放資源 
   oImage.Dispose();
   g.Dispose();
   tImage.Dispose();
  }
 }
}

這里再補充一個改進方法:

#region 上傳圖片 并生成縮略圖
/// <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 = image.Width;
  //獲取圖片的高度
   int oh = image.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 * toheight / towidth;
     x = 0;
     y(originalImage.Height - oh) / 2;
    }
    break;
    default: break;
   }
  //新建一個bmp圖片
   Bitmap bitmap = new Bitmap(towidth, toheight);
  //新建一個畫布 以BitMap 寬 高作為畫布的大小
   Graphics g = Graphics.FromImage(bitmap);
  //設(shè)置高質(zhì)量插值法
   g.InterpolationMode = InterpolationMode.High;
  //以高質(zhì)量 低速度 呈現(xiàn)
   g.SmoothingMode = SmoothingMode.HighQuality;
  //清空畫布 以白色背景色填充 
   g.Clear(Color.Transparent);
   //在指定位置并且按指定大小繪制原圖片的指定部分
   g.DrawImage(originalImage,new Rectangle(towidth,toheight),new Rectangle(x,y,ow,oh),GraphicsUnit.Pixel);
   try
   {
    //以jpg格式保存縮略圖
    bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
   }
   catch (Exception ex)
   {
    throw ex;
   }
   finally {
    //釋放資源
    originalImage.Dispose();
    bitmap.Dispose();
    g.Dispose();
   }
}
#endregion

上述就是小編為大家分享的ASP.NET中怎么實現(xiàn)上傳圖片并生成縮略圖了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI