C# 图片按比例进行压缩

1、对图片进行压缩,保存在本地

对于一个200k的png文件按0.6的缩放比例进行压缩,压缩后的大小为20k左右

对于一个80k的jpg文件按0.6的缩放比例压缩,压缩后为13k左右

public void imageZoom(string name, Double zoomScale)
        {
            Bitmap btImage = new Bitmap(name);
            Image serverImage = btImage;
            int width = (int)(serverImage.Width * zoomScale);
            int height = (int)(serverImage.Height * zoomScale);
            //画板大小
            int finalWidth = width, finalHeight = height;
            int srcImageWidth = serverImage.Width;
            int srcImageHeight = serverImage.Height;
            if (srcImageWidth > srcImageHeight)
            {
                finalHeight = srcImageHeight * width / srcImageWidth;
            }
            else
            {
                finalWidth = srcImageWidth * height / srcImageHeight;
            }


            //新建一个bmp图片
            Image newImage = new Bitmap(width, height);
            //新建一个画板
            Graphics g = Graphics.FromImage(newImage);
            //设置高质量插值法
            g.InterpolationMode = InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.White);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(serverImage, new Rectangle((width - finalWidth) / 2, (height - finalHeight) / 2, finalWidth, finalHeight), 0, 0, srcImageWidth, srcImageHeight, GraphicsUnit.Pixel);
            //以jpg格式保存缩略图
            MemoryStream msSaveImage = new MemoryStream();
            newImage.Save(@"D:\1.png",ImageFormat.Jpeg);
            serverImage.Dispose();
            newImage.Dispose();
            g.Dispose();
        }
 private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            imageZoom(ofd.FileName,0.6);
           
            
        }

2、对图片进行压缩,转换成Base64后进行传输,可测量字符串长度来对比

对一张图片转换成base64后,测量base64字符串的长度,对于一个80k的图片,压缩前长度为110800多,压缩后长度不到20000,减小了很多,便于传输。

网上有关于对字符串进行压缩的,那是针对纯字符串,对于已经转换成base64的字符串,使用GZipStream类来压缩数据基本上没有效果。

public  string ImageToBase64(string name,Double zoomScale)
        {

            Bitmap btImage = new Bitmap(name);
            Image serverImage = btImage;
            int width = (int)(serverImage.Width * zoomScale);
            int height = (int)(serverImage.Height * zoomScale);
            //画板大小
            int finalWidth = width, finalHeight = height;
            int srcImageWidth = serverImage.Width;
            int srcImageHeight = serverImage.Height;
            if (srcImageWidth > srcImageHeight)
            {
                finalHeight = srcImageHeight * width / srcImageWidth;
            }
            else
            {
                finalWidth = srcImageWidth * height / srcImageHeight;
            }
           

            //新建一个bmp图片
            Image newImage = new Bitmap(width, height);
            //新建一个画板
            Graphics g = Graphics.FromImage(newImage);
            //设置高质量插值法
            g.InterpolationMode = InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.White);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(serverImage, new Rectangle((width - finalWidth) / 2, (height - finalHeight) / 2, finalWidth, finalHeight), 0, 0, srcImageWidth, srcImageHeight, GraphicsUnit.Pixel);
            //以jpg格式保存缩略图
            MemoryStream msSaveImage = new MemoryStream();
            newImage.Save(msSaveImage, ImageFormat.Jpeg);
            serverImage.Dispose();
            newImage.Dispose();
            g.Dispose();
            byte[] imageBytes = msSaveImage.ToArray();
            msSaveImage.Close();
            return Convert.ToBase64String(imageBytes);
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值