Reduce The Quality of Image in C#

Introduction

We have used Microsoft Paint so many times to reduce the quality of HQ images. Either it may be of any digital camera or any DSLR. We ususally don't share a high-quality image on Facebook or any social media. We will now create a C# application to reduce the quality of images.

Image1.jpg

Background

In the background, we will use a .NET class, dialog boxes, text boxes, progress bars, numeric up down and defiantly, buttons. In addition, you may use some icons and header images.

User Interface

Image2.jpg

List of Controls

  • Check Box List
  • 3 Buttons
  • 2 Picture box
  • 1 Numeric Up Down
  • 1 Progress Bar
  • 2 Group Box

C# Back End Code

Step 1

We will first code the Add Button (+) on that given snapshot.

Note: It will pop up the "Folder Dialogue ", from where we can select the image's folder.

So, in the Add Button's Event, we will write::

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
// Execute when OK is Pressed in Dialogue Box.
{
    checkedListBox1.Items.Clear();
    // CLEAR THE SELECTED ITEMS
    foreach (string fileName in Directory.GetFiles(folderBrowserDialog1.SelectedPath))
    {
        // SHOW ALL THE FILES IN THAT SELECTED FOLDER
        FileInfo fiInfo = new FileInfo(fileName);
        // Object of FileInfo class, as it determines every details of Image.
        if (fiInfo.Extension.ToLower() == ".jpg" || fiInfo.Extension.ToLower() == ".jpeg")
            // Execute only for JPEG Images
            checkedListBox1.Items.Add(fileName);
    }
}
progressBar1.Maximum = checkedListBox1.Items.Count;  
// Set the Maximum value for Progress Bar
}

Step 2

When the user selects an item in the Check Box list, it will raise the event CheckBoxList1_SlectedItemIndexChange.

When we select an item then a Picture Box will show that specific Image. 

Try // If any exception occurs

{

    pictureBox2.ImageLocation = checkedListBox1.SelectedItem.ToString();

    // Let picture Box name is pictureBox2

}

catch

{

    // Nothing happen

}

Step 3

When the Remove Button (X) is clicked, then it will remove that specific image.

For that:

checkedListBox1.Items.Remove(checkedListBox1.SelectedItem);
// Remove the selected Item in List
   pictureBox2.ImageLocation = "";
//And set the image Location to NULL

Step 4

Finally, when you click the Check/OK button then it does the main code, that changes the quality of the image. So, in the Save Button Event:

progressBar1.Value = 0;

if (folderBrowserDialog2.ShowDialog()== DialogResult.OK)
// SaveFile Dialog Poped, Where You Choose the Destination Folder
    ImageCodecInfo imgCoder = null
    //Reference Variable of Codec Info of Image
    EncoderParameter epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,(int)numericUpDown1.Value);
    // Paramter List Of Codec List (in 2nd Argument, value of Aspect is ed)
    ImageCodecInfo[] imgCodec = ImageCodecInfo.GetImageEncoders();
    // Array of Codec Info
    EncoderParameters encdrParameters = new EncoderParameters(1);
    encdrParameters.Param[0] = epQuality;
    for (int i = 0; i < imgCodec.Length; i++)
    {
        if (imgCodec[i].MimeType == "image/jpeg")
        {
            imgCoder = imgCodec[i];
            break;
        }
    }
    foreach(string strFile in checkedListBox1.CheckedItems)
    {
        // Only Checked Pictures Will be Handled
        progressBar1.PerformStep(); // Increase Progress Bar value
        Image newImage = Image.FromFile(strFile);
        FileInfo filePicture = new FileInfo(strFile);
        // Save To its Destination
        newImage.Save(folderBrowserDialog2.SelectedPath+"\\"+filePicture.Name,imgCoder,encdrParameters) 
        // Dispose from Memory
        newImage.Dispose();
    }
}
// Finally, Poped the saved Location Windows, You may choose any Other Option
MessageBox.Show("Successully Done !!");
System.Diagnostics.Process.Start(folderBrowserDialog2.SelectedPath.ToString());


Similar Articles