Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear All,


I want to reduce the picture size less than 50 kb. Here i am giving the code which i have used in my application.

byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read(imageSize(int)FileUpload1.PostedFile.ContentLength);
var fileLength1 = (FileUpload1.FileContent.Length.ToString());
byte[] ImageData = GenerateThumbnails(0.005, uploadedImage.InputStream);
private byte[] GenerateThumbnails(double scaleFactor, Stream sourcePath)
{
      var image = System.Drawing.Image.FromStream(sourcePath);
      var newWidth = (int)(image.Width * scaleFactor);
      var newHeight = (int)(image.Height * scaleFactor);
      var thumbnailImg = new Bitmap(newWidth, newHeight);
      var thumbGraph = Graphics.FromImage(thumbnailImg);
      thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
      thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
      thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
      var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
      thumbGraph.DrawImage(image, imageRectangle);

      MemoryStream ms = new MemoryStream();
      image.Save(ms, ImageFormat.Jpeg);
      return ms.ToArray();
}


Here,the fumction GenerateThumbnails reduce the actual size of the picture but it is very low.Suppose i upload the image which has 800 kb ,function will reduce only 40 kb from 800 kb.Size of the image affectiong the perfomance of my website.So if anyone know the answer please help me.

Thanks in advance.

Regards,
Rajeev K.
Posted
Updated 25-Sep-13 2:27am
v2

1 solution

You, the GenerateThumbnails method sucks big time. Take a look at this excellent article : Thumbnails Generation[^]

It allows you to resize and scale images and allows you to have more control over the quality of the image being produced.

Cheers!
Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900