none
System.Byte[*] RRS feed

  • Question

  • Dear All,

    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.

    Saturday, March 11, 2006 11:01 AM

Answers

  • 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.

     

    Saturday, March 11, 2006 2:56 PM
    Moderator

All replies

  • How do you initialize the byte array? I this should work:


    object myObject = new byte[]
    {
        (byte)1,    (byte)2,    (byte)4,    (byte)8,
        (byte)16,   (byte)32,   (byte)64,   (byte)128
    };

    byte[] myByteArray = (byte[])myObject;

     

    Saturday, March 11, 2006 11:13 AM
    Moderator
  • 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[]

    {

    (byte)1, (byte)2, (byte)4, (byte)8,

    (byte)16, (byte)32, (byte)64, (byte)128

    };

    public int size;

    API.GetCurrentImage(1, out myObject, out size);

    byte[] myByteArray = (byte[])myObject;

    myByteArray = (byte[])myObject; // this is where the error occurs

    Saturday, March 11, 2006 11:53 AM
  • Then the GetCurrentImage method changed the myObject variable by initializing it with an other type.

    You can use the GetType mthod to lookup wish type myObject is after the GetCurrentImage method. Please lett us know the result!
    Saturday, March 11, 2006 11:59 AM
    Moderator
  • i used the follow to get the type

    textBox1.Text = myObject.GetType().ToString();

    the content of the textbox is System.Byte

  • ;

    Regards

Saturday, March 11, 2006 12:09 PM
  • "

    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 ?

     

    Saturday, March 11, 2006 12:24 PM
    Moderator
  • I don't understand you code output. But when it is a multidimensional array you can cast this to an multidimensional array.


    byte[,] myByteArray = (byte[,])myObject;
     

    Saturday, March 11, 2006 12:49 PM
    Moderator
  • 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

    Saturday, March 11, 2006 1:44 PM
  • 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)

    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

    theFormat
    Identifier specifiying the format of the image data. Values available are: 0 = JPEG, 1 = BMP
    theBuffer
    The buffer where the image data is returned.
    theBufferSize
    Size of the image data buffer returned.

    Return Value

    Returns an HRESULT value. Possible values include the following:

    Value Description
    S_OK Successful execution.
    S_FALSE The operation was not performed because no current image was available (e.g., stream stopped).
    E_FAIL Unexpected failure.
    E_INVALIDARG Invalid parameter. E.g., unrecognized format.
    E_POINTER An output parameter was not provided.

    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.

    Saturday, March 11, 2006 2:16 PM
  • 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

    Saturday, March 11, 2006 2:50 PM
  • 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.

     

    Saturday, March 11, 2006 2:56 PM
    Moderator
  • 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

    Saturday, March 11, 2006 4:50 PM
  • Worrior,


    i am also dealing with axis GetCurrentImage method i was not able to convert byte[] array to bitmap image ? can you please send me the code ? thanks in advance..
    jhyyggjhgh
    Friday, January 2, 2009 7:44 AM
  • I had the same problem and after googling a lot, came up with this:

     

    axisVideoDisplay.GetCurrentImage(1, out imageBuffer, out imageBufferSize);

    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, imageBuffer);
    byte[] buffer = ms.ToArray();

     

    'buffer' is now your byte array that can be accessed and manipulated. imageBuffer is an object, axisVideoDisplay is the toolbox control of type AxAxisMediaControl.

     

    Btw, where can you find the API documentation for all this? I am just using the object library to look up functions and arguments. Would be helpful if I came across some documentation.

    • Proposed as answer by shuvozula Wednesday, April 28, 2010 3:43 AM
    Wednesday, April 28, 2010 3:42 AM