Forum Discussion
Fowmy
5 years agoSuper User
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 T...
jmsceski
4 years agoNew Member
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();
}