I’ve got to interface my C++ project with a C# project and send an image to it across a named pipe. OpenCV stores matrix data in a contiguous chunk, starting at uchar* Mat::data. However, to send data across a named pipe, it must be a char*, so I just do a cast, not sure what else I’m supposed to do. It shouldn’t matter, it’s just data, not characters.
size_t s = frame2.elemSize() * frame2.rows * frame2.cols;
sendChars(hPipe, (char*)frame2.data, s);
On the C# side I read in the block of data to a char[] buffer. I then create a new Bitmap with the appropriate width, height, etc. and set the IntPtr to the beginning of the char[] buffer.
//in the C# forms private scope:
char[] buffer = new char[921600];
and then in a StartServer() function:
pipeServer = new NamedPipeServerStream("SamplePipe", PipeDirection.InOut);
//blah blah blah
using (StreamReader sr = new StreamReader(pipeServer))
{
sr.ReadBlock(buffer, 0, buffer.Length);
unsafe
{
fixed (char* ptr = buffer)
{
using (Bitmap image = new Bitmap(640, 480, 640*3, PixelFormat.Format24bppRgb, new IntPtr(ptr)))
{
pictureBox1.Image = image;
image.Save("test.png");
}
}
}
}
The result of this is a big red X on the C# form and the saved image looks garbled… but not just random noise. So the data’s coming through and getting messed up either on the C++ end before transmission or the C# end on interpretation. I have tried to work around this by using the Bitmap constructor that reads a stream and sending an encoded image instead of the raw pixels, but that fails harder than the above method (just gives an error, no output).

How do I transfer uchar* array with pixel data from C++ to C# and then reconstruct it inside a Bitmap so that I can display it on the form?
The use of a
StreamReaderhere seems like the wrong choice if what you’re sending is actually binary data. TheStreamReaderis used for reading text, and will decode the data to characters with some encoding which in your case would be auto-detected (and definitely wrong).You want to use the
BinaryReaderfor reading binary data, or read directly from theNamedPipeServerStreamusing theRead()method to get abyte[]array. Acharin C# is used for storing characters, and is unicode, not a single byte.