c#实现图片按比例自动压缩

     /// <summary>
        /// 图片压缩
        /// </summary>
        /// <param name="sPath">图片绝对路径</param>
        /// <param name="quality">压缩质量(数字越小压缩率越高) 1-100</param>
        /// <param name="destWidth">目标宽度,若为0,表示宽度按比例缩放</param>
        /// <param name="destHeight">目标高度,若为0,表示高度按比例缩放</param>
        /// <param name="ratePic">图片大小缩放比例值(1-99),当dWidth和dHeight等于0时才有效</param>
        /// <returns>成功返回压缩图片路径,否则返回空</returns>
        public static bool GetPictureThumbnail(string sPath, int quality = 20, int destWidth = 0, int destHeight = 0, int ratePic = 0)
        {
            Image imageSource =Image.FromFile(sPath);
            ImageFormat tFormat = imageSource.RawFormat;   
            if (destWidth == 0)
            {
                destWidth = imageSource.Width;
                if(ratePic>0 && ratePic<100)
                {
                    destWidth = (int)(destWidth * ratePic*0.01);
                }
            }
            if (destHeight == 0)
            {
                destHeight = imageSource.Height;
                if (ratePic > 0 && ratePic < 100)
                {
                    destHeight = (int)(destHeight * ratePic * 0.01);
                }
            }          

            Bitmap ob = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage(ob);

            g.Clear(Color.WhiteSmoke);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imageSource, new Rectangle(0, 0, destWidth, destHeight), 0, 0, imageSource.Width, imageSource.Height, GraphicsUnit.Pixel);

            g.Dispose();
            //以下代码为保存图片时,设置压缩质量    
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = quality;//设置压缩的比例1-100    
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                //string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(sPath);
             
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                if (jpegICIinfo != null)
                {
                    imageSource.Dispose();
                    //File.Delete(sPath);
                    ob.Save(sPath, jpegICIinfo, ep); 
                }
                else
                {
                    imageSource.Dispose();
                    //File.Delete(sFile);
                    ob.Save(sPath, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                imageSource.Dispose();
                ob.Dispose();
            }
        }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的图片比例压缩的算法示例,基于C#语言实现: ```csharp using System.Drawing; using System.Drawing.Imaging; public static Image CompressImage(Image image, int maxWidth, int maxHeight) { int width = image.Width; int height = image.Height; if (width <= maxWidth && height <= maxHeight) { return image; } int newWidth, newHeight; if (width * maxHeight > height * maxWidth) { newWidth = maxWidth; newHeight = height * maxWidth / width; } else { newHeight = maxHeight; newWidth = width * maxHeight / height; } var destRect = new Rectangle(0, 0, newWidth, newHeight); var destImage = new Bitmap(newWidth, newHeight); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, width, height, GraphicsUnit.Pixel, wrapMode); } } return destImage; } ``` 使用方法: ```csharp Image originalImage = Image.FromFile("original.jpg"); Image compressedImage = CompressImage(originalImage, 800, 600); compressedImage.Save("compressed.jpg", ImageFormat.Jpeg); ``` 其中,`CompressImage`方法接收三个参数:原始图片、目标宽度和目标高度。如果原始图片的宽度和高度都小于等于目标宽度和目标高度,则直接返回原始图片。否则,根据原始图片的宽高比例,计算出新的宽度和高度,然后使用`Graphics`对象将原始图片缩放到目标大小,并返回缩放后的新图片。最后,将新图片保存在磁盘上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值