How to strip out header from base 64 image in C#?

You can use String.Split method.

String[] substrings = image.Split(',');

string header = substrings[0];
string imgData = substrings[1]; 

byte[] bytes = Convert.FromBase64String(imgData);

UPDATE

Out of curiosity, I wrote a test which method is the fastest.

using System;
using System.Text.RegularExpressions;

namespace StackOwerflow
{
    public class Program
    {
        static public void Main()
        {
            int repeats = 10000;

            string imgStr = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB0IAAAQ4CAIAA...eXiM/H/wAAAABJRU5ErkJggg=="; //146 kb img file
            string r = string.Empty;

            var watch = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i < repeats; i++)
            {
                r = RegExMethod(imgStr);
            }
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine("RegEx time: {0} Ms", elapsedMs);

            watch = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i < repeats; i++)
            {
                r = SubStringMethod(imgStr);
            }
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine("SubString time: {0} Ms", elapsedMs);

            watch = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i < repeats; i++)
            {
                r = SplitMethod(imgStr);
            }
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine("Split time: {0} Ms", elapsedMs);

            Console.ReadKey();
        }

        public static string RegExMethod(string img)
        {
            return Regex.Replace(img, @"^data:image\/[a-zA-Z]+;base64,", string.Empty);
        }

        public static string SubStringMethod(string img)
        {
            return img.Substring(img.IndexOf(",") + 1);
        }

        public static string SplitMethod(string img)
        {
            return img.Split(',')[1];
        }

    }
}

And for my machine results:

RegEx time: 1022 Ms

SubString time: 1188 Ms

Split time: 5255 Ms

And the winner is RegEx.


Since you know the only instance of , in the string will be the separator between the preamble and the data, you could do it without regex like this:

string convert = image.Substring(image.IndexOf(",") + 1);

You could try something like this:

string result = Regex.Replace(image, @"^data:image\/[a-zA-Z]+;base64,", string.Empty);

this should catch the different extensions. I haven't tested this though so it might need some fiddling with.

Tags:

C#

Base64