不变形裁剪生成指定尺寸缩略图:

        #region Thumbs
        private static string GetMimeType(string imageExtension)
        {
            if (imageExtension.ToLower().Equals(".png"))
            {
                return "image/png";
            }
            else
            {
                return "image/jpeg";
            }
        }

        private static bool IsPng(string imageExtension)
        {
            return imageExtension.ToLower().Equals(".png");
        }

        private static ImageCodecInfo GetImageCodecInfo(string mimeType)
        {
            ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo ici in CodecInfo)
            {
                if (ici.MimeType == mimeType) return ici;
            }
            return null;
        }

        private static Bitmap GetBitmapFromStream(Stream inputStream)
        {
            Bitmap bitmap = new Bitmap(inputStream);
            return bitmap;
        }

        /// <summary>
        /// 图片压缩图片
        /// </summary>
        /// <param name="quality">图片压缩质量,越大照片越清晰,推荐80</param>
        private static void Compress(string imageExtension, Bitmap bmp, string saveFilePath, int quality)
        {
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            EncoderParameters eParams = new EncoderParameters(1);
            eParams.Param[0] = eParam;
            if (IsPng(imageExtension))
            {
                bmp.MakeTransparent(Color.Transparent);
            }
            bmp.Save(saveFilePath, GetImageCodecInfo(GetMimeType(imageExtension)), eParams);
            bmp.Dispose();
        }

        private static void Compress(string imageExtension, Stream inputStream, string saveFilePath, int quality)
        {
            Bitmap bmp = GetBitmapFromStream(inputStream);
            Compress(imageExtension, bmp, saveFilePath, quality);
        }

        private static void Compress(string saveFilePath, int width, int height, FileInfo fi, Image srcImage)
        {
            Bitmap destBitmap = new Bitmap(width, height, PixelFormat.Format64bppPArgb);
            Graphics destGraphics = Graphics.FromImage(destBitmap);
            //可以在这里设置填充背景颜色
            if (IsPng(fi.Extension))
            {
                destGraphics.Clear(Color.Transparent);
            }
            destGraphics.DrawImage(srcImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
            srcImage.Dispose();
            try
            {
                Compress(fi.Extension, destBitmap, saveFilePath, 100);
            }
            catch { }
            finally
            {
                destBitmap.Dispose();
                destGraphics.Dispose();
            }
        }

        /// <summary>
        /// 按指定尺寸压缩图片
        /// </summary>
        public static void Compress(string sourceFilePath, string saveFilePath, int width, int height)
        {
            FileInfo fi = new FileInfo(sourceFilePath);
            if (fi.Exists)
            {
                using (FileStream inputStream = fi.OpenRead())
                {
                    Image srcImage = Image.FromStream(inputStream);
                    if (srcImage.Width == width && srcImage.Height == height)
                    {
                        Compress(fi.Extension, inputStream, saveFilePath, 100);
                    }
                    else
                    {
                        //目标的宽高比例  
                        double destRate = (double)width / height;
                        //原图片的宽高比例  
                        double srcRate = (double)srcImage.Width / srcImage.Height;
                        //比例相当
                        if (destRate == srcRate)
                        {
                            //压缩缩略图
                            Compress(saveFilePath, width, height, fi, srcImage);
                        }
                        //原图与目标比例不等,裁剪后缩放  
                        else
                        {
                            //裁剪对象  
                            System.Drawing.Image cutImage = null;
                            System.Drawing.Graphics cutGraphics = null;
                            //定位  
                            Rectangle srcRect = new Rectangle(0, 0, 0, 0);//原图裁剪定位  
                            Rectangle destRect = new Rectangle(0, 0, 0, 0);//目标定位  

                            //宽为标准进行裁剪  
                            if (destRate > srcRate)
                            {
                                //裁剪对象实例化  
                                cutImage = new System.Drawing.Bitmap(srcImage.Width, (int)Math.Floor(srcImage.Width / destRate));
                                cutGraphics = System.Drawing.Graphics.FromImage(cutImage);

                                //裁剪源定位  
                                srcRect.X = 0;
                                srcRect.Y = (int)Math.Floor((srcImage.Height - srcImage.Width / destRate) / 2);
                                srcRect.Width = srcImage.Width;
                                srcRect.Height = (int)Math.Floor(srcImage.Width / destRate);

                                //裁剪目标定位  
                                destRect.X = 0;
                                destRect.Y = 0;
                                destRect.Width = srcImage.Width;
                                destRect.Height = (int)Math.Floor(srcImage.Width / destRate);
                            }
                            //高为标准进行裁剪  
                            else
                            {
                                cutImage = new System.Drawing.Bitmap((int)Math.Floor(srcImage.Height * destRate), srcImage.Height);
                                cutGraphics = System.Drawing.Graphics.FromImage(cutImage);

                                srcRect.X = (int)Math.Floor((srcImage.Width - srcImage.Height * destRate) / 2);
                                srcRect.Y = 0;
                                srcRect.Width = (int)Math.Floor(srcImage.Height * destRate);
                                srcRect.Height = srcImage.Height;

                                destRect.X = 0;
                                destRect.Y = 0;
                                destRect.Width = (int)Math.Floor(srcImage.Height * destRate);
                                destRect.Height = srcImage.Height;

                            }
                            //设置质量  
                            cutGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            cutGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                            //裁剪  
                            cutGraphics.DrawImage(srcImage, destRect, srcRect, System.Drawing.GraphicsUnit.Pixel);

                            //压缩缩略图
                            Compress(saveFilePath, width, height, fi, cutImage);
                        }
                    }


                }
            }
            fi = null;
        }
        #endregion
View Code

 

 

 posted on 2014-03-10 11:06  Lucien.Bao  阅读(307)  评论(0编辑  收藏  举报