ASP.NET实现上传图片并生成缩略图的方法

时间:2022-08-24 14:34:59

本文实例讲述了ASP.NET实现上传图片并生成缩略图的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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; //设置缩略图初始宽度
  int tHeight = 100; //设置缩略图初始高度
  //按比例计算出缩略图的宽度和高度
  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; //设置高质量插值法
  g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
  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();
  }
 }
}

这里再补充一个改进方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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);
  //设置高质量插值法
   g.InterpolationMode = InterpolationMode.High;
  //以高质量 低速度 呈现
   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程序设计有所帮助。