Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use ArrayToImage in C#.Net?

Solved!
Go to solution

I am trying to process a C# byte array using NI Vision tools.  To do this I first need to convert the array to a CWIMAQImage using ArrayToImage.  With the following code:

 

                CWIMAQImage srcImage;

                CWIMAQImage destImage;

srcImage = new CWIMAQImage();

                srcImage.SetSize(imageWidth, imageHeight); 

byte[] bufferArray = new byte[imageWidth * imageHeight];

// processing to initialize the buffer
...

                // Convert buffer to image

srcImage.ArrayToImage((Object)bufferArray);

// Process with Vision tools

...

bufferArray = (byte[])destImage.ImageToArray(0, 0, imageWidth, imageHeight);

 

I get the following runtime error on the call to ArrayToImage:

 

"You have passed an invalid type into a parameter of a VARIANT type"

 

Any suggestions of how I can do this?

 

I will also need to get the resulting image back into a C# byte array using ImageToArray. Because I haven't been able to get that far in running the code I don't know if my call will work or not.

 

Thanks,

Dave 

0 Kudos
Message 1 of 8
(6,065 Views)
Solution
Accepted by topic author mpu

Dave -

 

CWIMAQImage.ArrayToImage() takes in a 2D array, so your code would need to look something like:

    

byte[,] bufferArray = new byte[imageWidth, imageHeight];

// processing to initialize the buffer
...

                // Convert buffer to image

srcImage.ArrayToImage((Object)bufferArray);

 

For CWIMAQImage.ImageToArray(), it will return the array as an int[,], so you need to have a variable of that type set up and cast to int[,].

 

Greg Stoll

Vision R&D

National Instruments

Greg Stoll
LabVIEW R&D
0 Kudos
Message 2 of 8
(6,063 Views)

Hey mpu,

 

I'll paste the appropriate syntax and a simple example for you, but basically the array parameter needs to be a variant:

 

ArrayToImage Method

ArrayToImage Method

Syntax

CWIMAQImage.ArrayToImage Array

Purpose

Creates an image from an array.

Remarks

The Type property determines the resulting image type. For example, to create a color image from an input 2D array of integers, set the Type property to cwimaqImageTypeRGB32 and call ArrayToImage.

If the image type is not cwimaqImageTypeComplex or cwimaqImageTypeRGBU64, the input array is a 2D array of scalars.

If the image type is cwimaqImageTypeComplex, the input array is a 1D array of two elements. The first element is a 2D array containing the real plane, and the second element is a 2D array containing the imaginary plane. Both the arrays must have the same size.

If the image type is cwimaqImageTypeRGBU64, the input array is a 2D array of 1D arrays with 4 integer elements (the Red, Blue, Green, and Alpha channels of each pixel).

Parameters

Array As Variant

The source array of image data.

Example

Private Sub Run_Click()
    Const Size = 127
    Dim a(0 To Size, 0 To Size) As Long
    Dim i, j
    
    For i = 0 To Size
        For j = 0 To Size
            a(i, j) = i + j
        Next j
    Next i
    
    CWIMAQViewer1.Palette.Type = cwimaqPaletteRainbow
    CWIMAQViewer1.Image.ArrayToImage a
End Sub 

 

 

And while this is for VB, it should be almost exactly the same for your C# since our vision is not native to .net, and we are currently just using a .net interop.

Chris Van Horn
Applications Engineer
0 Kudos
Message 3 of 8
(6,060 Views)

Thanks, Greg.  That took care of the ArrayToImage issue.

 

Can you point me to samples of using NI Vision in C#.Net?  I have been using NI Vision from VB.Net and am just moving over. Many of the methods have Optional arguments in VB that are required in C#.  Seeing some working examples would be helpful.

 

Thanks,

Dave

0 Kudos
Message 4 of 8
(6,053 Views)

We have some MSVC examples in:

 

C:\Program Files\National Instruments\Vision\Examples\MSVC

 

Which is going to vary from acquisition to processing.

Chris Van Horn
Applications Engineer
0 Kudos
Message 5 of 8
(6,049 Views)

Dave -

 

Unfortunately, we don't currently have any examples using the NI Vision interop with C#.  If you have further questions about specific methods, I'd be happy to do my best to answer them.

 

The good news is that the next version of NI Vision will include native .NET support, with a new interface that will avoid these nasty casting issues.  It will also include a full set of examples for both C# and VB.NET.

 

Greg Stoll

Vision R&D

National Instruments

Greg Stoll
LabVIEW R&D
Message 6 of 8
(6,045 Views)

Check this one.....ByteArray to Image

 

William

 

0 Kudos
Message 7 of 8
(4,558 Views)

Thanks, William. That is a good link.

 

--Dave

0 Kudos
Message 8 of 8
(4,552 Views)