c# - How to resize an image's byte[] while keeping proportions? -
in asp.net mvc 4 application, have view allows users hover on image full-sized preview. works well.
currently, have image user hovers on set static width , height of 50 , 50, so:
<img id="@model.value" class="image-preview" height="50" width="50" src="@model.imagestring" />
@model.imagestring
value gets created action:
[httpget] public string getimageurl(guid fileid) { var file = db.fetchedfiles .first(ff => ff.id == fileid); return "data:image/*;base64," + convert.tobase64string(file.data); }
the above action i'd modify. how can output convert.tobase64string(file.data)
thumbnail image same proportions original file?
thanks in advance!
i found example on web, can't recall exactly. i'm guessing it's common solution.
here's went with:
using (var ms = new memorystream(data)) { var image = image.fromstream(ms); var ratiox = (double)150 / image.width; var ratioy = (double)50 / image.height; var ratio = math.min(ratiox, ratioy); var width = (int)(image.width * ratio); var height = (int)(image.height * ratio); var newimage = new bitmap(width, height); graphics.fromimage(newimage).drawimage(image, 0, 0, width, height); bitmap bmp = new bitmap(newimage); imageconverter converter = new imageconverter(); data = (byte[])converter.convertto(bmp, typeof(byte[])); return "data:image/*;base64," + convert.tobase64string(data); }
Comments
Post a Comment