Compress images with jpeg codec in C#

Today I’ll show you how to compress your images with help of the JPEG-codec in C#. You will be able to choose the grade of compression. So it’s your choice, if the function should produce smaller filesize or better quality of images.

private void CompressImage(Image sourceImage, int imageQuality, string savePath)
{
try
{
//Create an ImageCodecInfo-object for the codec information
ImageCodecInfo jpegCodec = null;

//Set quality factor for compression
EncoderParameter imageQualitysParameter = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, imageQuality);

//List all avaible codecs (system wide)
ImageCodecInfo[] alleCodecs = […]