简体   繁体   English

解码base64流到图像

[英]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. 我正在使用javascript从客户端发送base64编码的图像(我正在使用http://supa.sourceforge.net/为asp.net应用程序创建截屏上传器applet),这会向服务器发送ajax请求以存储图像。 At server I am using HttpContext in GenericHanlder in asp.net application. 在服务器上我在asp.net应用程序中使用GenericHanlder中的HttpContext。

How to convert image data from HttpContext to image at server? 如何将图像数据从HttpContext转换为服务器上的图像?

First, you need to convert the base 64 back into bytes: 首先,您需要将base 64转换回字节:

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

Then, you can load it into an instance of Image: 然后,您可以将其加载到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 如果要将其保存到文件中,请使用System.IO.File.WriteAllBytes

I needed to do something similar, but wanted to work directly with the InputStream, so used this to do the decoding: 我需要做类似的事情,但想直接使用InputStream,所以用它来做解码:

// using System.Security.Cryptography
var stream = new CryptoStream(Request.InputStream, new FromBase64Transform(), CryptoStreamMode.Read);
var img = Image.FromStream(stream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM