Monday, February 7, 2011

Reduce image size in Asp.net

You can reduce the image size while uploading, this would be helpul when we have some open interfece for end user, but you want to reducde the size of image uploaded by user.

//this path will be saved in database
string PathToBeUploaded = objDirInfo.CreateSubdirectory(this.CurrentUserFolder) + "\\" + strFilename;

// read the file in a stream.
System.IO.Stream strm = imgBigProduct.PostedFile.InputStream;

// Now form an image out of stream
System.Drawing.Image imageOriginal = System.Drawing.Image.FromStream(strm);

// get the thumbnail size as you need
System.Drawing.Image imageReduced = imageOriginal.GetThumbnailImage(350, 350, ThumbnailCallback, IntPtr.Zero);

// Save the reduced image.
imageReduced.Save(PathToBeUploaded);

// this you may need to call while calling the GetThumbnailImage method.
public bool ThumbnailCallback()
{
return false;
}