c# - reduce image size (physical and dimensions) coming from HttpPostedFileBase then convert to base64 -
does have example of converting image file coming httppostedfilebase reduced size , converting image base64? i've spent hours on out luck. here start of code. of hardcoded (image size).
this giving me black image when place base64 in image tag , view in browser.
public actionresult upload(httppostedfilebase file, decimal? id, decimal? id2) { image img = image.fromstream(file.inputstream, true, true); var bitmap = new bitmap(img.width - 100, img.height - 100); system.io.memorystream stream = new system.io.memorystream(); bitmap.save(stream, system.drawing.imaging.imageformat.bmp); byte[] imagebytes = stream.toarray(); string base64string = convert.tobase64string(imagebytes); insertimage(base64string); }
i'm asking how change image convert base64. more specific question called duplicate.
i have never used httppostedfilebase myself. so, simplified problem bit, should try future questions. should try narrow focus as possible. said, here method reduces dimensions of image represented stream , returns new image array of bytes.
private static byte[] reducesize(filestream stream, int maxwidth, int maxheight) { image source = image.fromstream(stream); double widthratio = ((double)maxwidth) / source.width; double heightratio = ((double)maxheight) / source.height; double ratio = (widthratio < heightratio) ? widthratio : heightratio; image thumbnail = source.getthumbnailimage((int)(source.width * ratio), (int)(source.height * ratio), abortcallback, intptr.zero); using (var memory = new memorystream()) { thumbnail.save(memory, source.rawformat); return memory.toarray(); } }
you can call method this:
public actionresult upload(httppostedfilebase file, decimal? id, decimal? id2) { byte[] imagebytes = reducesize(file.inputstream, 100, 100); string base64string = convert.tobase64string(imagebytes); insertimage(base64string); }
my reducesize() method maintains aspect ratio. may not need that, , may want change arguments can change how specify how resized. give shot , let me know how does.
Comments
Post a Comment