Posted By

mattbrailsford on 10/01/10


Tagged

resize image


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Image resizing


 / Published in: C#
 

  1. public static Bitmap ResampleImage(Image image, int width, int height, int destX, int destY, int destWidth, int destHeight)
  2. {
  3. var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
  4. bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
  5.  
  6. using (var graphics = Graphics.FromImage(bitmap))
  7. {
  8. graphics.Clear(Color.Transparent);
  9.  
  10. graphics.CompositingQuality = CompositingQuality.HighQuality;
  11. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  12. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  13. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  14.  
  15. graphics.DrawImage(image,
  16. new Rectangle(destX, destY, destWidth, destHeight),
  17. new Rectangle(1, 1, image.Width - 2, image.Height - 2),
  18. GraphicsUnit.Pixel);
  19. }
  20.  
  21. return bitmap;
  22. }
  23. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: sebastiaan on October 1, 2010

Maybe something like this will help

var parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);

ImageCodecInfo imageCodec = null; foreach (var codec in imageCodecs.Where(codec => codec.MimeType == "image/jpeg")) imageCodec = codec;

bitmap.Save(stream, imageCodec, parameters);

You can then set the response to image/jpeg and output the stream.

You need to login to post a comment.