03-31-2009 01:29 PM
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];
// 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
Solved! Go to Solution.
03-31-2009 01:46 PM
Dave -
CWIMAQImage.ArrayToImage() takes in a 2D array, so your code would need to look something like:
byte[,] bufferArray = new byte[imageWidth, imageHeight];
// 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
03-31-2009 01:57 PM
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
CWIMAQImage.ArrayToImage Array
Creates an image from an array.
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).
Array As Variant
The source array of image data.
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.
03-31-2009 02:48 PM
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
03-31-2009 02:54 PM
We have some MSVC examples in:
C:\Program Files\National Instruments\Vision\Examples\MSVC
Which is going to vary from acquisition to processing.
03-31-2009 03:06 PM
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
02-12-2015 12:38 AM
02-12-2015 06:47 AM
Thanks, William. That is a good link.
--Dave