Forum Discussion

Fowmy's avatar
Fowmy
Super User
5 years ago

Binary to Image Conversion - BLOB Field from Dynamics NAV

I have tried all possibilities to convert a BLOB field (Bitmap) from a SQL DB (Dynamcis NAV 2013) table to an Image URL, but not been able to find a solution yet.

 I converted the Binary file to Text then added a column as follows:

 

Then, changed the Data Category to Image URL, but the images are not showing up

 

 

Attempts that failed: 
 1. Tried the function: Binary.ToText([Picture],BinaryEncoding.Base64)

2. Removed image file extension and tried "data&colon;image/<image_format>;base64,"  and "data&colon;image/;base64,"

Appreciate any help on this
Thanks

5 Replies

    • Fowmy's avatar
      Fowmy
      Super User

      lbendlin 

      Yes, I did but not helping me. my image size is small and not an issue.

      Thanks

       

      • lbendlin's avatar
        lbendlin
        Super User

        Could you send me a sample bmp?  I guess it doesn't have to come from a blob, any base64 encoded blip should do, right?

  • NAV uses the first 4 bytes as a "magic number" that specifies the custom NAV Blob Type.
    {2, 69, 125, 91}

     

    If you are storing an image in the NAV database, you need to prepend the four bytes to the base-64 encoded byte array containing the image. If you don't, and launch a page that references the Blob fields, NAV will crash.

     

    If you are retrieving an image, you need to strip them. Otherwise, nothing except NAV is aware they are valid images.

     

    Sample procedure I used to store an image in the database, you'll need to reverse it.

    private static byte[] BlobMagic = new byte[]
    {
        2,
        69,
        125,
        91
    };
    
    private static byte[] CompressByteArray(byte[] data)
    {
        byte[] retVal;
        using (MemoryStream compressedMemoryStream = new MemoryStream())
        {
            using (DeflateStream compressStream = new DeflateStream(compressedMemoryStream, CompressionMode.Compress, true))
            {
                compressStream.Write(data, 0, data.Length);
                compressStream.Close();
                retVal = new byte[compressedMemoryStream.Length];
                compressedMemoryStream.Position = 0L;
                compressedMemoryStream.Read(retVal, 0, retVal.Length);
                compressedMemoryStream.Close();
                compressStream.Close();
            }
        }
        // add Blob Magic so NAV doesn't detect a human scent
        return BlobMagic.Concat(retVal).ToArray();
    }