SoFunction
Updated on 2025-03-07

C# implements lossless compression picture code example

Generally, in web applications, the images submitted by the client must be compressed. Especially for larger images, if not compressed, the page will become large and the opening speed will be slower, which will affect the user experience, so the image will generally be compressed.

Code implementation:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

namespace ImageCompress
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Lossless compression pictures        /// </summary>
        /// <param name="sFile">Original image address (including image name)</param>        /// <param name="dFile">Save the image address after compression (including the image name)</param>        /// <param name="flag">Compression quality (the smaller the number, the higher the compression rate) 1-100</param>        /// <param name="size">Maximum size of the compressed image</param>        /// <param name="sfsc">Is it the first call</param>        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool CompressImage(string sFile, string dFile, int flag = 90, int size = 300, bool sfsc = true)
        {
            //If it is the first call and the size of the original image is smaller than the size to be compressed, the file will be copied directly and true will be returned            FileInfo firstFileInfo = new FileInfo(sFile);
            if (sfsc == true &amp;&amp;  &lt; size * 1024)
            {
                (dFile);
                return true;
            }
            Image iSource = (sFile);
            ImageFormat tFormat = ;
            int dHeight =  / 2;
            int dWidth =  / 2;
            int sW = 0, sH = 0;
            //Scaling to scale            Size tem_size = new Size(, );
            if (tem_size.Width &gt; dHeight || tem_size.Width &gt; dWidth)
            {
                if ((tem_size.Width * dHeight) &gt; (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 = (ob);

            ();
             = .;
             = .;
             = .;

            (iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, , , );

            ();

            //The following code sets the compression quality when saving the picture            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//Set the compression ratio 1-100            EncoderParameter eParam = new EncoderParameter(, qy);
            [0] = eParam;

            try
            {
                ImageCodecInfo[] arrayICI = ();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x &lt; ; x++)
                {
                    if (arrayICI[x].("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                if (jpegICIinfo != null)
                {
                    (dFile, jpegICIinfo, ep);//dFile is the compressed new path                    FileInfo fi = new FileInfo(dFile);
                    if ( &gt; 1024 * size)
                    {
                        flag = flag - 10;
                        CompressImage(sFile, dFile, flag, size, false);
                    }
                }
                else
                {
                    (dFile, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                ();
                ();
            }
        }
        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);
        }
    }
}

This is all about this article about C# implementing lossless compressed images. I hope it will be helpful to everyone's learning and I hope everyone will support me more.