码迷,mamicode.com
首页 > Windows程序 > 详细

C#实现略缩图

时间:2014-10-23 12:11:38      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   文件   


 public class GenerateThumbnail
    {
        private Image imageFrom;
        /// <summary>
        /// 源图的路径(含文件名及扩展名
        /// </summary>
        /// <param name="pathImageFrom">源图的路径(含文件名及扩展名</param>
        public GenerateThumbnail(string pathImageFrom)
        {
            imageFrom = Image.FromFile(pathImageFrom);
        }
        /// <summary>
        /// 生成缩略图 静态方法 
        /// </summary>
        /// <param name="pathImageTo">生成的缩略图所保存的路径(含文件名及扩展名)注意:扩展名一定要与生成的缩略图格式相对应</param>
        /// <param name="Percent">比例 例如 0.8...</param>
        public void GenThumbnail(string pathImageTo, double Percent)
        {
            GenThumbnail(pathImageTo, Convert.ToInt32(imageFrom.Width * Percent), Convert.ToInt32(imageFrom.Height * Percent));
        }
        /**/
        /// <summary> 
        ///  生成缩略图 静态方法    
        /// </summary> 
        /// <param name="pathImageTo"> 生成的缩略图所保存的路径(含文件名及扩展名) 
        ///                            注意:扩展名一定要与生成的缩略图格式相对应 </param> 
        /// <param name="width"> 欲生成的缩略图 "画布" 的宽度(像素值) </param> 
        /// <param name="height"> 欲生成的缩略图 "画布" 的高度(像素值) </param> 
        public void GenThumbnail(string pathImageTo, int width, int height)
        {
            Bitmap bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.DrawImage(imageFrom, new Rectangle(0, 0, width, height), new Rectangle(0, 0, imageFrom.Width, imageFrom.Height), GraphicsUnit.Pixel);
            try
            {
                if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
                    bmp.Save(pathImageTo, ImageFormat.Jpeg);
                else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
                    bmp.Save(pathImageTo, ImageFormat.Png);
                else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
                    bmp.Save(pathImageTo, ImageFormat.Gif);
                else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
                    bmp.Save(pathImageTo, ImageFormat.Icon);
                else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
                    bmp.Save(pathImageTo, ImageFormat.Tiff);
                else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
                    bmp.Save(pathImageTo, ImageFormat.Wmf);
                else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
                    bmp.Save(pathImageTo, ImageFormat.Bmp);
                else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf))
                    bmp.Save(pathImageTo, ImageFormat.Emf);
                else if (imageFrom.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif))
                    bmp.Save(pathImageTo, ImageFormat.Exif);
                else
                    throw new Exception("无此类型图片");
            }
            finally
            {
                //显示释放资源 
                imageFrom.Dispose();
                bmp.Dispose();
                g.Dispose();
            }
        }
    }

 

C#实现略缩图

标签:style   blog   color   io   os   ar   for   sp   文件   

原文地址:http://www.cnblogs.com/yuming1983/p/4045342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!