web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Convert container to System.Byte[]

(0) ShareShare
ReportReport
Posted on by

Hi there

I have pictures in the data base (such as company logo) and I need to pass it to an external application which need the content as a System.Byte array.

How can I achieve such a thing?

Regards

*This post is locked for comments

I have the same question (0)
  • ImperialS Profile Picture
    425 on at

    Hey!

    I tried to find answer on same question and found static method in ax 2012 SRSFrameworkServiceHelper::convertContainerToBytes() ( same code must be available on 2009 too).

    private static System.Byte[] convertContainerToBytes(container _container)
    {
        System.IO.MemoryStream  memStream;
        System.Byte[]           bytes;
        Binary                  assemblyBinary;
    
        if (_container)
        {
            try
            {
                assemblyBinary = Binary::constructFromContainer(_container);
                memStream = assemblyBinary.getMemoryStream();
                bytes = memStream.ToArray();
                memStream.Close();
            }
            catch (Exception::CLRError)
            {
                SRSProxy::handleClrException(Exception::Error);
            }
    
            return bytes;
        }
    
        return null;
    }

    PS Ofc "_container" it is a blob data type.
  • Suggested answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    I think that getMemoryStream() was added in AX 2012.

    AX 2009 probably requires using the Binary class to write bytes one by one.

  • ImperialS Profile Picture
    425 on at

    Yep, you are right, my solution only for ax 2012.

  • ImperialS Profile Picture
    425 on at

    Good evening.

    In order to resolve this issue in ax 2009 use BinData class.

    System.Byte[] bytes;
    BinData binData;
    ;
    
    binData = new BinData();
    binData.setData(<container>);
        
    bytes = System.Convert::FromBase64String(binData.base64Encode());
  • Suggested answer
    Sebastian Strefel Profile Picture
    7 on at

    Hello, I just want post a solution for similar issue you mentioned.

    My original problem was, that the mehod Binary::constructFromContainer(_container) returned this error “Microsoft.Dynamics.Ax.Xpp.InfoException: Error executing code: Binary (object), method constructFromContainer called with invalid parameters.”

    I didn’t know, that this method expected a specific format of the given container.

    I have solve this error with the code below:

    public static System.Byte[] container2Byte(container value)
    {
        System.Byte[] byteArray = null;
    
        ContainerClass containerClass = new ContainerClass(value);
        container blobContainer = containerClass.toBlob();
    
        Binary binary = Binary::constructFromContainer(blobContainer);
        using (System.IO.MemoryStream stream = binary.getMemoryStream())
        {
            byteArray = stream.ToArray();
        }
        
        return byteArray;
    }

    And the vice versa way:

    public static container byte2Container(System.Byte[] value)
    {
        container blobContainer;
        using (System.IO.MemoryStream stream = new System.IO.MemoryStream(value))
        {
            Binary binary = Binary::constructFromMemoryStream(stream);
            blobContainer = binary.getContainer();
        }
    
        return ContainerClass::blob2Container(blobContainer);
    }

    Maybe this helps others with the same issue which I had.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans