I have an object data type that I pass to a function. I can view the values returned in debug mode. But I can’t make use of this data in any way. I tried to read the data into another data structure and I have no success to date. These are the declarations and error message;
Declaration:
public object buffer;
byte[] myByte;
myByte=(byte[])buffer;
Error message:
Unable to cast object of type 'System.Byte[ * ]' to type 'System.Byte[]'.
The buffer in the debug mode (VS 2005) holds the follow sample data
myByte=(byte[])buffer
-buffer|{Dimensions:[1..9000]}
[1] 40
[2] 0
[3] 0
[4] 0
[5] 128
....
Would be happy for any help.

System.Byte[*]
JMartin
Sorry the out of this
textBox1.Text = myObject.GetType().ToString();
is System.Byte[ *] . i tried
byte[,] myByteArray = (byte[,])myObject;
and the error message was again
Unable to cast object of type 'System.Byte[ *]' to type 'System.Byte[,]'.
- Thx
llorrac
byte[,] myByteArray = (byte[,])myObject;
c o e s u r f
This is some information about the API i am using.
The document does not talk of the C# syntax.
void AxAxisMediaControl.GetCurrentImage(int theFormat, out object theBuffer, out int theBufferSize)
GetCurrentImage
Gets the data corresponding to the image currently being displayed.
Syntax
HRESULT GetCurrentImage(int theFormat, [C++] VARIANT* theBuffer, LONG* theBufferSize );GetCurrentImage(ByVal theFormat As Integer, [Visual Basic] ByRef theBuffer As Variant, ByRef theBufferSize As Long )Parameters
Return Value
Returns an HRESULT value. Possible values include the following:
Remarks
If the format specified is BMP, the image data buffer is returned as a byte array with a BITMAPINFOHEADER structure followed immediately by the bitmap data in RGB24 format. Note that this buffer would not constitute a valid .BMP file if saved to a file. A BITMAPFILEHEADER structure is also required. If your purpose is to save the image to a file use SaveCurrentImage instead. This method will generate a proper .BMP file.
Cleo HUANG - MSFT
Argh... stupid me... I did not notice the 1..9000 range from the output. That's a byte array with a lower bound of 1 instead of 0. That's why you cannot cast it to byte[].
Cast it to Array and use the CopyTo method of it to copy the array to a "normal" byte array. Note that you need to specify 1 for the index parameter of the copy to (or better, specify the value returned by the Array.GetLowerBound method).
Hope this helps altough given the fact that your docs say that the paramter must be a VARIANT I'm not very sure... give it a try.
clarkster82
object myObject = new byte[]
{
(byte)1, (byte)2, (byte)4, (byte)8,
(byte)16, (byte)32, (byte)64, (byte)128
};
byte[] myByteArray = (byte[])myObject;
Sir Hugo
I used the above and i had the same error message. I pass the object to an API function. The follow is my code;
object myObject = new byte[]
{
};
public int size;
API.GetCurrentImage(1, out myObject, out size);
byte
[] myByteArray = (byte[])myObject;myByteArray = (byte[])myObject; // this is where the error occurs
Greg Fiore
These are some sample output in the watch window:
Name Value Type
((byte[])(myobject))[1] 40 byte
((byte[])(myobject))[15] 24 byte
Hope this is enough information.
Regards
Bill Moline
myByte=(byte[])buffer
-buffer|{Dimensions:[1..9000]}
[1] 40"
Looks like your buffer is in fact a multidimensional byte array. You cannot cast it to byte[]. How do you create that buffer
robmnk
Hi Mike, it works.
byte[] myByte = new byte[size]; Array.Copy((Array)buffer, (Array)myByte, size);Thank you all for your help. I very much appreciate it.
Regards
BitBlast
i used the follow to get the type
textBox1.Text = myObject.GetType().ToString();
the content of the textbox is System.Byte
Regards
PRC
You can use the GetType mthod to lookup wish type myObject is after the GetCurrentImage method. Please lett us know the result!