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

时间:2022-12-02 21:20:03

I want to convert an image to base64 and back to image again. Here is the code which i tried so far and the error also. Any suggestions please?

我想将图像转换为base64并再次返回图像。这是我到目前为止尝试的代码和错误。有什么建议吗?

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. Please review the stack trace for more information about the error and where it originated in the code.

描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

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

2 个解决方案

#1


7  

You are reading from an empty stream, rather than loading the existing data (imageBytes) into the stream. Try:

您正在读取空流,而不是将现有数据(imageBytes)加载到流中。尝试:

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; I would propose:

此外,您应该努力确保处理finalImage;我建议:

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; YMMV.

最后,请注意ASP.NET上不支持System.Drawing;因人而异。

Caution

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. 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或ASP.NET服务中使用System.Drawing命名空间中的类。尝试在其中一种应用程序类型中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常。有关支持的替代方法,请参阅Windows Imaging Components。

#2


2  

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);

#1


7  

You are reading from an empty stream, rather than loading the existing data (imageBytes) into the stream. Try:

您正在读取空流,而不是将现有数据(imageBytes)加载到流中。尝试:

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; I would propose:

此外,您应该努力确保处理finalImage;我建议:

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; YMMV.

最后,请注意ASP.NET上不支持System.Drawing;因人而异。

Caution

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. 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或ASP.NET服务中使用System.Drawing命名空间中的类。尝试在其中一种应用程序类型中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常。有关支持的替代方法,请参阅Windows Imaging Components。

#2


2  

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);