服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - .Net 实现图片缩略图上传通用方法

.Net 实现图片缩略图上传通用方法

2020-05-31 14:20lin山 ASP.NET教程

这篇文章主要介绍了.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/// <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;
        //设置高质量,低速度呈现平滑程度
        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 实现图片缩略图上传通用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/lins03/archive/2018/07/31/9397046.html

延伸 · 阅读

精彩推荐