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);