本地操作系统是 windows 7 .net framework 3.5 编译器 3.5.30729.5420
服务器为 windows 2008 .net framework 3.5 编译器 3.5.30729.1
比较费解
1 using ImageManipulation; 2 using System; 3 using System.Drawing; 4 using System.Drawing.Drawing2D; 5 using System.Drawing.Imaging; 6 using System.IO; 7 8 9 10 public class ImageHelper 11 { 12 public Image GetImageThumbnailByHeight(Image imgPhoto, int height) 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 return imgPhoto; 23 } 24 25 } 26 27 28 public Image GetImageThumbnail(Image imgPhoto, int width) 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 return imgPhoto; 39 } 40 41 } 42 43 public Image GetImageThumbnail(Image imgPhoto, int width, int height) 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 = new Bitmap(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 return quantizer.Quantize(image); 69 } 70 return image; 71 } 72 catch 73 { 74 return null; 75 } 76 } 77 78 public Image GetZipImageThumbnailByHeight(Image imgPhoto, int height, int flag) 79 { 80 Image imageThumbnail = this.GetImageThumbnailByHeight(imgPhoto, height); 81 return this.ZipImage(imageThumbnail, flag); 82 } 83 84 public Image GetZipImageThumbnail(Image imgPhoto, int width, int flag) 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, int flag) 91 { 92 Image image = this.GetImageThumbnail(imgPhoto, width, height); 93 return this.ZipImage(image, flag); 94 } 95 96 public Image WatermarkImage(Image originalImg, Image waterImg, WatermarkTRBL trbl) 97 { 98 Bitmap image = new Bitmap(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 = new Rectangle 104 { 105 Height = waterImg.Height, 106 Width = waterImg.Width 107 }; 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 return image; 127 } 128 129 public Image ZipImage(Image imgPhoto, int flag) 130 { 131 EncoderParameters encoderParams = new EncoderParameters(); 132 long[] numArray = new long[] { (long)flag }; 133 EncoderParameter parameter = new EncoderParameter(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 = new MemoryStream(); 148 if (encoder != null) 149 { 150 imgPhoto.Save(stream, encoder, encoderParams); 151 } 152 else 153 { 154 imgPhoto.Save(stream, imgPhoto.RawFormat); 155 } 156 return Image.FromStream(stream); 157 } 158 catch 159 { 160 return null; 161 } 162 } 163 164 public class WatermarkTRBL 165 { 166 private int? bottom; 167 private int? left; 168 private int? right; 169 private int? top; 170 171 public WatermarkTRBL() 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? Bottom 192 { 193 get 194 { 195 return this.bottom; 196 } 197 set 198 { 199 this.bottom = value; 200 } 201 } 202 203 public int? Left 204 { 205 get 206 { 207 return this.left; 208 } 209 set 210 { 211 this.left = value; 212 } 213 } 214 215 public int? Right 216 { 217 get 218 { 219 return this.right; 220 } 221 set 222 { 223 this.right = value; 224 } 225 } 226 227 public int? Top 228 { 229 get 230 { 231 return this.top; 232 } 233 set 234 { 235 this.top = value; 236 } 237 } 238 } 239 240 }
找到原因,windows 2008 版本问题, 服务器用的是windows 2008 enterprise 版本(非 win 2008 R2),
在 windows 2008 enterprise R2 上 图片处理的很好,gdiplus 版本可能不一致,功能也不一样 造成
看起来没啥区别呀,是显卡的问题吧。