#C: Convert images from Google's Firebase database as text to images on the file system

moondaddy 911 Reputation points
2021-07-12T15:47:33.777+00:00

I'm migrating data from a Google Firebase database to the .net platform and this db has a lot of images. I get them from firebase in json format which is text and need to convert them to image files. I have attached a sample of one of the images. Note: all images start with "/9j/". Not sure what this is. All images were created from iOS and Android devices. What would be the c# code to do this?

Thanks.

113946-imagetext.txt

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,935 questions
{count} votes

Accepted answer
  1. Castorix31 85,211 Reputation points
    2021-07-12T16:21:31.94+00:00

    You can do something like :
    (I added "==" to get a correct string and I get a valid JPG, but a partial image from your sample file...)

    {
        var sText = System.IO.File.ReadAllText(@"C:\Users\Christian\Downloads\imagetext.txt");
        sText += "==";
        string sDestFile = @"e:\test\testimage.jpg";
        var bytes = Convert.FromBase64String(sText);
        using (var fs = new FileStream(sDestFile, FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
            fs.Flush();
        } 
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.