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 :

Binary data in AX2012

Martin Dráb Profile Picture Martin Dráb 237,976 Most Valuable Professional

For a manipulation with binary data (e.g. binary files including images, serialized objects etc.), AX particularly offers classes BinData, BinaryIo and Binary. While the first two ones are the same in AX2012 as in AX2009, Binary got few new a useful methods:

public static Binary constructFromContainer(container _data)
public static Binary constructFromMemoryStream(CLRObject _memoryStream)
public container getContainer()
public CLRObject getMemoryStream()

Binary therefore can serve as a simple interface between AX (using containers) and data streams in .NET environment. The examples below demonstrate how useful it can be and how easy it is.

The following X++ code reads a content of a file to a variable of FileStream type, pass it to MemoryStream and put it to an X++ container via Binary:

System.IO.FileStream fileStream = System.IO.File::OpenRead(filename);
System.IO.MemoryStream memoryStream = new MemoryStream();
int size = fileStream.get_Length();
container con;
 
memoryStream.SetLength(size);
fileStream.Read(memoryStream.GetBuffer(), 0, size); //write to MemoryStream
con = Binary::constructFromMemoryStream(memoryStream).getContainer(); //from MemoryStream to container

The second example is in C# and requires proxies for X++ classes Image and Binary. It loads an image from AX using a resource ID and use it on a WPF button:

//Loads image with transparent background from AX
Image axImage = new Image();
axImage.loadResource(1030); //ID of some image
axImage.saveType(ImageSaveType.PNG);
 
//Creates data stream
Binary binary = Binary.constructFromContainer(axImage.getData());
MemoryStream stream = (MemoryStream)binary.getMemoryStream();
 
//Creates System.Windows.Media.Imaging.BitmapImage
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = memoryStream;
bitmap.EndInit();
 
//Sets image to a button
var wpfImage = new System.Windows.Controls.Image();
wpfImage.Source = bitmap;
button1.Content = wpfImage;

This was originally posted here.

Comments

*This post is locked for comments