Search
 
SCRIPT & CODE EXAMPLE
 

C

how to compress image in c

public static void CompressImage(string sourcepath, string destpath, int quality)
{
    var FileName = Path.GetFileName(sourcepath);
    destpath = destpath + "" + FileName;
    using (Bitmap bmpl = new Bitmap(sourcepath))
    {
        ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
        System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality;
        EncoderParameters myEncoderParameters = new EncoderParameters(1);
        EncoderParameter myEncoderParameter = new EncoderParameter(QualityEncoder, quality);
        myEncoderParameters.Param[0] = myEncoderParameter;
        bmpl.Save(destpath, jpgEncoder, myEncoderParameters);
    }
}

private static ImageCodecInfo GetEncoder(ImageFormat format)
{
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
    foreach(ImageCodecInfo codec in codecs)
    {
        if(codec.FormatID==format.Guid)
        {
            return codec;
        }
    }
    return null;
}

private void button3_Click(object sender, EventArgs e)
{
    string[] files = Directory.GetFiles(textBox1.Text);
    DialogResult result2 = folderBrowserDialog1.ShowDialog();
    if (result2 == DialogResult.OK)
    {
        foreach (var file in files)
        {
            string ext = Path.GetExtension(file).ToUpper();
            if (ext == ".PNG" || ext == ".JPG")
            {
                CompressImage(file, folderBrowserDialog1.SelectedPath, (int)comboBox1.SelectedItem);
            }
            else
            {
                MessageBox.Show("The selected file: " + textBox1.Text + " does not contain no imege.", "Compress Unsuccessfull!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                ClassGV.image = 1;
                break;
            }
        }
        if (ClassGV.image != 1)
        {
            MessageBox.Show("Compressed images has been stored to
" + folderBrowserDialog1.SelectedPath, "Compress Successfull!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
C :: print octal in c 
C :: debian9 remove pack 
C :: Defining a macro in a header file 
C :: function of science writing of a number 
C :: sdl close ev 
C :: finding average of elements in array using struct in C? 
C :: take array input from user and calc the avr in c 
C :: i765 OPT filing fees october 2 
C :: pointers c 
C :: perfect numbers in c 
C :: merge sort in c 
C :: free array in c 
C :: Recommended compiler and linker flags for GCC 
Dart :: flutter listtile shape border 
Dart :: textfield border radius flutter 
Dart :: flutter clear navigation stack 
Dart :: rounded borders for container in flutte 
Dart :: order list dart 
Dart :: flutter remove status bar 
Dart :: flutter showsnackbar 
Dart :: flutter analyze apk size 
Dart :: floating action button rectangle flutter 
Dart :: flutter url image 
Dart :: dart list to json 
Dart :: flutter column mainaxissize 
Dart :: floting action button small size 
Dart :: flutter safearea 
Dart :: dart write to file 
Dart :: Flutter Popup Menu Button Example Tutorial 
Dart :: what will do for each in dart 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =