Dynamics 365 Business Central: How to get the format, size, width and height of the picture/image

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to get the size, width and height of the picture/image in Business Central.
When uploading pictures to Business Central, sometimes you may need to record some basic information of pictures, such as format, size, width and height. How can this be done quickly?

For picture size, we can use the InStream.Length() Method (Gets the stream length) or procedure Length() in codeunit 4100 “Temp Blob”. More details: Better InStream support (InStream.Length & InStream.Position)

The rest of the information can be done with CodeUnit Image (ID 3971).

CodeUnit Image (ID 3971): Provides functionality for working with images.

It contains many interesting methods.

procedure GetWidth(): Gets the width in pixels.
procedure GetHeight(): Gets the height in pixels.
procedure GetFormat(): Gets the image format as an Enum “Image Format”.

But before using these methods, we need to read the data into Image Codeunit, which is a bit like CodeUnit Temp Blob. Microsoft has prepared two methods.

PS:
1. How to import/create item picture from encoded text (Base64 String)
2. How to convert Image (item picture) to encoded text (Base64 String) via AL

Let me give two simple examples.

1. Item Picture:

    2. Customer Picture:

    Very simple, 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 50112 ZYItemCard extends "Item Card"
    {
        actions
        {
            addafter("&Bin Contents")
            {
                action(GetPictureInfo)
                {
                    Caption = 'Get Picture Info';
                    ApplicationArea = All;
                    Promoted = true;
                    PromotedCategory = Process;
                    Image = GetLines;
    
                    trigger OnAction()
                    begin
                        GetPictureInfo();
                    end;
                }
            }
        }
    
        var
            Msg: Label 'Format is ''%1''\Size is ''%2'' bytes\Width is ''%3'' pixels\Height is ''%4'' pixels';
    
        local procedure GetPictureInfo()
        var
            Image: Codeunit Image;
            InS: InStream;
            ItemTenantMedia: Record "Tenant Media";
        begin
            if Rec.Picture.Count > 0 then begin
                ItemTenantMedia.Get(Rec.Picture.Item(1));
                ItemTenantMedia.CalcFields(Content);
                ItemTenantMedia.Content.CreateInStream(InS, TextEncoding::UTF8);
                Image.FromStream(InS);
                Message(Msg, Image.GetFormat(), InS.Length, Image.GetWidth(), Image.GetHeight());
            end;
        end;
    }
    
    pageextension 50113 ZYCustomerCardExt extends "Customer Card"
    {
        actions
        {
            addafter("Sales Journal")
            {
                action(GetPictureInfo)
                {
                    Caption = 'Get Picture Info';
                    ApplicationArea = All;
                    Promoted = true;
                    PromotedCategory = Process;
                    Image = GetLines;
    
                    trigger OnAction()
                    begin
                        GetPictureInfo();
                    end;
                }
            }
        }
    
        var
            Msg: Label 'Format is ''%1''\Size is ''%2'' bytes\Width is ''%3'' pixels\Height is ''%4'' pixels';
    
        local procedure GetPictureInfo()
        var
            Image: Codeunit Image;
            InS: InStream;
            CustTenantMedia: Record "Tenant Media";
        begin
            if Rec.Image.HasValue then begin
                CustTenantMedia.Get(Rec.Image.MediaId);
                CustTenantMedia.CalcFields(Content);
                CustTenantMedia.Content.CreateInStream(InS, TextEncoding::UTF8);
                Image.FromStream(InS);
                Message(Msg, Image.GetFormat(), InS.Length, Image.GetWidth(), Image.GetHeight());
            end;
        end;
    }

    PS:
    1. Rotate and flip picture/image in Dynamics 365 Business Central.

    2. How to quickly get the extension name, file name without extension, and Media (MIME) types from an uploaded file

    END

    Hope this will help.

    Thanks for reading.

    ZHU

    コメント

    Copied title and URL