1 usingImageManipulation;2 usingSystem;3 usingSystem.Drawing;4 usingSystem.Drawing.Drawing2D;5 usingSystem.Drawing.Imaging;6 usingSystem.IO;7
8
9
10 public classImageHelper11 {12 public Image GetImageThumbnailByHeight(Image imgPhoto, intheight)13 {14 decimal nPercent = (decimal)imgPhoto.Height / (decimal)height;15
16 if (nPercent > 1)17 {18 return GetImageThumbnail(imgPhoto, Convert.ToInt32(Math.Round(imgPhoto.Width / nPercent, 0)), height);19 }20 else
21 {22 returnimgPhoto;23 }24
25 }26
27
28 public Image GetImageThumbnail(Image imgPhoto, intwidth)29 {30 decimal nPercent = (decimal)imgPhoto.Width / (decimal)width;31
32 if (nPercent > 1)33 {34 return GetImageThumbnail(imgPhoto, width, Convert.ToInt32(Math.Round(imgPhoto.Height / nPercent, 0)));35 }36 else
37 {38 returnimgPhoto;39 }40
41 }42
43 public Image GetImageThumbnail(Image imgPhoto, int width, intheight)44 {45 decimal nPercent = 1;46 decimal wPercent = (decimal)imgPhoto.Width / (decimal)width;47 decimal hPercent = (decimal)imgPhoto.Height / (decimal)height;48
49 nPercent = wPercent > hPercent ?wPercent : hPercent;50
51 int w = Convert.ToInt32(Math.Round(imgPhoto.Width / nPercent, 0));52 int h = Convert.ToInt32(Math.Round(imgPhoto.Height / nPercent, 0));53
54
55
56
57 try
58 {59 Bitmap image = newBitmap(w, h, PixelFormat.Format24bppRgb);60 Graphics graphics =Graphics.FromImage(image);61 graphics.InterpolationMode =InterpolationMode.Default;62 graphics.SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.HighQuality;63 graphics.DrawImage(imgPhoto, new Rectangle(0, 0, w, h), new Rectangle(0, 0, imgPhoto.Width, imgPhoto.Height), GraphicsUnit.Pixel);64 graphics.Dispose();65 if ((imgPhoto.RawFormat == ImageFormat.Gif) || (imgPhoto.RawFormat.Guid ==ImageFormat.Gif.Guid))66 {67 OctreeQuantizer quantizer = new OctreeQuantizer(0xff, 8);68 returnquantizer.Quantize(image);69 }70 returnimage;71 }72 catch
73 {74 return null;75 }76 }77
78 public Image GetZipImageThumbnailByHeight(Image imgPhoto, int height, intflag)79 {80 Image imageThumbnail = this.GetImageThumbnailByHeight(imgPhoto, height);81 return this.ZipImage(imageThumbnail, flag);82 }83
84 public Image GetZipImageThumbnail(Image imgPhoto, int width, intflag)85 {86 Image imageThumbnail = this.GetImageThumbnail(imgPhoto, width);87 return this.ZipImage(imageThumbnail, flag);88 }89
90 public Image GetZipImageThumbnail(Image imgPhoto, int width, int height, intflag)91 {92 Image image = this.GetImageThumbnail(imgPhoto, width, height);93 return this.ZipImage(image, flag);94 }95
96 publicImage WatermarkImage(Image originalImg, Image waterImg, WatermarkTRBL trbl)97 {98 Bitmap image = newBitmap(originalImg.Width, originalImg.Height, PixelFormat.Format32bppArgb);99 using (Graphics graphics =Graphics.FromImage(image))100 {101 graphics.InterpolationMode =InterpolationMode.Default;102 graphics.DrawImage(originalImg, new Rectangle(0, 0, originalImg.Width, originalImg.Height), 0, 0, originalImg.Width, originalImg.Height, GraphicsUnit.Pixel);103 Rectangle destRect = newRectangle104 {105 Height =waterImg.Height,106 Width =waterImg.Width107 };108 if(trbl.Top.HasValue)109 {110 destRect.Y =trbl.Top.Value;111 }112 if(trbl.Right.HasValue)113 {114 destRect.X = (originalImg.Width - waterImg.Width) -trbl.Right.Value;115 }116 if(trbl.Bottom.HasValue)117 {118 destRect.Y = (originalImg.Height - waterImg.Height) -trbl.Bottom.Value;119 }120 if(trbl.Left.HasValue)121 {122 destRect.X =trbl.Left.Value;123 }124 graphics.DrawImage(waterImg, destRect, 0, 0, waterImg.Width, waterImg.Height, GraphicsUnit.Pixel);125 }126 returnimage;127 }128
129 public Image ZipImage(Image imgPhoto, intflag)130 {131 EncoderParameters encoderParams = newEncoderParameters();132 long[] numArray = new long[] { (long)flag };133 EncoderParameter parameter = newEncoderParameter(System.Drawing.Imaging.Encoder.Quality, numArray);134 encoderParams.Param[0] =parameter;135 try
136 {137 ImageCodecInfo[] imageEncoders =ImageCodecInfo.GetImageEncoders();138 ImageCodecInfo encoder = null;139 for (int i = 0; i < imageEncoders.Length; i++)140 {141 if (imageEncoders[i].FormatDescription.Equals("JPEG"))142 {143 encoder =imageEncoders[i];144 break;145 }146 }147 MemoryStream stream = newMemoryStream();148 if (encoder != null)149 {150 imgPhoto.Save(stream, encoder, encoderParams);151 }152 else
153 {154 imgPhoto.Save(stream, imgPhoto.RawFormat);155 }156 returnImage.FromStream(stream);157 }158 catch
159 {160 return null;161 }162 }163
164 public classWatermarkTRBL165 {166 private int?bottom;167 private int?left;168 private int?right;169 private int?top;170
171 publicWatermarkTRBL()172 {173 this.top = null;174 this.right = null;175 this.bottom = null;176 this.left = null;177 }178
179 public WatermarkTRBL(int? top, int? right, int? bottom, int?left)180 {181 this.top = null;182 this.right = null;183 this.bottom = null;184 this.left = null;185 this.top =top;186 this.right =right;187 this.bottom =bottom;188 this.left =left;189 }190
191 public int?Bottom192 {193 get
194 {195 return this.bottom;196 }197 set
198 {199 this.bottom =value;200 }201 }202
203 public int?Left204 {205 get
206 {207 return this.left;208 }209 set
210 {211 this.left =value;212 }213 }214
215 public int?Right216 {217 get
218 {219 return this.right;220 }221 set
222 {223 this.right =value;224 }225 }226
227 public int?Top228 {229 get
230 {231 return this.top;232 }233 set
234 {235 this.top =value;236 }237 }238 }239
240 }