Resize the image

We can start with a byte array which contains the image data loaded from either a database or file. We can then resize the bitmap using a number of System.Drawing methods and finally save the bitmap to an output stream. In this case the output stream is the Response output stream



System.IO.MemoryStream memoryStream = new System.IO.MemoryStream( byteArray );
System.Drawing.Image image = System.Drawing.Image.FromStream( memoryStream );
System.Drawing.Image thumbnail = new Bitmap( newWidth, newHeight );
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage( thumbnail );

graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;

graphic.DrawImage(image, 0, 0, newWidth, newHeight);

if( contentType == "image/gif" )
{
using ( thumbnail )
{
OctreeQuantizer quantizer = new OctreeQuantizer ( 255 , 8 ) ;
using ( Bitmap quantized = quantizer.Quantize ( bitmap ) )
{
Response.ContentType = "image/gif";
quantized.Save ( Response.OutputStream , ImageFormat.Gif ) ;
}
}
}


if( contentType == "image/jpeg" )
{
info = ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
Response.ContentType = "image/jpeg";
thumbnail.Save(Response.OutputStream, info[1], encoderParameters);
}



By
Nathan


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: