0

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 function GenerateThumbnails reduces the actual size of the picture but it is very low. Suppose I upload the image which has a size of 800kb, the function will reduce only 40kb from 800kb. The size of the image affecting the performance of my website.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
Rajeev
  • 5
  • 2
  • 8

2 Answers2

2

You can also use the Thumbnailimagemethod... it seem to be exactly what you want. (I dont tested the compression, but it definatly compreses the image)

Image FinalImage = Image.FromFile(@"yourPath").GetThumbnailImage(X, Y, () => false, IntPtr.Zero);

Also you may use a lower InterpolationMode, CompositingQuality, and SmoothingMode.

BudBrot
  • 1,341
  • 2
  • 24
  • 44
0

Reducing the Resolution of the Image can result in producing low size Images,

Change the scaleFactor value < 0.5, in your existing code.

var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
Arun Selva Kumar
  • 2,593
  • 3
  • 19
  • 30