Hieu Nguyen Trung
Hieu Nguyen Trung

Reputation: 1622

ASP.Net MVC 3: Get length in kb of an WebImage

I have a small form that allow user to upload an avatar I used WebImage to get the file uploaded from client (WebImage.GetImageFromRequest() that return WebImage object), in the server i have to check to size of avatar, how can i do that?

Edit: I mean the length of it, the length in kb

Upvotes: 2

Views: 1918

Answers (1)

Brian Dishaw
Brian Dishaw

Reputation: 5825

http://msdn.microsoft.com/en-us/library/system.web.helpers.webimage(v=vs.99).aspx

The WebImage class has properties that expose the Height and Width of the image in pixels. So this will give you the size of the image.

EDIT

I think this post might help you with that, but it requires you have the image saved to disk.

How to get the file size of a "System.Drawing.Image"

Additionally, you could do something like this, though it'll have some overhead as you'll be putting all of the bytes into memory...

WebImage uploadedImage = WebImage.GetImageFromRequest("somefile");
long sizeInKBytes = uploadedImage.GetBytes().Length / 1024;

Upvotes: 4

Related Questions