Image Compressor Using Windows Forms With C#

In this article, I have explained how to compress images by reducing the quality of images using C#.

Here, I have used a Windows.Forms application to demonstrate how to reduce the size of an image by reducing the quality.

  1. In this example first, let's create a Windows Forms Project Template from the "Project Templates" section, as shown below.

    Go to File -> New -> Project and follow the screen below.

    Image Compressor Using Windows Forms With C#
  1. Next, design the form with two textboxes for reading the source path and destination path. Add three buttons – two buttons to browse the source and destination folders respectively and one button to compress the images. Add one Combobox to load some compress values. The design can be seen below.

    Image Compressor Using Windows Forms With C#
  1. After designing, you need to load the Compress Quality combobox data; for that, write the following code in the Form load event.
    1. private void Form1_Load(object sender, EventArgs e)  
    2. {  
    3.     for (int i = 10; i <= 100; i = i + 10)  
    4.     {  
    5.         cmbQuality.Items.Add(i);                 
    6.     }  
    7.     cmbQuality.SelectedIndex = 4;             
    8. }  
  1. Next, let’s write the Browse functionality for both source and destination folders. The code for Browse control is shown below.

    Let’s create a common function which can load source or destination textboxes according to the browse button clicks and call that form "Search" buttons.
    1. private void OpenFolderDialog(TextBox Filepath)  
    2. {  
    3.     FolderBrowserDialog folderDlg = new FolderBrowserDialog();  
    4.     folderDlg.ShowNewFolderButton = true;  
    5.    
    6.     DialogResult result = folderDlg.ShowDialog();  
    7.     if (result == DialogResult.OK)  
    8.     {  
    9.         Filepath.Text = folderDlg.SelectedPath;  
    10.         Environment.SpecialFolder root = folderDlg.RootFolder;  
    11.     }  
    12. }  
    13.   
    14. private void btnSouceBrowse_Click(object sender, EventArgs e)  
    15. {  
    16.     OpenFolderDialog(txtSource);  
    17. }  
    18.   
    19. private void btnDestFolder_Click(object sender, EventArgs e)  
    20. {  
    21.     OpenFolderDialog(txtDestination);  
    22. }  
  1. Now, let’s write a function to compress the image.
    1. using System.Drawing;  
    2. using System.Drawing.Imaging;  
    3. public static void CompressImage(string SoucePath, string DestPath, int quality)  
    4. {  
    5.     var FileName = Path.GetFileName(SoucePath);  
    6.     DestPath = DestPath + "\\" + FileName;  
    7.   
    8.     using (Bitmap bmp1 = new Bitmap(SoucePath))  
    9.     {  
    10.         ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);  
    11.    
    12.         System.Drawing.Imaging.Encoder QualityEncoder= System.Drawing.Imaging.Encoder.Quality;  
    13.    
    14.         EncoderParameters myEncoderParameters = new EncoderParameters(1);  
    15.    
    16.         EncoderParameter myEncoderParameter = new EncoderParameter(QualityEncoder, quality);  
    17.    
    18.         myEncoderParameters.Param[0] = myEncoderParameter;  
    19.         bmp1.Save(DestPath, jpgEncoder, myEncoderParameters);   
    20.          
    21.     }  
    22. }  
    23.   
    24. private static ImageCodecInfo GetEncoder(ImageFormat format)  
    25. {  
    26.     ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();  
    27.     foreach (ImageCodecInfo codec in codecs)  
    28.     {  
    29.         if (codec.FormatID == format.Guid)  
    30.         {  
    31.             return codec;  
    32.         }  
    33.     }  
    34.     return null;  
    35. }  
    In the above function, CompressImage, I am passing the source path, destination path, and quality number. Then, I am taking only the filename with extension from source path and concatenating that with destination path. After that, I am reading and modifying the source file into the destination path using bitmap.

    First, create an ImageCodecInfo passing jpeg image format, create quality encoder (QualityEncoder), and create one encoder parameter. Then, pass the quality encoder(QualityEncoder) which we have created already.

    Here, pass the parameter value as Quality no. which is passed to this function. After creating the parameter, save the bitmap image by passing the destination path, imageCodacInfo (jpgEncoder), and Encoder parameter.
  1. Now, we can use our CompressImage function to compress the images by passing the source image path.

    So, let’s add the functionality for the Compress button, as shown below.
    1. private void btnCompress_Click(object sender, EventArgs e)  
    2. {  
    3.          
    4.     string[] files = Directory.GetFiles(txtSource.Text);  
    5.             
    6.     foreach (var file in files)  
    7.     {  
    8.         string ext = Path.GetExtension(file).ToUpper();  
    9.         if (ext == ".PNG"|| ext == ".JPG")  
    10.             CompressImage(file, txtDestination.Text, (int)cmbQuality.SelectedItem);  
    11.     }  
    12.    
    13.     MessageBox.Show("Compressed Images has been stored to\n"+ txtDestination.Text);  
    14.     txtDestination.Text = "";  
    15.     txtSource.Text = "";   
    16. }  

In this click event, first, collect all the file paths from Source folder path using the Directory.GetFiles method and store to string array. Then, loop all the files and check the extensions if those are matched. Then, call the CompressImage function by passing the file path, destination folder path, and image quality from Combo Box.

Now, you can see the below output of source files and destination files with their respective sizes.

Image Compressor Using Windows Forms With C#