Image 保存到MemoryStream FileSize 变大是怎么回事?原本300kb 的图片保存之后变500多kb 了?
费解的很!!!!
怎么这么多奇怪不清晰的问题 浮云遮望眼啊
1 /// <summary> 2 /// 传送图片到ftp 3 /// 示例: 4 /// ReceiveAndSaveImageToFtp(img,1234,"ftp://usn:pwd@ip.ip:21","/htdoc/essay/","http://i1.zzimg.com/essay/"); 5 /// </summary> 6 /// <param name="image"></param> 7 /// <param name="id"></param> 8 /// <param name="ftpUri"></param> 9 /// <param name="saveFtpPath"></param> 10 /// <param name="httpAddressPrefix"></param> 11 /// <returns></returns> 12 public PicInfoEntity ReceiveAndSaveImageToFtp(Image image, int id, string ftpUri, string saveFtpPath, string httpAddressPrefix) 13 { 14 PicInfoEntity entity = new PicInfoEntity(); 15 16 Image originalImage, smallImage, middleImage; 17 if (watermarkImageEnable) 18 { 19 Image waterImg = Image.FromFile(watermarkImagePath); 20 21 22 originalImage = ImageHelper.WatermarkImage(image, waterImg, watermarkTRBL); 23 24 smallImage = ImageHelper.GetZipImageThumbnail(originalImage, 128, 86);//宽128 缩放 25 middleImage = ImageHelper.GetZipImageThumbnail(originalImage, 400, 86); 26 27 } 28 else 29 { 30 originalImage = image; 31 smallImage = ImageHelper.GetZipImageThumbnail(originalImage, 128, 86);//宽128 缩放 32 middleImage = ImageHelper.GetZipImageThumbnail(originalImage, 400, 86); 33 34 } 35 36 37 int parentFolderName = getParentFolderName(id); 38 39 if (!FtpHelper.FtpExistsFolder(ftpUri, saveFtpPath, parentFolderName.ToString())) 40 { 41 FtpHelper.FtpCreateFolder(ftpUri, saveFtpPath + parentFolderName); 42 } 43 44 45 if (!FtpHelper.FtpExistsFolder(ftpUri, saveFtpPath + parentFolderName, id.ToString())) 46 { 47 48 FtpHelper.FtpCreateFolder(ftpUri, saveFtpPath + parentFolderName + "/" + id); 49 } 50 51 52 string imageExt = getImageExt(image); 53 54 if (imageExt == null) 55 { 56 return null; 57 } 58 59 60 using (MemoryStream stream = new MemoryStream()) 61 { 62 originalImage.Save(stream, image.RawFormat); 63 byte[] data = stream.GetBuffer(); 64 stream.Seek(0, SeekOrigin.Begin); 65 66 FtpHelper.FtpSaveFile(ftpUri, data, saveFtpPath + parentFolderName + "/" + id + "/" + "original" + imageExt); 67 } 68 69 using (MemoryStream stream = new MemoryStream()) 70 { 71 smallImage.Save(stream, image.RawFormat); 72 byte[] data = stream.GetBuffer(); 73 stream.Seek(0, SeekOrigin.Begin); 74 75 FtpHelper.FtpSaveFile(ftpUri, data, saveFtpPath + parentFolderName + "/" + id + "/" + "small" + imageExt); 76 } 77 78 79 80 using (MemoryStream stream = new MemoryStream()) 81 { 82 if (imageExt == ".gif") 83 { 84 originalImage.Save(stream, image.RawFormat); 85 86 } 87 else 88 { 89 middleImage.Save(stream, image.RawFormat); 90 } 91 92 stream.Seek(0, SeekOrigin.Begin); 93 94 byte[] data = stream.GetBuffer(); 95 FtpHelper.FtpSaveFile(ftpUri, data, saveFtpPath + parentFolderName + "/" + id + "/" + "middle" + imageExt); 96 97 } 98 99 100 if (httpAddressPrefix.LastIndexOf("/") != httpAddressPrefix.Length - 1) 101 { 102 httpAddressPrefix += "/"; 103 } 104 105 entity.Original = httpAddressPrefix + parentFolderName + "/" + id + "/" + "original" + imageExt; 106 entity.Middle = httpAddressPrefix + parentFolderName + "/" + id + "/" + "middle" + imageExt; 107 entity.Small = httpAddressPrefix + parentFolderName + "/" + id + "/" + "small" + imageExt; 108 109 return entity; 110 }
ImageHelper类
1 public class ImageHelper 2 { 3 4 public class WatermarkTRBL 5 { 6 public WatermarkTRBL() 7 { 8 9 } 10 int? top = null, right = null, bottom = null, left = null; 11 public WatermarkTRBL(int? top, int? right, int? bottom, int? left) 12 { 13 this.top = top; 14 this.right = right; 15 this.bottom = bottom; 16 this.left = left; 17 } 18 19 public int? Top 20 { 21 get { return top; } 22 set { top = value; } 23 } 24 public int? Right 25 { 26 get { return right; } 27 set { right = value; } 28 } 29 30 public int? Bottom 31 { 32 get { return bottom; } 33 set { bottom = value; } 34 } 35 36 public int? Left 37 { 38 get { return left; } 39 set { left = value; } 40 } 41 } 42 43 44 /// <summary> 45 /// 给图片加水印 46 /// </summary> 47 /// <param name="originalImg"></param> 48 /// <param name="waterImg"></param> 49 /// <param name="trbl"></param> 50 /// <returns></returns> 51 public static Image WatermarkImage(Image originalImg, Image waterImg, WatermarkTRBL trbl) 52 { 53 54 Image image = (Image)originalImg.Clone(); 55 using (Graphics g = Graphics.FromImage(image)) 56 { 57 Rectangle r = new Rectangle(); 58 r.Height = waterImg.Height; 59 r.Width = waterImg.Width; 60 if (trbl.Top != null) 61 { 62 r.Y = (int)trbl.Top; 63 } 64 65 if (trbl.Right != null) 66 { 67 r.X = originalImg.Width - waterImg.Width - (int)trbl.Right; 68 } 69 70 if (trbl.Bottom != null) 71 { 72 r.Y = originalImg.Height - waterImg.Height - (int)trbl.Bottom; 73 } 74 75 if (trbl.Left != null) 76 { 77 r.X = (int)trbl.Left; 78 } 79 80 g.DrawImage(waterImg, r, 0, 0, waterImg.Width, waterImg.Height, GraphicsUnit.Pixel); 81 } 82 83 return image; 84 } 85 86 87 88 /// <summary> 89 /// 等比缩放图片 90 /// </summary> 91 /// <param name="imgPhoto"></param> 92 /// <param name="width"></param> 93 /// <param name="height"></param> 94 /// <returns></returns> 95 public static System.Drawing.Image GetImageThumbnail(System.Drawing.Image imgPhoto, int width, int height) 96 { 97 98 int sourceWidth = imgPhoto.Width; 99 int sourceHeight = imgPhoto.Height; 100 101 102 //int x, y; //位置 103 int w, h; //缩放后的宽和高 104 105 if (sourceWidth > width || sourceHeight > height) 106 { 107 decimal wPercent = (decimal)sourceWidth / width; 108 decimal hPercent = (decimal)sourceHeight / height; 109 decimal nPercent; 110 if (hPercent > wPercent) 111 { 112 nPercent = hPercent; 113 } 114 else 115 { 116 nPercent = wPercent; 117 } 118 119 w = (int)Math.Round(sourceWidth / nPercent, 0); 120 h = (int)Math.Round(sourceHeight / nPercent, 0); 121 122 123 124 } 125 else 126 { 127 w = sourceWidth; 128 h = sourceHeight; 129 } 130 131 132 133 134 try 135 { 136 137 Bitmap bmPhoto = new Bitmap(w, h, PixelFormat.Format24bppRgb); 138 //bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 139 Graphics grPhoto = Graphics.FromImage(bmPhoto); 140 //grPhoto.Clear(Color.White); 141 //grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 142 //设置高质量插值法 143 grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 144 145 //设置高质量,低速度呈现平滑程度 146 grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 147 148 //清空画布并以透明背景色填充 149 grPhoto.Clear(System.Drawing.Color.Transparent); 150 151 152 grPhoto.DrawImage(imgPhoto, 153 new Rectangle(0, 0, w, h),///它指定所绘制图像的位置和大小。 154 new Rectangle(0, 0, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。 155 GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。 156 157 grPhoto.Dispose(); 158 159 if (imgPhoto.RawFormat == ImageFormat.Gif || imgPhoto.RawFormat.Guid == ImageFormat.Gif.Guid) 160 { 161 OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); 162 using (Bitmap quantized = quantizer.Quantize(imgPhoto)) 163 { 164 return quantized; 165 } 166 } 167 else 168 { 169 return bmPhoto; 170 } 171 172 } 173 catch 174 { 175 176 return null; 177 } 178 } 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 public static System.Drawing.Image GetImageThumbnail(System.Drawing.Image imgPhoto, int width) 197 { 198 199 int sourceWidth = imgPhoto.Width; 200 int sourceHeight = imgPhoto.Height; 201 202 203 //int x, y; //位置 204 int w, h; //缩放后的宽和高 205 206 if (sourceWidth > width) 207 { 208 decimal nPercent = (decimal)sourceWidth / width; 209 w = (int)Math.Round(sourceWidth / nPercent, 0); 210 h = (int)Math.Round(sourceHeight / nPercent, 0); 211 } 212 else 213 { 214 w = sourceWidth; 215 h = sourceHeight; 216 } 217 218 219 try 220 { 221 Bitmap bmPhoto = new Bitmap(w, h); 222 //bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 223 Graphics grPhoto = Graphics.FromImage(bmPhoto); 224 //grPhoto.Clear(Color.White); 225 //grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 226 //设置高质量插值法 227 grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 228 229 //设置高质量,低速度呈现平滑程度 230 grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 231 232 //清空画布并以透明背景色填充 233 grPhoto.Clear(System.Drawing.Color.Transparent); 234 235 236 grPhoto.DrawImage(imgPhoto, 237 new Rectangle(0, 0, w, h),///它指定所绘制图像的位置和大小。 238 new Rectangle(0, 0, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。 239 GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。 240 241 grPhoto.Dispose(); 242 243 if (imgPhoto.RawFormat == ImageFormat.Gif || imgPhoto.RawFormat.Guid == ImageFormat.Gif.Guid) 244 { 245 OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); 246 return quantizer.Quantize(bmPhoto); 247 } 248 else 249 { 250 return bmPhoto; 251 } 252 253 } 254 catch 255 { 256 257 return null; 258 } 259 } 260 261 262 263 264 265 266 267 public static System.Drawing.Image GetZipImageThumbnail(System.Drawing.Image imgPhoto, int width, int height, int flag) 268 { 269 Image thumbnail = GetImageThumbnail(imgPhoto, width, height); 270 return ZipImage(thumbnail, flag); 271 } 272 273 274 public static System.Drawing.Image GetZipImageThumbnail(System.Drawing.Image imgPhoto, int width, int flag) 275 { 276 Image thumbnail = GetImageThumbnail(imgPhoto, width); 277 return ZipImage(thumbnail, flag); 278 279 } 280 281 282 283 284 285 286 287 288 /// <summary> 289 /// 压缩图片 290 /// </summary> 291 /// <param name="imgPhoto"></param> 292 /// <param name="flag"></param> 293 /// <returns></returns> 294 public static System.Drawing.Image ZipImage(System.Drawing.Image imgPhoto, int flag) 295 { 296 EncoderParameters ep = new EncoderParameters(); 297 long[] qy = new long[1]; 298 qy[0] = flag;//设置压缩的比例1-100 299 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); 300 ep.Param[0] = eParam; 301 302 try 303 { 304 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); 305 ImageCodecInfo jpegICIinfo = null; 306 for (int x = 0; x < arrayICI.Length; x++) 307 { 308 if (arrayICI[x].FormatDescription.Equals("JPEG")) 309 { 310 jpegICIinfo = arrayICI[x]; 311 break; 312 } 313 } 314 315 MemoryStream ms = new MemoryStream(); 316 317 318 if (jpegICIinfo != null) 319 { 320 321 imgPhoto.Save(ms, jpegICIinfo, ep);//dFile是压缩后的新路径 322 } 323 else 324 { 325 imgPhoto.Save(ms, imgPhoto.RawFormat); 326 } 327 328 System.Drawing.Image zipImage = System.Drawing.Image.FromStream(ms); 329 return zipImage; 330 } 331 catch 332 { 333 return null; 334 } 335 } 336 337 338 }
1、有发现变大后的图片是否正常吗?(在感官上是一致的)
2、有比较过两个图片大小的差异是多少吗?
3、有用图片工具比较过吗?
可能使用.NET的IMAGE对象存储的时候,会添加或删除一些数据头,从而导致文件变大或变小。