public void FormatPicture(HttpPostedFileBase file) {
WebImage image = new WebImage(file.InputStream);
if (image.Width > 85 || image.Height > 85) {
int w = image.Width;
int h = image.Height;
if (w > h) {
w = 85;
h = image.Height * w / image.Width;
}
else {
h = 85;
w = image.Width * h / image.Height;
}
image.Resize(w, h);
}
image.Save(PictureFilepath);
}
The HttpPostedFileBase file comes from the view model and contains the uploaded image from the file control. In this example the image is resized to a maximum of 85 pixels, with the smaller dimension interpolated, and stored.
10 March 2014
MVC - Resize an image
It turns out there is a helper class in the System.Web namespace for treating images. This class, WebImage, makes things like resizing e.g. a user profile picture when it is uploaded very easy:
Subscribe to:
Post Comments (Atom)
thank you so much
ReplyDelete