各式各样的图片缩略水印静态类

今天想学习一下图像变形的技巧,无意间在网上看到这个代码,觉得不错,转发一下:

  1using System;
  2using System.Collections.Generic;
  3using System.Drawing;
  4using System.Drawing.Drawing2D;
  5using System.Drawing.Imaging;
  6namespace Steam.Core
  7{
  8public static class ImageDeal
  9{
 10/**//// <summary>
 11/// 缩略图,按高度和宽度来缩略
 12///http://www.cnblogs.com/pooeo/
 13/// </summary>
 14/// <param name="image"></param>
 15/// <param name="size"></param>
 16/// <returns></returns>

 17public static Image Scale(Image image, Size size)
 18{
 19return image.GetThumbnailImage(size.Width, size.Height, nullnew IntPtr());
 20}

 21/**//// <summary>
 22/// 缩略图,按倍数来缩略
 23///http://www.cnblogs.com/pooeo/
 24/// </summary>
 25/// <param name="image">原图</param>
 26/// <param name="multiple">放大或缩小的倍数,负数表示缩小,正数表示放大</param>
 27/// <returns></returns>

 28public static Image Scale(Image image, Int32 multiple)
 29{
 30Int32 newWidth;
 31Int32 newHeight;
 32Int32 absMultiple = Math.Abs(multiple);
 33if (multiple == 0)
 34{
 35return image.Clone() as Image;
 36}

 37if (multiple < 0)
 38{
 39newWidth = image.Width / absMultiple;
 40newHeight = image.Height / absMultiple;
 41}

 42else
 43{
 44newWidth = image.Width * absMultiple;
 45newHeight = image.Height * absMultiple;
 46}

 47return image.GetThumbnailImage(newWidth, newHeight, nullnew IntPtr());
 48}

 49/**//// <summary>
 50/// 固定宽度缩略
 51///http://www.cnblogs.com/pooeo/
 52/// </summary>
 53/// <param name="image"></param>
 54/// <param name="width"></param>
 55/// <returns></returns>

 56public static Image ScaleFixWidth(Image image, Int32 width)
 57{
 58Int32 newWidth = width;
 59Int32 newHeight;
 60Double tempMultiple = (Double)newWidth / (Double)image.Width;
 61newHeight = (Int32)(((Double)image.Height) * tempMultiple);
 62Image newImage = new Bitmap(newWidth, newHeight);
 63using (Graphics newGp = Graphics.FromImage(newImage))
 64{
 65newGp.CompositingQuality = CompositingQuality.HighQuality;
 66//设置高质量插值法
 67newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
 68//设置高质量,低速度呈现平滑程度
 69newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
 70//清空画布并以透明背景色填充
 71newGp.Clear(Color.Transparent);
 72newGp.DrawImage(image, new Rectangle(00, newWidth, newHeight));
 73}

 74return newImage;
 75}

 76/**//// <summary>
 77/// 固定高度缩略
 78///http://www.cnblogs.com/pooeo/
 79/// </summary>
 80/// <param name="image"></param>
 81/// <param name="height"></param>
 82/// <returns></returns>

 83public static Image ScaleFixHeight(Image image, Int32 height)
 84{
 85Int32 newWidth;
 86Int32 newHeight = height;
 87Double tempMultiple = (Double)newHeight / (Double)image.Height;
 88newWidth = (Int32)(((Double)image.Width) * tempMultiple);
 89Image newImage = new Bitmap(newWidth, newHeight);
 90using (Graphics newGp = Graphics.FromImage(newImage))
 91{
 92newGp.CompositingQuality = CompositingQuality.HighQuality;
 93//设置高质量插值法
 94newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
 95//设置高质量,低速度呈现平滑程度
 96newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
 97//清空画布并以透明背景色填充
 98newGp.Clear(Color.Transparent);
 99newGp.DrawImage(image, new Rectangle(00, newWidth, newHeight));
100}

101return newImage;
102}

103/**//// <summary>
104/// 裁减缩略,根据固定的高度和宽度
105///http://www.cnblogs.com/pooeo/
106/// </summary>
107/// <param name="image"></param>
108/// <param name="width"></param>
109/// <param name="heigth"></param>
110/// <returns></returns>

111public static Image ScaleCut(Image image, Int32 width, Int32 height)
112{
113int x = 0;
114int y = 0;
115int ow = image.Width;
116int oh = image.Height;
117if (width >= ow && height >= oh)
118{
119return image;
120}

121//如果结果要比原来的宽
122if (width > ow)
123{
124width = ow;
125}

126if (height > oh)
127{
128height = oh;
129}

130if ((double)image.Width / (double)image.Height > (double)width / (double)height)
131{
132oh = image.Height;
133ow = image.Height * width / height;
134= 0;
135= (image.Width - ow) / 2;
136}

137else
138{
139ow = image.Width;
140oh = image.Width * height / width;
141= 0;
142= (image.Height - oh) / 2;
143}

144Image newImage = new Bitmap(width, height);
145using (Graphics newGp = Graphics.FromImage(newImage))
146{
147newGp.CompositingQuality = CompositingQuality.HighQuality;
148//设置高质量插值法
149newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
150//设置高质量,低速度呈现平滑程度
151newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
152//清空画布并以透明背景色填充
153newGp.Clear(Color.Transparent);
154newGp.DrawImage(image, new Rectangle(00, width, height),
155new Rectangle(x, y, ow, oh),
156GraphicsUnit.Pixel);
157}

158return newImage;
159}

160/**//// <summary>
161/// 生成缩略图
162///http://www.cnblogs.com/pooeo/
163/// </summary>
164/// <param name="originalImagePath">源图路径(物理路径)</param>
165/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
166/// <param name="width">缩略图宽度</param>
167/// <param name="height">缩略图高度</param>
168/// <param name="mode">生成缩略图的方式</param>

169public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
170{
171Image originalImage = Image.FromFile(originalImagePath);
172int towidth = width;
173int toheight = height;
174int x = 0;
175int y = 0;
176int ow = originalImage.Width;
177int oh = originalImage.Height;
178switch (mode)
179{
180case "HW"://指定高宽缩放(可能变形)
181break;
182case "W"://指定宽,高按比例
183toheight = originalImage.Height * width / originalImage.Width;
184break;
185case "H"://指定高,宽按比例
186towidth = originalImage.Width * height / originalImage.Height;
187break;
188case "Cut"://指定高宽裁减(不变形)
189if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
190{
191oh = originalImage.Height;
192ow = originalImage.Height * towidth / toheight;
193= 0;
194= (originalImage.Width - ow) / 2;
195}

196else
197{
198ow = originalImage.Width;
199oh = originalImage.Width * height / towidth;
200= 0;
201= (originalImage.Height - oh) / 2;
202}

203break;
204default:
205break;
206}

207//新建一个bmp图片
208Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
209//新建一个画板
210Graphics g = System.Drawing.Graphics.FromImage(bitmap);
211//设置高质量插值法
212g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
213//设置高质量,低速度呈现平滑程度
214g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
215//清空画布并以透明背景色填充
216g.Clear(Color.Transparent);
217//在指定位置并且按指定大小绘制原图片的指定部分
218g.DrawImage(originalImage, new Rectangle(00, towidth, toheight),
219new Rectangle(x, y, ow, oh),
220GraphicsUnit.Pixel);
221try
222{
223//以jpg格式保存缩略图
224bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
225}

226catch (System.Exception e)
227{
228throw e;
229}

230finally
231{
232originalImage.Dispose();
233bitmap.Dispose();
234g.Dispose();
235}

236}

237/**//// <summary>
238/// 打水印,在某一点
239///http://www.cnblogs.com/pooeo/
240/// </summary>
241/// <param name="image"></param>
242/// <param name="waterImagePath"></param>
243/// <param name="p"></param>

244public static void Makewater(Image image, String waterImagePath, Point p)
245{
246ImageDeal.Makewater(image, waterImagePath, p, ImagePosition.TopLeft);
247}

248public static void Makewater(Image image, String waterImagePath, Point p, ImagePosition imagePosition)
249{
250using (Image warterImage = Image.FromFile(waterImagePath))
251{
252using (Graphics newGp = Graphics.FromImage(image))
253{
254newGp.CompositingQuality = CompositingQuality.HighQuality;
255//设置高质量插值法
256newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
257//设置高质量,低速度呈现平滑程度
258newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
259switch (imagePosition)
260{
261case ImagePosition.BottomLeft:
262p.Y = image.Height - warterImage.Height - p.Y;
263break;
264case ImagePosition.TopRigth:
265p.X = image.Width - warterImage.Width - p.X;
266break;
267case ImagePosition.BottomRight:
268p.Y = image.Height - warterImage.Height - p.Y;
269p.X = image.Width - warterImage.Width - p.X;
270break;
271}

272newGp.DrawImage(warterImage, new Rectangle(p, new Size(warterImage.Width, warterImage.Height)));
273}

274}

275}

276public static void Makewater(Image image, String waterStr, Font font, Brush brush, Point p)
277{
278ImageDeal.Makewater(image, waterStr, font, brush, p, ImagePosition.TopLeft);
279}

280public static void Makewater(Image image, String waterStr, Font font, Brush brush, Point p, ImagePosition imagePosition)
281{
282using (Graphics newGp = Graphics.FromImage(image))
283{
284Int32 stringWidth;
285Int32 stringHeight;
286stringHeight = (int)font.Size;
287stringWidth = (int)(((float)StringDeal.GetBitLength(waterStr) / (float)2* (font.Size + 1));
288newGp.CompositingQuality = CompositingQuality.HighQuality;
289//设置高质量插值法
290newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
291//设置高质量,低速度呈现平滑程度
292newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
293//文字抗锯齿
294newGp.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
295switch (imagePosition)
296{
297case ImagePosition.BottomLeft:
298p.Y = image.Height - stringHeight - p.Y;
299break;
300case ImagePosition.TopRigth:
301p.X = image.Width - stringWidth - p.X;
302break;
303case ImagePosition.BottomRight:
304p.Y = image.Height - stringHeight - p.Y;
305p.X = image.Width - stringWidth - p.X;
306break;
307}

308newGp.DrawString(waterStr, font, brush, p);
309}

310}

311/**//// <summary>
312/// 高质量保存
313///http://www.cnblogs.com/pooeo/
314/// </summary>
315/// <param name="image"></param>
316/// <param name="path"></param>

317public static void SaveQuality(Image image, String path)
318{
319ImageCodecInfo myImageCodecInfo;
320Encoder myEncoder;
321EncoderParameter myEncoderParameter;
322EncoderParameters myEncoderParameters;
323myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
324myEncoder = Encoder.Quality;
325myEncoderParameters = new EncoderParameters(1);
326myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0-100
327myEncoderParameters.Param[0= myEncoderParameter;
328try
329{
330image.Save(path, myImageCodecInfo, myEncoderParameters);
331}

332finally
333{
334myEncoderParameter.Dispose();
335myEncoderParameters.Dispose();
336}

337}

338}

339public enum StringPosition
340{
341TopLeft,
342BottomLeft
343}

344public enum ImagePosition
345{
346TopLeft,
347BottomLeft,
348BottomRight,
349TopRigth
350}

351}
posted @ 2007-10-06 11:54  元宝  阅读(698)  评论(2编辑  收藏  举报
我最爱的地质软件?!……