C#对图片操作的常用函数(转至CSDN)
1 /// <summary>
2 /// 获取一个图片按等比例缩小后的大小。
3 /// </summary>
4 /// <param name="maxWidth">需要缩小到的宽度</param>
5 /// <param name="maxHeight">需要缩小到的高度</param>
6 /// <param name="imageOriginalWidth">图片的原始宽度</param>
7 /// <param name="imageOriginalHeight">图片的原始高度</param>
8 /// <returns>返回图片按等比例缩小后的实际大小</returns>
9 public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
10 {
11 double w = 0.0;
12 double h = 0.0;
13 double sw = Convert.ToDouble(imageOriginalWidth);
14 double sh = Convert.ToDouble(imageOriginalHeight);
15 double mw = Convert.ToDouble(maxWidth);
16 double mh = Convert.ToDouble(maxHeight);
17
18 if (sw < mw && sh < mh)
19 {
20 w = sw;
21 h = sh;
22 }
23 else if ((sw / sh) > (mw / mh))
24 {
25 w = maxWidth;
26 h = (w * sh) / sw;
27 }
28 else
29 {
30 h = maxHeight;
31 w = (h * sw) / sh;
32 }
33
34 return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
35 }
36
37 /// <summary>
38 /// 对给定的一个图片(Image对象)生成一个指定大小的缩略图。
39 /// </summary>
40 /// <param name="originalImage">原始图片</param>
41 /// <param name="thumMaxWidth">缩略图的宽度</param>
42 /// <param name="thumMaxHeight">缩略图的高度</param>
43 /// <returns>返回缩略图的Image对象</returns>
44 public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)
45 {
46 Size thumRealSize = Size.Empty;
47 System.Drawing.Image newImage = originalImage;
48 Graphics graphics = null;
49
50 try
51 {
52 thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);
53 newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);
54 graphics = Graphics.FromImage(newImage);
55
56 graphics.CompositingQuality = CompositingQuality.HighQuality;
57 graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
58 graphics.SmoothingMode = SmoothingMode.HighQuality;
59
60 graphics.Clear(Color.Transparent);
61
62 graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
63 }
64 catch { }
65 finally
66 {
67 if (graphics != null)
68 {
69 graphics.Dispose();
70 graphics = null;
71 }
72 }
73
74 return newImage;
75 }
76 /// <summary>
77 /// 对给定的一个图片文件生成一个指定大小的缩略图。
78 /// </summary>
79 /// <param name="originalImage">图片的物理文件地址</param>
80 /// <param name="thumMaxWidth">缩略图的宽度</param>
81 /// <param name="thumMaxHeight">缩略图的高度</param>
82 /// <returns>返回缩略图的Image对象</returns>
83 public static System.Drawing.Image GetThumbNailImage(string imageFile, int thumMaxWidth, int thumMaxHeight)
84 {
85 System.Drawing.Image originalImage = null;
86 System.Drawing.Image newImage = null;
87
88 try
89 {
90 originalImage = System.Drawing.Image.FromFile(imageFile);
91 newImage = GetThumbNailImage(originalImage, thumMaxWidth, thumMaxHeight);
92 }
93 catch { }
94 finally
95 {
96 if (originalImage != null)
97 {
98 originalImage.Dispose();
99 originalImage = null;
100 }
101 }
102
103 return newImage;
104 }
105 /// <summary>
106 /// 对给定的一个图片文件生成一个指定大小的缩略图,并将缩略图保存到指定位置。
107 /// </summary>
108 /// <param name="originalImageFile">图片的物理文件地址</param>
109 /// <param name="thumbNailImageFile">缩略图的物理文件地址</param>
110 /// <param name="thumMaxWidth">缩略图的宽度</param>
111 /// <param name="thumMaxHeight">缩略图的高度</param>
112 public static void MakeThumbNail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)
113 {
114 System.Drawing.Image newImage = GetThumbNailImage(originalImageFile, thumMaxWidth, thumMaxHeight);
115 try
116 {
117 newImage.Save(thumbNailImageFile, ImageFormat.Jpeg);
118 }
119 catch
120 { }
121 finally
122 {
123 newImage.Dispose();
124 newImage = null;
125 }
126 }
127 /// <summary>
128 /// 将一个图片的内存流调整为指定大小,并返回调整后的内存流。
129 /// </summary>
130 /// <param name="originalImageStream">原始图片的内存流</param>
131 /// <param name="newWidth">新图片的宽度</param>
132 /// <param name="newHeight">新图片的高度</param>
133 /// <returns>返回调整后的图片的内存流</returns>
134 public static MemoryStream ResizeImage(Stream originalImageStream, int newWidth, int newHeight)
135 {
136 MemoryStream newImageStream = null;
137
138 System.Drawing.Image newImage = Globals.GetThumbNailImage(System.Drawing.Image.FromStream(originalImageStream), newWidth, newHeight);
139 if (newImage != null)
140 {
141 newImageStream = new MemoryStream();
142 newImage.Save(newImageStream, ImageFormat.Jpeg);
143 }
144
145 return newImageStream;
146 }
147 /// <summary>
148 /// 将一个内存流保存为磁盘文件。
149 /// </summary>
150 /// <param name="stream">内存流</param>
151 /// <param name="newFile">目标磁盘文件地址</param>
152 public static void SaveStreamToFile(Stream stream, string newFile)
153 {
154 if (stream == null || stream.Length == 0 || string.IsNullOrEmpty(newFile))
155 {
156 return;
157 }
158
159 byte[] buffer = new byte[stream.Length];
160 stream.Position = 0;
161 stream.Read(buffer, 0, buffer.Length);
162 FileStream fileStream = new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write);
163 fileStream.Write(buffer, 0, buffer.Length);
164 fileStream.Flush();
165 fileStream.Close();
166 fileStream.Dispose();
167 }
168 /// <summary>
169 /// 对一个指定的图片加上图片水印效果。
170 /// </summary>
171 /// <param name="imageFile">图片文件地址</param>
172 /// <param name="waterImage">水印图片(Image对象)</param>
173 public static void CreateImageWaterMark(string imageFile, System.Drawing.Image waterImage)
174 {
175 if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
176 {
177 return;
178 }
179
180 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
181
182 if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)
183 {
184 return;
185 }
186
187 Graphics graphics = Graphics.FromImage(originalImage);
188
189 int x = originalImage.Width - waterImage.Width - 10;
190 int y = originalImage.Height - waterImage.Height - 10;
191 int width = waterImage.Width;
192 int height = waterImage.Height;
193
194 graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
195 graphics.Dispose();
196
197 MemoryStream stream = new MemoryStream();
198 originalImage.Save(stream, ImageFormat.Jpeg);
199 originalImage.Dispose();
200
201 System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
202
203 imageWithWater.Save(imageFile);
204 imageWithWater.Dispose();
205 }
206 /// <summary>
207 /// 对一个指定的图片加上文字水印效果。
208 /// </summary>
209 /// <param name="imageFile">图片文件地址</param>
210 /// <param name="waterText">水印文字内容</param>
211 public static void CreateTextWaterMark(string imageFile, string waterText)
212 {
213 if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile))
214 {
215 return;
216 }
217
218 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
219
220 Graphics graphics = Graphics.FromImage(originalImage);
221
222 graphics.SmoothingMode = SmoothingMode.HighQuality;
223 graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
224 graphics.CompositingQuality = CompositingQuality.HighQuality;
225 graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
226
227 SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
228 Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);
229 SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);
230
231 float x = (float)originalImage.Width - waterTextSize.Width - 10F;
232 float y = (float)originalImage.Height - waterTextSize.Height - 10F;
233
234 graphics.DrawString(waterText, waterTextFont, brush, x, y);
235
236 graphics.Dispose();
237 brush.Dispose();
238
239 MemoryStream stream = new MemoryStream();
240 originalImage.Save(stream, ImageFormat.Jpeg);
241 originalImage.Dispose();
242
243 System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
244
245 imageWithWater.Save(imageFile);
246 imageWithWater.Dispose();
247 }
248
249 /// <summary>
250 /// 判断上传组件是否包含内容。
251 /// </summary>
252 /// <param name="fileUpload">ASP.NET 2.0标准上传组件</param>
253 /// <returns>如果数据有效,则返回True,否则返回False</returns>
254 public static bool IsAttachmentValid(FileUpload fileUpload)
255 {
256 if (fileUpload != null &&
257 fileUpload.PostedFile != null &&
258 !string.IsNullOrEmpty(fileUpload.PostedFile.FileName) &&
259 fileUpload.PostedFile.ContentLength > 0)
260 {
261 return true;
262 }
263 return false;
264 }
265
2 /// 获取一个图片按等比例缩小后的大小。
3 /// </summary>
4 /// <param name="maxWidth">需要缩小到的宽度</param>
5 /// <param name="maxHeight">需要缩小到的高度</param>
6 /// <param name="imageOriginalWidth">图片的原始宽度</param>
7 /// <param name="imageOriginalHeight">图片的原始高度</param>
8 /// <returns>返回图片按等比例缩小后的实际大小</returns>
9 public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
10 {
11 double w = 0.0;
12 double h = 0.0;
13 double sw = Convert.ToDouble(imageOriginalWidth);
14 double sh = Convert.ToDouble(imageOriginalHeight);
15 double mw = Convert.ToDouble(maxWidth);
16 double mh = Convert.ToDouble(maxHeight);
17
18 if (sw < mw && sh < mh)
19 {
20 w = sw;
21 h = sh;
22 }
23 else if ((sw / sh) > (mw / mh))
24 {
25 w = maxWidth;
26 h = (w * sh) / sw;
27 }
28 else
29 {
30 h = maxHeight;
31 w = (h * sw) / sh;
32 }
33
34 return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
35 }
36
37 /// <summary>
38 /// 对给定的一个图片(Image对象)生成一个指定大小的缩略图。
39 /// </summary>
40 /// <param name="originalImage">原始图片</param>
41 /// <param name="thumMaxWidth">缩略图的宽度</param>
42 /// <param name="thumMaxHeight">缩略图的高度</param>
43 /// <returns>返回缩略图的Image对象</returns>
44 public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)
45 {
46 Size thumRealSize = Size.Empty;
47 System.Drawing.Image newImage = originalImage;
48 Graphics graphics = null;
49
50 try
51 {
52 thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);
53 newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);
54 graphics = Graphics.FromImage(newImage);
55
56 graphics.CompositingQuality = CompositingQuality.HighQuality;
57 graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
58 graphics.SmoothingMode = SmoothingMode.HighQuality;
59
60 graphics.Clear(Color.Transparent);
61
62 graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
63 }
64 catch { }
65 finally
66 {
67 if (graphics != null)
68 {
69 graphics.Dispose();
70 graphics = null;
71 }
72 }
73
74 return newImage;
75 }
76 /// <summary>
77 /// 对给定的一个图片文件生成一个指定大小的缩略图。
78 /// </summary>
79 /// <param name="originalImage">图片的物理文件地址</param>
80 /// <param name="thumMaxWidth">缩略图的宽度</param>
81 /// <param name="thumMaxHeight">缩略图的高度</param>
82 /// <returns>返回缩略图的Image对象</returns>
83 public static System.Drawing.Image GetThumbNailImage(string imageFile, int thumMaxWidth, int thumMaxHeight)
84 {
85 System.Drawing.Image originalImage = null;
86 System.Drawing.Image newImage = null;
87
88 try
89 {
90 originalImage = System.Drawing.Image.FromFile(imageFile);
91 newImage = GetThumbNailImage(originalImage, thumMaxWidth, thumMaxHeight);
92 }
93 catch { }
94 finally
95 {
96 if (originalImage != null)
97 {
98 originalImage.Dispose();
99 originalImage = null;
100 }
101 }
102
103 return newImage;
104 }
105 /// <summary>
106 /// 对给定的一个图片文件生成一个指定大小的缩略图,并将缩略图保存到指定位置。
107 /// </summary>
108 /// <param name="originalImageFile">图片的物理文件地址</param>
109 /// <param name="thumbNailImageFile">缩略图的物理文件地址</param>
110 /// <param name="thumMaxWidth">缩略图的宽度</param>
111 /// <param name="thumMaxHeight">缩略图的高度</param>
112 public static void MakeThumbNail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight)
113 {
114 System.Drawing.Image newImage = GetThumbNailImage(originalImageFile, thumMaxWidth, thumMaxHeight);
115 try
116 {
117 newImage.Save(thumbNailImageFile, ImageFormat.Jpeg);
118 }
119 catch
120 { }
121 finally
122 {
123 newImage.Dispose();
124 newImage = null;
125 }
126 }
127 /// <summary>
128 /// 将一个图片的内存流调整为指定大小,并返回调整后的内存流。
129 /// </summary>
130 /// <param name="originalImageStream">原始图片的内存流</param>
131 /// <param name="newWidth">新图片的宽度</param>
132 /// <param name="newHeight">新图片的高度</param>
133 /// <returns>返回调整后的图片的内存流</returns>
134 public static MemoryStream ResizeImage(Stream originalImageStream, int newWidth, int newHeight)
135 {
136 MemoryStream newImageStream = null;
137
138 System.Drawing.Image newImage = Globals.GetThumbNailImage(System.Drawing.Image.FromStream(originalImageStream), newWidth, newHeight);
139 if (newImage != null)
140 {
141 newImageStream = new MemoryStream();
142 newImage.Save(newImageStream, ImageFormat.Jpeg);
143 }
144
145 return newImageStream;
146 }
147 /// <summary>
148 /// 将一个内存流保存为磁盘文件。
149 /// </summary>
150 /// <param name="stream">内存流</param>
151 /// <param name="newFile">目标磁盘文件地址</param>
152 public static void SaveStreamToFile(Stream stream, string newFile)
153 {
154 if (stream == null || stream.Length == 0 || string.IsNullOrEmpty(newFile))
155 {
156 return;
157 }
158
159 byte[] buffer = new byte[stream.Length];
160 stream.Position = 0;
161 stream.Read(buffer, 0, buffer.Length);
162 FileStream fileStream = new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write);
163 fileStream.Write(buffer, 0, buffer.Length);
164 fileStream.Flush();
165 fileStream.Close();
166 fileStream.Dispose();
167 }
168 /// <summary>
169 /// 对一个指定的图片加上图片水印效果。
170 /// </summary>
171 /// <param name="imageFile">图片文件地址</param>
172 /// <param name="waterImage">水印图片(Image对象)</param>
173 public static void CreateImageWaterMark(string imageFile, System.Drawing.Image waterImage)
174 {
175 if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
176 {
177 return;
178 }
179
180 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
181
182 if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)
183 {
184 return;
185 }
186
187 Graphics graphics = Graphics.FromImage(originalImage);
188
189 int x = originalImage.Width - waterImage.Width - 10;
190 int y = originalImage.Height - waterImage.Height - 10;
191 int width = waterImage.Width;
192 int height = waterImage.Height;
193
194 graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
195 graphics.Dispose();
196
197 MemoryStream stream = new MemoryStream();
198 originalImage.Save(stream, ImageFormat.Jpeg);
199 originalImage.Dispose();
200
201 System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
202
203 imageWithWater.Save(imageFile);
204 imageWithWater.Dispose();
205 }
206 /// <summary>
207 /// 对一个指定的图片加上文字水印效果。
208 /// </summary>
209 /// <param name="imageFile">图片文件地址</param>
210 /// <param name="waterText">水印文字内容</param>
211 public static void CreateTextWaterMark(string imageFile, string waterText)
212 {
213 if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile))
214 {
215 return;
216 }
217
218 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
219
220 Graphics graphics = Graphics.FromImage(originalImage);
221
222 graphics.SmoothingMode = SmoothingMode.HighQuality;
223 graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
224 graphics.CompositingQuality = CompositingQuality.HighQuality;
225 graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
226
227 SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
228 Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);
229 SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);
230
231 float x = (float)originalImage.Width - waterTextSize.Width - 10F;
232 float y = (float)originalImage.Height - waterTextSize.Height - 10F;
233
234 graphics.DrawString(waterText, waterTextFont, brush, x, y);
235
236 graphics.Dispose();
237 brush.Dispose();
238
239 MemoryStream stream = new MemoryStream();
240 originalImage.Save(stream, ImageFormat.Jpeg);
241 originalImage.Dispose();
242
243 System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
244
245 imageWithWater.Save(imageFile);
246 imageWithWater.Dispose();
247 }
248
249 /// <summary>
250 /// 判断上传组件是否包含内容。
251 /// </summary>
252 /// <param name="fileUpload">ASP.NET 2.0标准上传组件</param>
253 /// <returns>如果数据有效,则返回True,否则返回False</returns>
254 public static bool IsAttachmentValid(FileUpload fileUpload)
255 {
256 if (fileUpload != null &&
257 fileUpload.PostedFile != null &&
258 !string.IsNullOrEmpty(fileUpload.PostedFile.FileName) &&
259 fileUpload.PostedFile.ContentLength > 0)
260 {
261 return true;
262 }
263 return false;
264 }
265

浙公网安备 33010602011771号