Dynamics 365 Business Central: How to import/create item picture from encoded text (Base64 String)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to create item picture from encoded text (Base64 String) in Business Central.

I saw this question in the Dynamics 365 Community last week. More details: Dynamics Community Forum Thread Details.

Base64 is a binary to a text encoding scheme that represents binary data in an American Standard Code for Information Interchange (ASCII) string format. It’s designed to carry data stored in binary format across the channels, and it takes any form of data and transforms it into a long string of plain text. You can find more about Base64 in wiki.

We have discussed Importing picture/image from URL before, and this time we use a similar requirements.

1. Add a new Import Picture From Encoded Text button in the Item Picture.

2. Create a dialog box for the user to enter the Encoded Text of the picture (variable)

PS: If you want to save encoded text in the table, because the amount of text is very long, it cannot be saved directly.

The maximum length for a field of type ‘Text’ is 2048

Please refer to the following link.
Dynamics 365 Business Central: How to save large text/string in the table

3. Import the picture from the Encoded Text.

First of all, the main character this time is codeunit 4110 “Base64 Convert” (Converts text to and from its base-64 representation)

For image conversion to Encoded Text (Base64), this time I use an external conversion tool.
Convert PNG to Base64 tool:

Update 2023.07.26: How to convert Image (item picture) to encoded text (Base64 String) via AL

You can download the text file below to test it directly.

Orange:

Banana:

Apple:

Test video: Looks good👏👏👏

Give it a try!!!😁

Source Code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50118 ZYItemPictureExt extends "Item Picture"
{
    actions
    {
        addafter(TakePicture)
        {
            action(ImportPictureFromiEncodedText)
            {
                Caption = 'Import Picture From Encoded Text';
                Image = Import;
                ApplicationArea = All;

                trigger OnAction()
                var
                    PictureEncodedTextDialog: Page "Picture Encoded Text Dialog";
                begin
                    PictureEncodedTextDialog.SetItemInfo(Rec."No.", Rec.Description);
                    if PictureEncodedTextDialog.RunModal() = Action::OK then
                        PictureEncodedTextDialog.ImportPictureFromiEncodedText();
                end;
            }
        }
    }
}

page 50102 "Picture Encoded Text Dialog"
{
    PageType = StandardDialog;
    Caption = 'Picture Encoded Text Dialog';

    layout
    {
        area(content)
        {
            field(ItemNo; ItemNo)
            {
                ApplicationArea = All;
                Caption = 'Item No.';
                Editable = false;
            }
            field(ItemDesc; ItemDesc)
            {
                ApplicationArea = All;
                Caption = 'Item Description';
                Editable = false;
            }
            field(PictureEncodedTextDialog; PictureEncodedTextDialog)
            {
                ApplicationArea = All;
                Caption = 'Picture Encoded Text';
                MultiLine = true;
            }
        }
    }
    var
        ItemNo: Code[20];
        ItemDesc: Text[100];
        PictureEncodedTextDialog: Text;

    procedure SetItemInfo(NewItemNo: Code[20]; NewItemDesc: Text[100])
    begin
        ItemNo := NewItemNo;
        ItemDesc := NewItemDesc;
    end;

    procedure ImportPictureFromiEncodedText()
    var
        FileManagement: Codeunit "File Management";
        FileName: Text;
        ClientFileName: Text;
        InStr: InStream;
        OutStr: OutStream;
        TempBlob: Codeunit "Temp Blob";
        Item: Record Item;
        OverrideImageQst: Label 'The existing picture will be replaced. Do you want to continue?';
        MustSpecifyDescriptionErr: Label 'You must add a description to the item before you can import a picture.';
        Base64Convert: Codeunit "Base64 Convert";
    begin
        if Item.Get(ItemNo) then begin
            if Item.Description = '' then
                Error(MustSpecifyDescriptionErr);

            if Item.Picture.Count > 0 then
                if not Confirm(OverrideImageQst) then
                    Error('');
            FileName := ItemDesc + '.png';
            TempBlob.CreateOutStream(OutStr);
            Base64Convert.FromBase64(PictureEncodedTextDialog, OutStr);
            TempBlob.CreateInStream(InStr);

            Clear(Item.Picture);
            Item.Picture.ImportStream(InStr, FileName);
            Item.Modify(true);
        end;
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL