剪裁图片.cs

csharp

浏览数:169

2019-1-7

AD:资源代下载服务


剪裁图片.cs

        //imgSrc 原图片路径
        //savePath 保存剪裁后的图片路径
        private void MakeThumbnailImage(string imgSrc, string savePath, string fileSuffix, int width, int height, string model)
        {
            //load img
            Image original = Image.FromFile(imgSrc);
            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int imageWidth = original.Width;
            int imageHeight = original.Height;
            switch (model)
            {
                case "HW": //指定宽高缩放(补白)
                    if (((double)original.Width / (double)original.Height) > ((double)towidth / (double)toheight))
                    {
                        imageWidth = original.Width;
                        imageHeight = original.Width * height / towidth;
                        x = 0;
                        y = (original.Height - imageHeight) / 2;
                    }
                    else
                    {
                        imageHeight = original.Height;
                        imageWidth = original.Height * towidth / toheight;
                        y = 0;
                        x = (original.Width - imageWidth) / 2;
                    }
                    break;
                case "W"://指定宽高比例
                    toheight = original.Height * width / original.Width;
                    break;
                case "H"://指定高宽比例
                    towidth = original.Width * height / original.Height;
                    break;
                case "Cut"://指定高宽裁剪(不变形)
                    if (((double)original.Width / (double)original.Height) > ((double)towidth / (double)toheight))
                    {
                        imageHeight = original.Height;
                        imageWidth = original.Height * towidth / toheight;
                        y = 0;
                        x = (original.Width - imageWidth) / 2;
                    }
                    else
                    {
                        imageWidth = original.Width;
                        imageHeight = original.Width * height / towidth;
                        x = 0;
                        y = (original.Height - imageHeight) / 2;
                    }
                    break;
                default:
                    break;
            }

            //创建图片
            Bitmap b = new Bitmap(towidth, toheight);
            using (Graphics g = Graphics.FromImage(b))  //在图片上创建画板
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;     //高质量插值算法
                g.SmoothingMode = SmoothingMode.AntiAlias;      //抗锯齿
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.Clear(Color.White);   //清空画板 白色填充
                g.DrawImage(original, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, imageWidth, imageHeight), GraphicsUnit.Pixel);
                //save
                SaveImage(b, savePath, GetCodecInfo("image/" + GetFormat(fileSuffix).ToString().ToLower()));
            }
            b.Dispose();    //释放资源
        }

        private void SaveImage(Image img, string savePath, ImageCodecInfo ici)
        {
            //设置 原图 对象的 EncoderParameters
            using (EncoderParameters parameters = new EncoderParameters())
            {
                parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)100));
                img.Save(savePath, ici, parameters);
            }
        }

        private ImageCodecInfo GetCodecInfo(string mimeType)
        {
            ImageCodecInfo[] codecInfo = ImageCodecInfo.GetImageEncoders();
            foreach (var item in codecInfo)
            {
                if (item.MimeType == mimeType)
                    return item;
            }
            return null;
        }

        private ImageFormat GetFormat(string fileSuffix)
        {
            switch (fileSuffix.ToLower())
            {
                case "jpg":
                case "jpeg":
                    return ImageFormat.Jpeg;
                case "bmp":
                    return ImageFormat.Bmp;
                case "png":
                    return ImageFormat.Png;
                case "gif":
                    return ImageFormat.Gif;
                case "emf":
                    return ImageFormat.Emf;
                case "ico":
                    return ImageFormat.Icon;
                case "exif":
                    return ImageFormat.Exif;
                case "tiff":
                    return ImageFormat.Tiff;
                case "wmf":
                    return ImageFormat.Wmf;
                default:
                    return ImageFormat.Jpeg;
            }
        }