• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 808
  • Last Modified:

Decode base64 image from Xml using C#

hello everyone,
I need to regenerate an image from an Xml file, where the image is encoded as base64. I have to create a windows application using C# which allows the user to select more than one Xml file.
I know that C# allows you to select an element within Xml file, and it is possible to decode the image from base64 to its original format. But I am having a hard time to find the right code to do that.
Thanks in advance for your help.
0
doc_amin
Asked:
doc_amin
1 Solution
 
Bob LearnedCommented:
Here is an image converter class:

using System;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;

public class ImageConverter
{

    public static Image ByteArrayToImage(byte[] data, string fileName)
    {
        using (MemoryStream ms = new MemoryStream(data))
        {
            Image img = Image.FromStream(ms);
            return img;
        }
    }

    public static byte[] ImageToByteArray(Image img, ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, format);
            return ms.ToArray();
        }
    }

    public static string ImageToBase64String(Image img, ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, format);
            return Convert.ToBase64String(ms.ToArray());
        }
    }

    public static Image Base64StringToImage(string imageString)
    {
        byte[] data = Convert.FromBase64String(imageString);
        using (MemoryStream ms = new MemoryStream(data))
        {
            return Image.FromStream(ms);
        }
    }

}

Bob
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now