Converting ByteArray to Char Array.

Hi.

I'm having some troubles when performing this conversion.

In my C# code I read a image to memory, a TIFF, then I convert this to a ByteArray.

After that I call a method from a managed class that I developed in C++/CLI, that receives a Byte Array and a int.

On my managed method I need to call a unmanaged funtion from a C header file, and that functions receives a Char *.

However, when I call the function the return is not waht I'm expecting, and I thing that there might be some problem with the data that I'm sending.

Can I peform this operation to pass the char * to my C function

pin_ptr<Byte> aux = &bufferTemp[0];

unsigned char * auxPtr = aux;

bufferTemp is a byte array and is stored on managed memory.

Thanks in advance for your help.

Best regards

 

Luis Filipe de Sousa



Answer this question

Converting ByteArray to Char Array.

  • Attu

    It looks OK, but maybe there is some bug in your implementation. Can you show mode details: C# call, C++/CLI function
  • MReinhardt

    Hi Alex,

    thanks for your fast answer, my C# code is actually very simple, it looks like this:

    MyReturn[] ret;

    MyWrapper aWrapper = new MyWrapper("1st param", "2nd param", "3rd para");

    Image img = Image.FromFile("C:\\Ft-00920464690007050927.tif");

    byte[] arrAux = ImageToByteArray(img);

    /* Bellow is the call to the C++/CLI functions */

    ret = aWrapper .Operation(arrAux, arrAux.Length);

    The code to conver the Image to a byte array that I used was something like this:

    MemoryStream ms = new MemoryStream();

    img.Save(ms, ImageFormat.Tiff);

    byte[] bmpBytes = ms.ToArray();

    img.Dispose();

    ms.Close();

    return bmpBytes;

    The signature of my C++/CLI method is:

    array<MyReturn^>^ MyWrapper::Operation(array<Byte>^ bufferTemp, int size){

    // And here comes the implementation of my method

    I have some calls to unmanaged methods, and all work as expected besides

    the one that need the char

    }

    Once again, thank you for your answer


  • Manuel5

    The problem is in your ImageToByteArray function. It makes byte array which contains the whole tif file, and not only image pixels. To get image pixels in byte array you need to use Bitmap.LockBits method which gives direct access to pixels array. Array structure depends on image type: 24 or 32 bpp color image, 8 bpp patette image etc. What is image type in your case You can detect this using Image.PixelFormat property.
  • Meff

    Hi again.

    This is a C++ forum I know that, however like Alex suggested I might have a bug somewhere on my code. I found it, and would like to share it with the rest of the users.

    Actually the bug was in my C# code, I cant use the function to convert the file to a byte array, and I cant read the file to memory using Image.FromFile.

    I need to read the file in binary mode, send it to a byte array, the code that I used was like this, just for test purposes:

    FileInfo f = new FileInfo("C:\\MyImage.tif");

    System.IO.BinaryReader br;

    System.IO.FileStream fs;

    fs = new System.IO.FileStream("C:\\Ft-00920464690007050927.tif",System.IO.FileMode.Open,System.IO.FileAccess.Read);

    br = new System.IO.BinaryReader(fs, System.Text.ASCIIEncoding.ASCII);

    byte[] arr = new byte[f.Length];

    br.Read(arr, 0,(int)(f.Length-1));

    Using this code to read the file that I need and invoking the functions works as expected.

    I placed here the post since this is the first time that I'm using C++ interop and thought that the problem was with some conversion that I was missing.

    I hope this can be usefull, once again thanks for the help.

    Best regards

    Luis Filipe de Sousa


  • Converting ByteArray to Char Array.