Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding base64 Stream to image

I am sending base64 encoded image from client side using javascript (I am creating Screenshot uploader applet for asp.net application using http://supa.sourceforge.net/) and this sends an ajax request to server to store the image. At server I am using HttpContext in GenericHanlder in asp.net application.

How to convert image data from HttpContext to image at server?

like image 859
sunil5715 Avatar asked Oct 15 '12 18:10

sunil5715


2 Answers

First, you need to convert the base 64 back into bytes:

byte[] data = System.Convert.FromBase64String(fromBase64);

Then, you can load it into an instance of Image:

MemoryStream ms = new MemoryStream(data);
Image img = Image.FromStream(ms);

If you want to save it to a file instead, use System.IO.File.WriteAllBytes

like image 76
Steven Hunt Avatar answered Oct 05 '22 22:10

Steven Hunt


I needed to do something similar, but wanted to work directly with the InputStream, so used this to do the decoding:

// using System.Security.Cryptography
var stream = new CryptoStream(Request.InputStream, new FromBase64Transform(), CryptoStreamMode.Read);
var img = Image.FromStream(stream);
like image 41
DigitalDan Avatar answered Oct 05 '22 21:10

DigitalDan