Thursday, August 20, 2009

How to compress, resize image in .net (C#, VB, Asp.net)

You may want to compress image and save to a new file or compress them while loading any big images in your asp.net application.

Here you go:

Asp.net with C#

//Suppose we get image from query string
string strImgUrl = Request.QueryString["imgUrl"];
//Load image
System.Drawing.Image imgFullSize = System.Drawing.Image.FromFile(strImgUrl);
//Set return image type
Response.ContentType = "image/gif";
//Create Encoder Parameter and specify the quality
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality,50L);
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;

//Resize image if you want
//In this case, if the image width greater than 1800, do resize width and height
//int intWidth, intHeight;
//intWidth = imgFullSize.Width;
//intHeight = imgFullSize.Height;
//if (imgFullSize.Width > 1800)
//{
//intWidth = 1800;
//intHeight = (int)(((float)intWidth / imgFullSize.Width) * imgFullSize.Height);
//}

//System.Drawing.Image imgThumbNail = imgFullSize.GetThumbnailImage(intWidth, //intHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), //IntPtr.Zero);
//imgThumbNail.Save(Response.OutputStream, jpegCodec, encoderParams);
//imgThumbNail.Dispose();

//Return image
imgFullSize.Save(Response.OutputStream, jpegCodec, encoderParams);



C#

private void VaryQualityLevel()
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");

//Or you do can use buil-in method
//ImageCodecInfo jgpEncoder GetEncoderInfo("image/gif");//"image/jpeg",...
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;

// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);

myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);

// Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);

}


...


private ImageCodecInfo GetEncoder(ImageFormat format)
{

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}



VB.net


Private Sub VaryQualityLevel()
' Get a bitmap.
Dim bmp1 As New Bitmap("c:\TestPhoto.jpg")

//Or you can use build-in method
//Dim jgpEncoder As ImageCodecInfo = GetEncoderInfo("image/jpeg");
Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)

' Create an Encoder object based on the GUID
' for the Quality parameter category.
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality

' Create an EncoderParameters object.
' An EncoderParameters object has an array of EncoderParameter
' objects. In this case, there is only one
' EncoderParameter object in the array.
Dim myEncoderParameters As New EncoderParameters(1)

Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters)

myEncoderParameter = New EncoderParameter(myEncoder, 100&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters)

' Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = New EncoderParameter(myEncoder, 0&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters)

End Sub 'VaryQualityLevel



...


Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo

Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()

Dim codec As ImageCodecInfo
For Each codec In codecs
If codec.FormatID = format.Guid Then
Return codec
End If
Next codec
Return Nothing

End Function

6 comments:

dd said...

Thanks, awesome post, saved me a lot of time :)

Unknown said...

Good article!!

Will said...

Excellent! Many thanks, this is very useful.

Anonymous said...

Juste a msdn copy and paste...

http://msdn.microsoft.com/fr-fr/library/bb882583.aspx

Anonymous said...

Very good! Thanks!!!

Deva said...

Excellent! Many thanks, this is very useful.