Friday, January 22, 2010

Create a base64 encoded string from an image in AX

Here's how you can create a base64 encoded string from an image in AX:

static void Job4(Args _args)
    System.Drawing.Image     image;
    Str                      string;
    System.IO.MemoryStream   memoryStream;

    // Load the file
    image = System.Drawing.Image::FromFile(@"C:\sandbox\test.tif");

    // Convert the file to a memory stream
    memoryStream = new System.IO.MemoryStream();
    image.Save(memoryStream, System.Drawing.Imaging.ImageFormat::get_Tiff());

    // Create the base64 encoded string
    string = System.Convert::ToBase64String(memoryStream.ToArray());

    // Save it again, to a new image (just for fun or test if you want)
    AifUtil::saveBase64ToFile(@"C:\sandbox\test1.tif", string);


Jan said...

Nick picker: Base64 is 6 bit not 64 bit.

Palle Agermark said...

True - I really should only do one thing at a time :-)

Marcel said...

Can I load an image in AX from an base 64 string (or from an System.Drawing.Bitmap), without use files in disk?