SoFunction
Updated on 2025-03-07

C# implements compressed images into controllable JPEG format

1. In C#, you can use the Bitmap class to read and process image files

To save as a JPEG picture that can be controlled by yourself, you can use Bitmap's Save method and set the JPEG's compression quality in it. Here is a simple example of how to save Bitmap as a JPEG image where compression quality can be customized:

Bitmap bitmap = new Bitmap("");
("", , 
new EncoderParameters(1, new EncoderParameter(, 50L)));

In this example, "" is the input image file name, and "" is the output image file name. It is a parameter of JPEG compression quality, and 50L means that the compression quality is 50. This value can be an integer between 0 and 100, 0 means the highest mass and 100 means the lowest mass. Note that this example uses the EncoderParameters class to set the compression quality of the JPEG. EncoderParameters is an enumeration class that contains one or more EncoderParameter objects. The EncoderParameter object is used to set specific encoding parameters. In this example, we set up an EncoderParameter object with the Encoder parameter of 50L.

#Image processing: Save as a JPEG that can be controlled by yourself by yourself

/** <summary>
        /// Use when saving JPG        /// </summary>
        /// <param name="mimeType"></param>
        /// <returns>Get ImageCodecInfo for the specified mimeType</returns>        private static ImageCodecInfo GetCodecInfo(string mimeType)
        {
            ImageCodecInfo[] CodecInfo = ();
            foreach (ImageCodecInfo ici in CodecInfo)
            {
                if ( == mimeType) return ici;
            }
            return null;
        }
        /** &lt;summary&gt;
        /// Save as JPEG format, supports compression quality options        /// &lt;/summary&gt;
        /// &lt;param name="bmp"&gt;&lt;/param&gt;
        /// &lt;param name="FileName"&gt;&lt;/param&gt;
        /// &lt;param name="Qty"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool KiSaveAsJPEG(Bitmap bmp, string FileName, int Qty)
        {
            try
            {
                EncoderParameter p;
                EncoderParameters ps;
                ps = new EncoderParameters(1);
                p = new EncoderParameter(, Qty);
                [0] = p;
                (FileName, GetCodecInfo("image/jpeg"), ps);
                
                return true;
            }
            catch
            {
                return false;
            }
        }

3. In C#, you can use the classes in the namespace and save as a JPEG image and control its compression quality.

However, the task of directly controlling the quality of JPEG compression is not directly supported in .NET because JPEG compression is done by libraries (such as libjpeg), and .NET does not provide an interface to directly operate these libraries.

However, you can use some third-party libraries like ImageSharp, which provides more advanced image processing capabilities, including JPEG compression.

First, you need to install the ImageSharp package. You can install it through the NuGet package manager:

Install-Package ImageSharp

You can then use the following code to save an image as JPEG while controlling its compression quality:

using ;  
using ImageSharp;  
using ;  
  
public void SaveImageWithQuality(Bitmap bmp, string path, int quality)  
{  
    using (var img = (bmp))  
    {  
        (x => x  
             .Format()  
             .Quality(quality)  
             .Save(path));  
    }  
}

In this code, Bitmap is the image you want to save, path is the path you want to save, quality is the quality of JPEG, with a range of 0-100 (100 means the highest quality, i.e. no compression). Note that ImageSharp's Quality setting does not directly correspond to the traditional JPEG compression quality setting, it is a more complex algorithm, but roughly, higher quality values ​​mean less compression and better image quality.

This is just a possible solution. In fact, for such a question, you may need to have a deeper understanding of JPEG compression, or look for a third-party library that can directly control the quality of JPEG compression.

This is the end of this article about C#’s implementation of compressed images in JPEG format that can be controlled. For more related C#’s compressed images, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!