Convert a .NET PictureBox to an IMAQ Image

Updated Apr 1, 2019

Reported In

Driver

  • NI-IMAQ

Issue Details

I have a .NET PictureBox that contains an image. Now, I would like to process the image using the the IMAQ API. How do I convert the .NET PictureBox to an IMAQ Image?

Solution

One way to convert the .NET PictureBox image to an IMAQ Image is to use a memory stream.
  1. Use a Property Node to get a reference to the current Image in the .NET PictureBox.
  2. Add an Invoke Node and navigate to Select Method » Save(Stream stream, ImageFormat format)
  3. To create the memory stream, first add a Constructor Node. Then right-click and press Select Constructor... . Select the mscorlib(4.0.0.0) assembly from the drop-down list. Navigate to System.IO.MemoryStream under Objects and select the MemoryStream() constructor. Press OK.
  1. Wire the refnum of the constructed memory stream to the stream input of the Save method invoke node you created in step 2.
  2. Next select the image format. Add a Property Node, right-click and navigate to Select Class... » .NET » System.Drawing.Imaging.ImageFormat. Then, right-click again and press Select Property to choose png as the image format.
  3. To access the memory buffer, add a new Invoke Node, wire a reference to the memory stream, and select method GetBuffer(). This returns a byte array representing the image in png-format.
  4. Convert the byte array to a string and use PNG Data to LV Image.vi to convert the data to a cluster of image data.
  5. The Unflatten Pixmap.vi can then convert the image data to a 2D array of pixel values, i.e. a pixmap.
  6. Finally, the pixmap can be converted to an IMAQ Image using the IMAQ ArrayToColorImage.vi

Additional Information

For more details about the .NET classes, their properties and methods, consult the external link: .NET API Browser.