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.
Best Solution
I use
Image.FromStream
to as follows:Note that the returned
Image
isIDisposable
.You'll need a reference to
System.Drawing.dll
for this to work, andImage
is in theSystem.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...
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 typicalIDisposable
usage inside a constructor; you maintain ownership of the newIDisposable
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: