๐ Introduction
In Dynamics 365 Finance and Operations (D365FO), it's common to deal with external systems returning image streams, such as a barcode service, a document API, or a file server. In such cases, you may receive the image as a binary stream (System.IO.Stream), You’ll need to convert it into a container to display it in the UI or store it in the database.
This blog post explains how to convert a MemoryStream into a displayable image in D365FO using X++.
๐งช Sample Scenario
Let’s say you're calling an external URL using System.Net.HttpWebRequest and want to load the image from the response into a form control.
✅ X++ Code to Convert MemoryStream to Image
System.Net.HttpWebResponse response;
System.IO.Stream stream;
request.ContentType = "image/png";
stream = response.GetResponseStream();
stream.CopyTo(fileStream);
// Convert memory stream to container
BinData binData = new BinData();
binData.setData(baseContainer);
// Set image data
image.setData(binData.getData());
FormImageControl1.image(image);
}
๐ฌ Explanation
-
✅
HttpWebRequestis used to call the external image URL. -
✅ The response stream is copied into a
MemoryStream. -
✅ We use
Binary::constructFromMemoryStream()to convert the stream into a container. -
✅ This container is then used to create an
Imageobject. -
✅ Finally, we bind it to the form’s image control like
FormImageControl1.
๐ก Use Case Ideas
-
Show customer logos dynamically.
-
Load barcode images from external services.
-
Display product thumbnails received via APIs.
๐ง Conclusion
BinData + Image objects. This approach is ideal for real-time image rendering from REST APIs or streaming sources.