C#实现无损压缩图片代码示例

作者:農碼一生 时间:2022-01-23 19:59:25 

一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的。尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,影响用户体验,所以一般会将图片进行压缩。

代码实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageCompress
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }
       /// <summary>
       /// 无损压缩图片
       /// </summary>
       /// <param name="sFile">原图片地址(包含图片名称)</param>
       /// <param name="dFile">压缩后保存图片地址(包含图片名称)</param>
       /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
       /// <param name="size">压缩后图片的最大大小</param>
       /// <param name="sfsc">是否是第一次调用</param>
       /// <returns></returns>
       public static bool CompressImage(string sFile, string dFile, int flag = 90, int size = 300, bool sfsc = true)
       {
           //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
           FileInfo firstFileInfo = new FileInfo(sFile);
           if (sfsc == true && firstFileInfo.Length < size * 1024)
           {
               firstFileInfo.CopyTo(dFile);
               return true;
           }
           Image iSource = Image.FromFile(sFile);
           ImageFormat tFormat = iSource.RawFormat;
           int dHeight = iSource.Height / 2;
           int dWidth = iSource.Width / 2;
           int sW = 0, sH = 0;
           //按比例缩放
           Size tem_size = new Size(iSource.Width, iSource.Height);
           if (tem_size.Width > dHeight || tem_size.Width > dWidth)
           {
               if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
               {
                   sW = dWidth;
                   sH = (dWidth * tem_size.Height) / tem_size.Width;
               }
               else
               {
                   sH = dHeight;
                   sW = (tem_size.Width * dHeight) / tem_size.Height;
               }
           }
           else
           {
               sW = tem_size.Width;
               sH = tem_size.Height;
           }

Bitmap ob = new Bitmap(dWidth, dHeight);
           Graphics g = Graphics.FromImage(ob);

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

g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);

g.Dispose();

//以下代码为保存图片时,设置压缩质量
           EncoderParameters ep = new EncoderParameters();
           long[] qy = new long[1];
           qy[0] = flag;//设置压缩的比例1-100
           EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
           ep.Param[0] = eParam;

try
           {
               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)
               {
                   ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
                   FileInfo fi = new FileInfo(dFile);
                   if (fi.Length > 1024 * size)
                   {
                       flag = flag - 10;
                       CompressImage(sFile, dFile, flag, size, false);
                   }
               }
               else
               {
                   ob.Save(dFile, tFormat);
               }
               return true;
           }
           catch
           {
               return false;
           }
           finally
           {
               iSource.Dispose();
               ob.Dispose();
           }
       }
       private void button1_Click(object sender, EventArgs e)
       {
           string sfile = @"C:\Users\Mingliang_wang\Pictures\Screenshots\s_test.jpg";
           string dfile = @"C:\Users\Mingliang_wang\Pictures\Screenshots\d_test.jpg";
           CompressImage(sfile,dfile);
       }
   }
}

来源:https://www.cnblogs.com/wml-it/p/14818161.html

标签:C#,无损,压缩,图片
0
投稿

猜你喜欢

  • C#控件picturebox实现图像拖拽和缩放

    2023-08-09 08:23:05
  • C# winform循环播放多个视频

    2021-06-30 06:39:21
  • Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据详解

    2023-04-09 22:36:45
  • Java 添加、替换、删除PDF中的图片的示例代码

    2023-08-28 09:06:26
  • 简述Java中的四种引用类型

    2023-11-29 00:46:56
  • java Apache poi 对word doc文件进行读写操作

    2023-09-23 02:13:15
  • 详解Java中对象池的介绍与使用

    2023-07-25 13:13:48
  • linphone-sdk-android版本号生成解析

    2022-10-18 14:43:18
  • zookeeper概述图文详解

    2021-12-07 15:46:03
  • java连接SQL Server数据库的超详细教程

    2023-04-05 21:46:22
  • 基于java中的流程控制语句总结(必看篇)

    2023-11-08 09:56:59
  • 使用HTTPclient保持长连接

    2023-10-17 12:29:34
  • Java并发编程之Exchanger方法详解

    2022-08-22 02:44:41
  • C#生成带注释的dll并引用实现

    2023-08-31 16:01:09
  • 使用jdk1.8实现将list根据指定的值去分组的操作

    2022-10-04 18:12:48
  • 详解如何热更新线上的Java服务器代码

    2021-05-26 01:29:33
  • 通过Feign进行调用@FeignClient 找不到的解决方案

    2023-08-18 19:06:52
  • 基于Java生成GUID的实现方法

    2022-04-09 02:44:09
  • 如何解决springboot读取配置文件的中文乱码问题

    2022-09-13 22:26:42
  • C#中将xml文件反序列化为实例时采用基类还是派生类的知识点讨论

    2022-08-04 19:27:32
  • asp之家 软件编程 m.aspxhome.com