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.
I use
Image.FromStreamto as follows:Note that the returned
ImageisIDisposable.You'll need a reference to
System.Drawing.dllfor this to work, andImageis in theSystem.Drawingnamespace.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
clipRectangleis 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 typicalIDisposableusage inside a constructor; you maintain ownership of the newIDisposableobject 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: