简体   繁体   English

将Base64字符串转换为图像附件

[英]Convert Base64 String To Image Attachment

I have an image converted to a base64 string that I need to convert back to an image and attach to a MailMessage . 我将图像转换为base64字符串,需要将其转换回图像并附加到MailMessage

Here is the relevant code converting it from base64 string to image (I think I can skip the Image object and do this using one memory stream, but had some issues implementing that). 这是将其从base64字符串转换为图像的相关代码(我认为我可以跳过Image对象并使用一个内存流来执行此操作,但是在实现该操作时会遇到一些问题)。 Attempting to save the Image to a MemoryStream throws a generic GDI+ error: 尝试将Image保存到MemoryStream会引发通用GDI +错误:

Image image = ImageHelper.Base64ToImage(attachment.FieldData);

if (image != null)
{
    using (var ms = new MemoryStream())
    {
        image.Save(ms, ImageFormat.Png); // Throws a generic GDI+ error on Save

        ms.Position = 0;

        var imageAttachment = new Attachment(ms, "image.png", "image/png");

        message.Attachments.Add(imageAttachment);
    }
}

public static class ImageHelper
{
    public static Image Base64ToImage(string base64String)
    {
        if (string.IsNullOrEmpty(base64String))
        {
            return null;
        }

        byte[] imageBytes = Convert.FromBase64String(base64String);

        using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            ms.Write(imageBytes, 0, imageBytes.Length);

            Image image = Image.FromStream(ms, true);

            return image;
        }
    }
}

I'm able to serve up the raw base64 string elsewhere using an img tag and it works fine so I'm confident that the problem isn't with the base64 string itself: 我可以使用img标签在其他地方提供原始base64字符串,并且可以正常工作,因此我相信问题不在于base64字符串本身:

<img src="data:image/png;base64,<myBase64StringHere>" alt="My Image" width="500" />

I must be doing something wrong in converting it back, but I haven't been able to figure out the issue. 在将其转换回时,我一定做错了,但我一直无法弄清楚这个问题。 Thanks for any help with this! 感谢您对此的任何帮助!

Image.FromStream(Stream) says, "You must keep the stream open for the lifetime of the Image", but your using statement disposes the stream as the Image is returned. Image.FromStream(Stream)说,“您必须在Image的生存Image.FromStream(Stream)保持流处于打开状态”,但是您的using语句在返回Image时将其处置。 A workaround would be to return both the image and the stream together as a tuple and without the using : 一种解决方法是将图像和流作为元组一起返回,而不using

public static Tuple<Image, MemoryStream> Base64ToImage(string base64String)
{
    if (string.IsNullOrEmpty(base64String))
    {
        return null;
    }

    byte[] imageBytes = Convert.FromBase64String(base64String);
    var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)        
    ms.Write(imageBytes, 0, imageBytes.Length);
    Image image = Image.FromStream(ms, true);

    return new Tuple<Image, MemoryStream>(image, ms);
}

Also note to take care and view each overload on the MSDN pages. 另请注意,请注意并查看MSDN页面上的每个重载。 Normally I would say, "view the most encompassing overload to get all the remarks and notes", but in this case that is not true. 通常,我会说“查看最广泛的重载以获得所有注释和注释”,但是在这种情况下,这是不正确的。 The MSDN page for the biggest overload, Image.FromStream Method (Stream, Boolean, Boolean) does not mention that you need to keep the stream open, but I am fairly certain that is a mistake on that particular page. 最大重载的MSDN页面Image.FromStream Method (Stream, Boolean, Boolean) 没有提到您需要保持流打开,但我可以肯定地说这是特定页面上的错误。

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

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