0

In my sql table I have a varchar that represents an image. The string looks like this:

255 216 255 224 0 16 74 70 73 70 0 1 1 1 0 96 0 96 0 0 255 219 0 67 ...

saved as a .txt it is around 20KB in size.

First question: what type of string is this? I couldn't find anything online. Looks to me like RGB values because 255 is the highest number in there.

How can I use C# (ASP.net MVC to be precise) to convert this string format to an actual image again?

Thank you.

UPDATE

Talked to the developer:

the image is pulled from the Active Directory thumbnailPhoto attribute and is stored as a string. Maybe it's best to convert the source instead.

4
  • 5
    Maybe you should ask the table owner what that data mean Commented Oct 27, 2015 at 14:17
  • 4
    While you're at it ask them why in the name of all things holy they stored binary data in a text-based field. (SQL has supported binary data for many many years) Commented Oct 27, 2015 at 14:21
  • 1
    Looking at raw data without any information about what it represents is useless. As a starting point, you should at least know the image's format. Is it JPG? Is it PNG? Is it GIF? Every format has different headers and stores different pixel information. Every format is different to read. Commented Oct 27, 2015 at 14:21
  • Is it possible to read that into a byte array? Then use the Image class to create an image from it? If possible hunt down the code which saves the file, then just do it backwards. Commented Oct 27, 2015 at 14:23

1 Answer 1

2

You'll need to know the encoding. As an example, here is how I am decoding a binary string into a tiff image:

    private static BlockUIContainer ConvertBinaryToImageUIContainer(string binary)
    {
        byte[] binaryData = Convert.FromBase64String(binary);
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = new MemoryStream(binaryData);
        bitmapImage.EndInit();
        BlockUIContainer container = new BlockUIContainer();
        container.Child = new System.Windows.Controls.Image { Source = bitmapImage };
        return container;
    }

Take the binary string and depending on the encoding convert it to byte. In my case it was a Base64String. Create a new image and create a memorystream as the source. I, optionally, set mine in a BlockUIContainer to display in a WPF app, but you could use the bitmapImage or byte[] data how you wish. Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.