C# 图片按比例进行压缩

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

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

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

cs 复制代码
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();
        }
cs 复制代码
 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类来压缩数据基本上没有效果。

cs 复制代码
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);
        }
相关推荐
mudtools17 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫20 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机