question

moondaddy-8531 avatar image
0 Votes"
moondaddy-8531 asked moondaddy-8531 commented

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

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


dotnet-csharp
imagetext.txt (64.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Try something like this:

 string s = "/9j/4AAQS. . . .";
    
 switch( s.Length % 4 )
 {
 case 3: s += "="; break;
 case 2: s += "=="; break;
 }
    
 var bytes = Convert.FromBase64String( s );
 File.WriteAllBytes( "MyImage.jfif", bytes );
0 Votes 0 ·

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered moondaddy-8531 commented

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();
     } 
 }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @Castorix31 , It was a partial image because I copied the image text from SSMS query results which cut off most of the image text. But that's ok it was proof of concept.

I had to remove the

 sText += "==";

from the code when reading the image text from sql server and not I can write images to the file system.

0 Votes 0 ·