本文实例为大家分享了C#绘制正方形图片的的具体代码,供大家参考,具体内容如下
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; namespace treads { /// <summary> /// 制作小正方形 /// </summary> class Class3 { private string srcFileName = @"x" ; //获取图片的路径 private string srcFileName1 = @"x" ; //要保持图片的新路径 /// <summary> /// 保存图片 /// </summary> /// <param name="image">Image 对象</param> /// <param name="savePath">保存路径</param> /// <param name="ici">指定格式的编解码参数</param> private static void SaveImage(Image image, string savePath, ImageCodecInfo ici) { //设置 原图片 对象的 EncoderParameters 对象 EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (( long )100)); image.Save(savePath, ici, parameters); parameters.Dispose(); } /// <summary> /// 获取图像编码解码器的所有相关信息 /// </summary> /// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param> /// <returns>返回图像编码解码器的所有相关信息</returns> private static ImageCodecInfo GetCodecInfo( string mimeType) { ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo ici in CodecInfo) { if (ici.MimeType == mimeType) return ici; } return null ; } /// <summary> /// 计算新尺寸 /// </summary> /// <param name="width">原始宽度</param> /// <param name="height">原始高度</param> /// <param name="maxWidth">最大新宽度</param> /// <param name="maxHeight">最大新高度</param> /// <returns></returns> private static Size ResizeImage( int width, int height, int maxWidth, int maxHeight) { //此次2012-02-05修改过================= if (maxWidth <= 0) maxWidth = width; if (maxHeight <= 0) maxHeight = height; //以上2012-02-05修改过================= decimal MAX_WIDTH = ( decimal )maxWidth; decimal MAX_HEIGHT = ( decimal )maxHeight; decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT; int newWidth, newHeight; decimal originalWidth = ( decimal )width; decimal originalHeight = ( decimal )height; if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT) { decimal factor; // determine the largest factor if (originalWidth / originalHeight > ASPECT_RATIO) { factor = originalWidth / MAX_WIDTH; newWidth = Convert.ToInt32(originalWidth / factor); newHeight = Convert.ToInt32(originalHeight / factor); } else { factor = originalHeight / MAX_HEIGHT; newWidth = Convert.ToInt32(originalWidth / factor); newHeight = Convert.ToInt32(originalHeight / factor); } } else { newWidth = width; newHeight = height; } return new Size(newWidth, newHeight); } /// <summary> /// 得到图片格式 /// </summary> /// <param name="name">文件名称</param> /// <returns></returns> public static ImageFormat GetFormat( string name) { string ext = name.Substring(name.LastIndexOf( "." ) + 1); switch (ext.ToLower()) { case "jpg" : case "jpeg" : return ImageFormat.Jpeg; case "bmp" : return ImageFormat.Bmp; case "png" : return ImageFormat.Png; case "gif" : return ImageFormat.Gif; default : return ImageFormat.Jpeg; } } /// <summary> /// 制作小正方形 /// </summary> /// <param name="image">图片对象</param> /// <param name="newFileName">新地址</param> /// <param name="newSize">长度或宽度</param> public static void MakeSquareImage(Image image, string newFileName, int newSize) { int i = 0; int width = image.Width; int height = image.Height; if (width > height) i = height; else i = width; Bitmap b = new Bitmap(newSize, newSize); try { Graphics g = Graphics.FromImage(b); //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.HighQuality; //清除整个绘图面并以透明背景色填充 g.Clear(Color.Transparent); if (width < height) g.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle(0, (height - width) / 2, width, width), GraphicsUnit.Pixel); else g.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle((width - height) / 2, 0, height, height), GraphicsUnit.Pixel); SaveImage(b, newFileName, GetCodecInfo( "image/" + GetFormat(newFileName).ToString().ToLower())); } finally { image.Dispose(); b.Dispose(); } } /// <summary> /// 制作小正方形 /// </summary> /// <param name="fileName">图片文件名</param> /// <param name="newFileName">新地址</param> /// <param name="newSize">长度或宽度</param> public static void MakeSquareImage( string fileName, string newFileName, int newSize) { MakeSquareImage(Image.FromFile(fileName), newFileName, newSize); } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/yuanzijian-ruiec/archive/2018/10/14/9786686.html