首页新闻找找看学习计划

写了一个缩放图片的类,但是缩放过后,要比原图文件大小大很多(原图300KB,缩略图512KB)

0
[已关闭问题] 关闭于 2013-01-09 15:06
  1 using System;
  2 using System.Drawing;
  3 using System.Drawing.Drawing2D;
  4 using System.Drawing.Imaging;
  5 using System.IO;
  6 using ImageManipulation;
  7 
  8 namespace Zz.Common
  9 {
 10 
 11 
 12     public class ImageHelper
 13     {
 14 
 15         public class WatermarkTRBL
 16         {
 17             public WatermarkTRBL()
 18             {
 19 
 20             }
 21             int? top = null, right = null, bottom = null, left = null;
 22             public WatermarkTRBL(int? top, int? right, int? bottom, int? left)
 23             {
 24                 this.top = top;
 25                 this.right = right;
 26                 this.bottom = bottom;
 27                 this.left = left;
 28             }
 29 
 30             public int? Top
 31             {
 32                 get { return top; }
 33                 set { top = value; }
 34             }
 35             public int? Right
 36             {
 37                 get { return right; }
 38                 set { right = value; }
 39             }
 40 
 41             public int? Bottom
 42             {
 43                 get { return bottom; }
 44                 set { bottom = value; }
 45             }
 46 
 47             public int? Left
 48             {
 49                 get { return left; }
 50                 set { left = value; }
 51             }
 52         }
 53 
 54 
 55         /// <summary>
 56         /// 给图片加水印
 57         /// </summary>
 58         /// <param name="originalImg"></param>
 59         /// <param name="waterImg"></param>
 60         /// <param name="trbl"></param>
 61         /// <returns></returns>
 62         public static Image WatermarkImage(Image originalImg, Image waterImg, WatermarkTRBL trbl)
 63         {
 64 
 65             Image image = (Image)originalImg.Clone();
 66             using (Graphics g = Graphics.FromImage(image))
 67             {
 68                 Rectangle r = new Rectangle();
 69                 r.Height = waterImg.Height;
 70                 r.Width = waterImg.Width;
 71                 if (trbl.Top != null)
 72                 {
 73                     r.Y = (int)trbl.Top;
 74                 }
 75 
 76                 if (trbl.Right != null)
 77                 {
 78                     r.X = originalImg.Width - waterImg.Width - (int)trbl.Right;
 79                 }
 80 
 81                 if (trbl.Bottom != null)
 82                 {
 83                     r.Y = originalImg.Height - waterImg.Height - (int)trbl.Bottom;
 84                 }
 85 
 86                 if (trbl.Left != null)
 87                 {
 88                     r.X = (int)trbl.Left;
 89                 }
 90 
 91                 g.DrawImage(waterImg, r, 0, 0, waterImg.Width, waterImg.Height, GraphicsUnit.Pixel);
 92             }
 93 
 94             return image;
 95         }
 96 
 97 
 98 
 99         /// <summary>
100         /// 等比缩放图片
101         /// </summary>
102         /// <param name="imgPhoto"></param>
103         /// <param name="width"></param>
104         /// <param name="height"></param>
105         /// <returns></returns>
106         public static System.Drawing.Image GetImageThumbnail(System.Drawing.Image imgPhoto, int width, int height)
107         {
108 
109             int sourceWidth = imgPhoto.Width;
110             int sourceHeight = imgPhoto.Height;
111 
112 
113             //int x, y;  //位置
114             int w, h; //缩放后的宽和高
115 
116             if (sourceWidth > width || sourceHeight > height)
117             {
118                 decimal wPercent = (decimal)sourceWidth / width;
119                 decimal hPercent = (decimal)sourceHeight / height;
120                 decimal nPercent;
121                 if (hPercent > wPercent)
122                 {
123                     nPercent = hPercent;
124                 }
125                 else
126                 {
127                     nPercent = wPercent;
128                 }
129 
130                 w = (int)Math.Round(sourceWidth / nPercent, 0);
131                 h = (int)Math.Round(sourceHeight / nPercent, 0);
132 
133 
134 
135             }
136             else
137             {
138                 w = sourceWidth;
139                 h = sourceHeight;
140             }
141 
142 
143 
144 
145             try
146             {
147 
148                 Bitmap bmPhoto = new Bitmap(w, h, PixelFormat.Format24bppRgb);
149                 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
150                 Graphics grPhoto = Graphics.FromImage(bmPhoto);
151                 grPhoto.Clear(Color.White);
152                 grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
153 
154                 grPhoto.DrawImage(imgPhoto,
155                     new Rectangle(0, 0, w, h),///它指定所绘制图像的位置和大小。
156                     new Rectangle(0, 0, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。
157                     GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。
158 
159                 grPhoto.Dispose();
160 
161                 if (imgPhoto.RawFormat == ImageFormat.Gif || imgPhoto.RawFormat.Guid == ImageFormat.Gif.Guid)
162                 {
163                     OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);
164                     using (Bitmap quantized = quantizer.Quantize(imgPhoto))
165                     {
166                         return quantized;
167                     }
168                 }
169                 else
170                 {
171                     return bmPhoto;
172                 }
173 
174             }
175             catch
176             {
177 
178                 return null;
179             }
180         }
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198         public static System.Drawing.Image GetImageThumbnail(System.Drawing.Image imgPhoto, int width)
199         {
200 
201             int sourceWidth = imgPhoto.Width;
202             int sourceHeight = imgPhoto.Height;
203 
204 
205             //int x, y;  //位置
206             int w, h; //缩放后的宽和高
207 
208             if (sourceWidth > width)
209             {
210                 decimal nPercent = (decimal)sourceWidth / width;
211                 w = (int)Math.Round(sourceWidth / nPercent, 0);
212                 h = (int)Math.Round(sourceHeight / nPercent, 0);
213             }
214             else
215             {
216                 w = sourceWidth;
217                 h = sourceHeight;
218             }
219 
220 
221             try
222             {
223                 Bitmap bmPhoto = new Bitmap(w, h, PixelFormat.Format24bppRgb);
224                 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
225                 Graphics grPhoto = Graphics.FromImage(bmPhoto);
226                 grPhoto.Clear(Color.White);
227                 grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
228 
229                 grPhoto.DrawImage(imgPhoto,
230                     new Rectangle(0, 0, w, h),///它指定所绘制图像的位置和大小。
231                     new Rectangle(0, 0, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。
232                     GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。
233 
234                 grPhoto.Dispose();
235 
236                 if (imgPhoto.RawFormat == ImageFormat.Gif || imgPhoto.RawFormat.Guid == ImageFormat.Gif.Guid)
237                 {
238                     OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);
239                     return quantizer.Quantize(bmPhoto);
240                 }
241                 else
242                 {
243                     return bmPhoto;
244                 }
245 
246             }
247             catch
248             {
249 
250                 return null;
251             }
252         }
253 
254 
255 
256 
257 
258 
259 
260         public static System.Drawing.Image GetZipImageThumbnail(System.Drawing.Image imgPhoto, int width, int height, int flag)
261         {
262             Image thumbnail = GetImageThumbnail(imgPhoto, width, height);
263             return ZipImage(thumbnail, flag);
264         }
265 
266 
267         public static System.Drawing.Image GetZipImageThumbnail(System.Drawing.Image imgPhoto, int width, int flag)
268         {
269             Image thumbnail = GetImageThumbnail(imgPhoto, width);
270             return ZipImage(thumbnail, flag);
271 
272         }
273 
274 
275 
276 
277 
278 
279 
280 
281         /// <summary>
282         /// 压缩图片
283         /// </summary>
284         /// <param name="imgPhoto"></param>
285         /// <param name="flag"></param>
286         /// <returns></returns>
287         public static System.Drawing.Image ZipImage(System.Drawing.Image imgPhoto, int flag)
288         {
289             EncoderParameters ep = new EncoderParameters();
290             long[] qy = new long[1];
291             qy[0] = flag;//设置压缩的比例1-100
292             EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
293             ep.Param[0] = eParam;
294 
295             try
296             {
297                 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
298                 ImageCodecInfo jpegICIinfo = null;
299                 for (int x = 0; x < arrayICI.Length; x++)
300                 {
301                     if (arrayICI[x].FormatDescription.Equals("JPEG"))
302                     {
303                         jpegICIinfo = arrayICI[x];
304                         break;
305                     }
306                 }
307 
308                 MemoryStream ms = new MemoryStream();
309 
310 
311                 if (jpegICIinfo != null)
312                 {
313 
314                     imgPhoto.Save(ms, jpegICIinfo, ep);//dFile是压缩后的新路径
315                 }
316                 else
317                 {
318                     imgPhoto.Save(ms, imgPhoto.RawFormat);
319                 }
320 
321                 System.Drawing.Image zipImage = System.Drawing.Image.FromStream(ms);
322                 return zipImage;
323             }
324             catch
325             {
326                 return null;
327             }
328         }
329 
330 
331     }
332 }

帮我看看代码哪里出了问题??

fun5的主页 fun5 | 初学一级 | 园豆:4
提问于:2012-06-19 21:47
< >
分享
所有回答(2)
0
public bool ThumbnailCallback()
 {
 return false;
 }
 public void Example_GetThumb(PaintEventArgs e)
 {
 Image.GetThumbnailImageAbort myCallback =
 new Image.GetThumbnailImageAbort(ThumbnailCallback);
 Bitmap myBitmap = new Bitmap("Climber.jpg");
 Image myThumbnail = myBitmap.GetThumbnailImage(
 40, 40, myCallback, IntPtr.Zero);
 e.Graphics.DrawImage(myThumbnail, 150, 75);
 }
Angkor--:-- | 园豆:1082 (小虾三级) | 2012-06-20 07:49
0

果断给你粘个方法过来看看能不能帮到你,

#region  生成缩略图
///<summary>
       
/// 生成缩略图
       
///</summary>
       
///<param name="originalImagePath">源图路径(物理路径)</param>
       
///<param name="thumbnailPath">缩略图路径(物理路径)</param>
       
///<param name="width">缩略图宽度</param>
       
///<param name="height">缩略图高度</param>
       
///<param name="mode">生成缩略图的方式</param>
        public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, string type)
        {

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
                case "HW"://指定高宽缩放(可能变形)                
                    break;
                case "W"://指定宽,高按比例                    
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case "H"://指定高,宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case "Cut"://指定高宽裁减(不变形)                
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }

            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

            //新建一个画板
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);

            try
            {
                System.Drawing.Imaging.ImageFormat imgtype = System.Drawing.Imaging.ImageFormat.Jpeg;
                if (type == "png")
                {
                    imgtype = System.Drawing.Imaging.ImageFormat.Png;
                }
                if (type == "jpg")
                {
                    imgtype = System.Drawing.Imaging.ImageFormat.Jpeg;
                }
                //保存缩略图
                bitmap.Save(thumbnailPath, imgtype);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }
        #endregion

 

 

我写的一个修改图片大小的小工具,原文地址给你

http://www.cnblogs.com/mcc7/archive/2011/11/22/2259322.html

澈澈 | 园豆:435 (菜鸟二级) | 2012-06-20 12:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册