views:

987

answers:

1

I am using ASP.NET MVC and I've an action that uploads the file. The file is being uploaded properly. But I want width and height of the image. I think I need to convert the HttpPostedFileBase to Image first and then proceed. How do I do that?

And please let me know if there is another better way to get the width and height of the image.

+11  A: 

I use something like:

Image.FromStream(httpPostedFileBase.InputStream, true, true)

Note that the returned Image is IDisposable.

You'll need a reference to System.Drawing.dll for this to work, and Image is in the System.Drawing namespace.

Resizing the Image

I'm not sure what you're trying to do, but if you happen to be making thumbnails or something similar, you may be interested in doing something like...

try {
 bitmap = new Bitmap(newWidth,newHeight);
 using (Graphics g = Graphics.FromImage(bitmap))
 {
  g.SmoothingMode = SmoothingMode.HighQuality;
  g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  g.CompositingQuality = CompositingQuality.HighQuality;
  g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  g.DrawImage(oldImage,
   new Rectangle(0,0,newWidth,newHeight),
   clipRectangle, GraphicsUnit.Pixel);
 }//done with drawing on "g"
 return bitmap;//IDisposable
}
catch
{
 if (bitmap != null) bitmap.Dispose();
 throw;
}

where clipRectangle is the rectangle of the original image you wish to scale into the new bitmap (you'll need to manually deal with aspect ratio). The catch-block is typical IDisposable usage inside a constructor; you maintain ownership of the new IDisposable object until it is returned (you may want to doc that with code-comments).

Saving as Jpeg

Unfortunately, the default "save as jpeg" encoder doesn't expose any quality controls, and chooses a terribly low default quality.

You can manually select the encoder as well, however, and then you can pass arbitrary parameters:

ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders()
    .Where(codecInfo => codecInfo.MimeType == "image/jpeg").First();
using (EncoderParameters encParams = new EncoderParameters(1))
{
 encParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)quality);
 //quality should be in the range [0..100]
 image.Save(outputStream, jpgInfo, encParams);
}
Eamon Nerbonne
Great code snippet! Why are you disposing of the image in your catch block and then rethrowing the exception instead of just using the finally block?
Jeff Widmer
because the finally block *always* executes - but the newly created image only needs to be disposed on error. If no error occurs, the new image was correctly constructed and becomes owned by the calling function - now it's their responsibility to dispose it.
Eamon Nerbonne
I'll add an explanation about the try-catch with IDisposable.
Eamon Nerbonne
Awesome answer, thanks.
blu