How to create a Temp Blob with an image?

For testing a feature very similar to the standard Document Attachments using automated tests, I needed to create a Temp Blob filled with some tiny image in given, different image formats (jpg, png etc.). The Base Application does already provide such a procedure called CreateTempBLOBWithImageOfType (source):

    local procedure CreateTempBLOBWithImageOfType(var TempBlob: Codeunit "Temp Blob"; ImageType: Text)
    var
        ImageFormat: DotNet ImageFormat;
        Bitmap: DotNet Bitmap;
        InStr: InStream;
    begin
        TempBlob.CreateInStream(InStr);
        Bitmap := Bitmap.Bitmap(1, 1);
        case ImageType of
            'png':
                Bitmap.Save(InStr, ImageFormat.Png);
            'jpeg':
                Bitmap.Save(InStr, ImageFormat.Jpeg);
            else
                Bitmap.Save(InStr, ImageFormat.Bmp);
        end;
        Bitmap.Dispose();
    end;

But looking at the highlighted DotNet variables above, we cannot declare such a procedure in our own code. While I searched for alternatives, I found the Image module in the System Application. It already provides a code sample for saving an image in an arbitrary format. It also points to the Image Format enum that we should use:

procedure Example()
var
    TempBlob: Codeunit "Temp Blob";
    Image: Codeunit Image;
    InStream: InStream;
    OutStream: OutStream;
    FileName: Text;
begin
    UploadIntoStream('', '', '', FileName, InStream);

    Image.FromStream(InStream);
    Image.SetFormat(Enum::"Image Format"::Png);

    TempBlob.CreateOutStream(OutStream);
    Image.Save(OutStream);	

    TempBlob.CreateInStream(InStream);
    FileName := FileName + '.png';
    DownloadFromStream(InStream, '', '', '', FileName);
end;

Good, but since this is about writing automated tests, the last step was to eliminate the code for uploading and downloading a file, and to replace it with some fixed sample data. As I am still a total loser at jiggling with streams, instead I looked into the standard tests for the Image module, and found a test called CreateImageFromStreamTest. I only needed to put all the puzzle pieces together – and this is my updated version of CreateTempBLOBWithImageOfType:

    local procedure CreateTempBLOBWithImageOfType(var TempBlob: Codeunit "Temp Blob"; ImageFormat: Enum "Image Format")
    var
        Image: Codeunit Image;
        Base64Convert: Codeunit "Base64 Convert";
        InStream: InStream;
        OutStream: OutStream;
        ImageAsBase64Txt: Label 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAFCAYAAAB8ZH1oAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAhSURBVBhXYwCC/0RirILYMIIDAtjYUIzCwYexCqJhhv8AD/M3yc4WsFgAAAAASUVORK5CYII=', Locked = true;
    begin
        TempBlob.CreateOutStream(OutStream);
        Base64Convert.FromBase64(ImageAsBase64Txt, OutStream);
        TempBlob.CreateInStream(InStream);

        Image.FromStream(InStream);
        Image.SetFormat(ImageFormat);       
        Image.Save(OutStream);
    end;

It takes in an Image Format Enum (supporting much more formats than earlier), and returns the Temp Blob. Happy AL coding!

One thought on “How to create a Temp Blob with an image?

Add yours

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started