C# .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
/// <summary>
       /// 合并宽度一样的图片
       /// </summary>
       /// <param name="imgUrls">多张图片,按顺序合并</param>
       /// <param name="mergeImgPath">合成图片的具体地址</param>
       /// <returns></returns>
       public bool MergeImg(List<string> imgUrls, string mergeImgPath)
       {
           imgUrls = imgUrls.OrderBy(c => c, new SemiNumericComparer()).ToList();
           using (var file = File.OpenWrite(mergeImgPath))
           {
               try
               {
                   Image image1 = System.Drawing.Image.FromFile(imgUrls[0]);
                   var width = image1.Width;
                   var height = image1.Height;
 
                   //计算整体
                   for (int i = 1; i < imgUrls.Count(); i++)
                   {
                       using (Image tempImg = System.Drawing.Image.FromFile(imgUrls[i]))
                       {
                           height += tempImg.Height;
                       }
                   }
 
                   using (Bitmap b = new Bitmap(width, height))
                   using (Graphics g = Graphics.FromImage(b))
                   {
                       g.CompositingMode = CompositingMode.SourceCopy;
 
                       //先画第一个图片
                       g.DrawImageUnscaled(image1, 0, 0);
 
                       //目前的高度
                       var currentHeight = image1.Height;
 
                       //画多张图片
                       for (int j = 1; j < imgUrls.Count(); j++)
                       {
                           using (Image currentImg = System.Drawing.Image.FromFile(imgUrls[j]))
                           {
                               g.DrawImageUnscaled(currentImg, 0, currentHeight);
                               currentHeight += currentImg.Height;
                           }
                       }
                       b.Save(file, ImageFormat.Jpeg);
 
                   }
                   return true; ;
               }
               catch (Exception ex)
               {
                   return false;
               }
           }
       }
 
 
 
       /// <summary>
       /// Resize the image to the specified width and height.
       /// </summary>
       /// <param name="imagePath">The image to resize.</param>
       /// <param name="width">The width to resize to.</param>
       /// <param name="height">The height to resize to.</param>
       /// <returns>The resized image.</returns>
       public string ResizeImage(string imagePath, int width, int height)
       {
           if (imagePath.ToLower().Contains(".mp4"))
           {
               return "";
           }
           System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath);
           var destRect = new Rectangle(0, 0, width, height);
           var destImage = new Bitmap(width, height);
 
           destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
 
           using (var graphics = Graphics.FromImage(destImage))
           {
               graphics.CompositingMode = CompositingMode.SourceCopy;
               graphics.CompositingQuality = CompositingQuality.HighQuality;
               graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
               graphics.SmoothingMode = SmoothingMode.HighQuality;
               graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
 
               using (var wrapMode = new ImageAttributes())
               {
                   wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                   graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
               }
           }
           #region  提高压缩质量
           EncoderParameters ep = new EncoderParameters();
           long[] qy = new long[1];
           qy[0] = 1;//设置压缩的比例1-100,压缩比例越小越好
           EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
           ep.Param[0] = eParam;
           #endregion
 
           #region 设置imgcodeinfo为JPEG
           ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
           ImageCodecInfo jpegICIinfo = null;
           for (int x = 0; x < arrayICI.Length; x++)
           {
               if (arrayICI[x].FormatDescription.Equals("JPEG"))
               {
                   jpegICIinfo = arrayICI[x];
                   break;
               }
           }
           #endregion
           
           var resizeImagePath = string.Empty;
           if (jpegICIinfo != null)
           {
               resizeImagePath = imagePath.Replace("jpg", width + "x" + height + ".jpg");
               destImage.Save(resizeImagePath, jpegICIinfo, ep);
 
           }
           return resizeImagePath;
       }

  

posted @   iDEAAM  阅读(1291)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
历史上的今天:
2016-07-15 EPI Server相关
2015-07-15 将大数据利用 BCP 导出SqlServer数据到CSV
点击右上角即可分享
微信分享提示