将图像转换为base64,反之亦然 [英] Convert an image to base64 and vice versa

查看:141
本文介绍了将图像转换为base64,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像转换为Base64和回的图像了。 这里是code,我试过到目前为止,并错误也。有什么建议吗?

 公共无效Base64ToImage(字符串codeD)
{
    为System.Drawing.Image finalImage;
    MemoryStream的毫秒=新的MemoryStream();
    byte []的imageBytes = Convert.FromBase64String(codeD);
    ms.Read(imageBytes,0,imageBytes.Length);
    ms.Seek(0,SeekOrigin.Begin);
    finalImage = System.Drawing.Image.FromStream(毫秒);

    Response.ContentType =为image / jpeg;
    Response.AppendHeader(内容处置,附件;文件名= LeftCorner.jpg);
    finalImage.Save(Response.OutputStream,ImageFormat.Jpeg);
}
 

该错误是:

  

参数是无效的。

     

说明:当前Web请求的执行过程中发生未处理的异常。请查看有关错误的详细堆栈跟踪信息,以及它起源于code。

     

异常详细信息:System.ArgumentException:参数无效

     

源错误:

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

  

源文件:E:\实践项目\ FaceDetection \ Default.aspx.cs行:36

解决方案

您的的从空流,而不是装载现有的数据(<$ C C $> imageBytes )的的流。尝试:

 字节[] imageBytes = Convert.FromBase64String(codeD);
使用(VAR毫秒=新的MemoryStream(imageBytes)){
    finalImage = System.Drawing.Image.FromStream(毫秒);
}
 

此外,你应该努力确保 finalImage 配置;我建议:

 为System.Drawing.Image finalImage = NULL;
尝试 {
    //现有的code,可能会(也可能不会)成功创建图像
    //并分配到finalImage
} 最后 {
    如果(finalImage!= NULL)finalImage.Dispose();
}
 

最后,请注意System.Drawing中 是不支持的 在ASP.NET;情况因人而异。

  

注意

     不支持在Windows或ASP.NET服务使用System.Drawing命名空间中的

类。尝试使用这些类从在这些类型的应用程序中的一个可能会产生意想不到的问题,如降低服务性能和运行时异常。受支持的替代方法,请参阅Windows成像组件。

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?

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.

Exception Details: System.ArgumentException: Parameter is not valid.

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

解决方案

You are reading from an empty stream, rather than loading the existing data (imageBytes) into the stream. 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; 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; YMMV.

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.

这篇关于将图像转换为base64,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆