简体   繁体   English

将图像转换为base64,反之亦然

[英]Convert an image to base64 and vice versa

I want to convert an image to base64 and back to image again. 我想将图像转换为base64并再次返回图像。 Here is the code which i tried so far and the error also. 这是我到目前为止尝试的代码和错误。 Any suggestions please? 有什么建议吗?

public void Base64ToImage(string coded)
{
    System.Drawing.Image finalImage;
    MemoryStream ms = new MemoryStream();
    byte[] imageBytes = Convert.FromBase64String(coded);
    ms.Read(imageBytes, 0, imageBytes.Length);
    ms.Seek(0, SeekOrigin.Begin);
    finalImage = System.Drawing.Image.FromStream(ms);

    Response.ContentType = "image/jpeg";
    Response.AppendHeader("Content-Disposition", "attachment; filename=LeftCorner.jpg");
    finalImage.Save(Response.OutputStream, ImageFormat.Jpeg);
}

The error is : 错误是:

Parameter is not valid. 参数无效。

Description: An unhandled exception occurred during the execution of the current web request. 描述:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

Exception Details: System.ArgumentException: Parameter is not valid. 异常详细信息:System.ArgumentException:参数无效。

Source Error: 来源错误:

Line 34:             ms.Read(imageBytes, 0, imageBytes.Length);
Line 35:             ms.Seek(0, SeekOrigin.Begin);
Line 36:             finalImage = System.Drawing.Image.FromStream(ms);
Line 37:         
Line 38:         Response.ContentType = "image/jpeg";

Source File: e:\\Practice Projects\\FaceDetection\\Default.aspx.cs Line: 36 源文件:e:\\ Practice Projects \\ FaceDetection \\ Default.aspx.cs行:36

You are reading from an empty stream, rather than loading the existing data ( imageBytes ) into the stream. 您正在读取空流,而不是将现有数据( imageBytes )加载流中。 Try: 尝试:

byte[] imageBytes = Convert.FromBase64String(coded);
using(var ms = new MemoryStream(imageBytes)) {
    finalImage = System.Drawing.Image.FromStream(ms);
}

Also, you should endeavour to ensure that finalImage is disposed; 此外,您应该努力确保处理finalImage ; I would propose: 我建议:

System.Drawing.Image finalImage = null;
try {
    // the existing code that may (or may not) successfully create an image
    // and assign to finalImage
} finally {
    if(finalImage != null) finalImage.Dispose();
}

And finally, note that System.Drawing is not supported on ASP.NET; 最后,请注意ASP.NET上不支持 System.Drawing; YMMV. 因人而异。

Caution 警告

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. System.Drawing命名空间中的类不支持在Windows或ASP.NET服务中使用。 Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. 尝试在其中一种应用程序类型中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常。 For a supported alternative, see Windows Imaging Components. 有关支持的替代方法,请参阅Windows Imaging Components。

The MemoryStream.Read Method reads bytes from the MemoryStream into the specified byte array. 所述MemoryStream.Read方法读取来自字节的MemoryStream到指定的字节数组。

If you want to write the byte array to the MemoryStream , use the MemoryStream.Write Method : 如果要将字节数组写入MemoryStream ,请使用MemoryStream.Write方法

ms.Write(imageBytes, 0, imageBytes.Length);
ms.Seek(0, SeekOrigin.Begin);

Alternatively, you can simply wrap the byte array in a MemoryStream : 或者,您可以简单地将字节数组包装在MemoryStream中

MemoryStream ms = new MemoryStream(imageBytes);

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

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