Displaying Camera Image in C# Dashboard

Dear Cheif Delphi Community,

I know this is a C++ forum, but I dont know anywhere else to post this, but either ways, I am trying to display the camera video on a custom C# form. So far, I have managed to make the the form connect to the camera on the cRIO, and understand that there is data, it even shows the maount of bytes read, ranging from 4(header and size) to 9000(I dont know), this is my current code:

if (netStream.DataAvailable)
                {
                    image.indicator.Text = "Data Available";
                    //image.videoStream.Image = Image.FromStream(netStream);
                    data = new byte[serverClient.ReceiveBufferSize];
                    
                    bytesRead = netStream.Read(data, 0, serverClient.ReceiveBufferSize);
                    //tw.WriteLine(DateTime.Now + " " + bytesRead.ToString());
                    if (bytesRead > 100)
                    {
                        try
                        {
                            MemoryStream ms = new MemoryStream(data);
                            image.videoStream.Image = Image.FromStream(ms, false, true);
                        }
                        catch (Exception ex)
                        {
                            tw.WriteLine(DateTime.Now + ex.ToString());
                        }
                    }
                }
                else if (!netStream.DataAvailable)
                {
                    image.indicator.Text = "No Data Available";
                }

Whoever knows C#, the Image.FromStream() method keeps returning a Parameter is Invalid in the excetion. Obviously I think that data(the byte] array that stores the image data] is corrupt or something, I do not know though. Can anyone please show me some proper code here for displaying the image, btw I know the program is connected because the status label does read Data Available.

the data is not an image stream like the one .Net is expecting. I am not sure what the encoding is, but you will need to (I think) capture the data one frame at a time (yes, manually) and convert that to a .Net Image

That is what it is doing, capture ONE image at a time and displaying it, and netStream is not a .NET Image Stream, it is a network stream, and it reads data from a socket, the only thing remotely related or used for creating the image is data(byte]) that stores the image data in byte form, MemoryStream ms, which is used to store data and then get the Image form the stream, Image.FromStream(ms); . Like I said before, it also shows that there is data available on the stream, it is just the stupid Image.FromStream() keeps returning an Parameter is Invalid in its exception. I dont know what to do at this point.

I’d recommend looking at the LabVIEW dashboard code, and see if it happens to mention what kind of image its expecting.

From looking at the PCVideoServer source code, it looks like there is a header with the four bytes 0x01, 0x00, 0x00, 0x00, followed by 4 more bytes representing the size of the image as a 32-bit integer. The image is sent as a JPEG.


success = AxisCamera::GetInstance().CopyJPEG(&imageData, numBytes, imageDataSize);
...
// Write header to PC
static const char header[4]={1,0,0,0};
int headerSend = write(newPCSock, const_cast<char*>(header), 4);
// Write image length to PC
int lengthSend = write(newPCSock, reinterpret_cast<char*>(&numBytes), 4);
// Write image to PC
int sent = write (newPCSock, imageData, numBytes);

It would seem to me that you’re passing in those header bytes into Image.FromStream() and that’s why it isn’t working. Try to use the header information to your advantage. It will mark when an image begins and its length.

The Dashboard Code from LabVIEW doesnt help at all :D, ive read through the whole thing countless times, the PCVideoServer.cpp I have also read through that code,infact I am sitting right in front of it lol, that is why there is a if(readBytes > 1000) code, it makes sure I am not sending the header or imageSize as parameters to the MemoryStream, actually after much testing, i got the image to display at about .01 fps rofl, it updates the image irregularly and it is lagging BEYONG belief, here is my current code for that image processing:


if (netStream.DataAvailable)
                {
                    image.indicator.Text = "Data Available";
                    //image.videoStream.Image = Image.FromStream(netStream);
                    data = new byte[serverClient.ReceiveBufferSize];
                    
                    //bytesRead = netStream.Read(data, 0, serverClient.ReceiveBufferSize);
                    bytesRead = client.Receive(data, 0, serverClient.ReceiveBufferSize, 0);
                    //tw.WriteLine(DateTime.Now + " " + bytesRead.ToString());
                    if (bytesRead > 1000)
                    {
                        try
                        {
                            MemoryStream ms = new MemoryStream(data);
                            image.videoStream.Image = Image.FromStream(ms, false, true);
                        }
                        catch (Exception ex)
                        {
                            tw.WriteLine(DateTime.Now + ex.ToString());
                        }
                    }
                }

The problem is that you’re just randomly grabbing chunks of data > 1000 bytes in length, throwing it into an image, and hoping that everything lines up correctly.

I would recommend reading 8 bytes first, and look for the 0x01,0x00,0x00,x00 header in the first 4. If that’s there, read the length from the next 4 bytes. Then, read in that number of bytes from your networkstream, and pass it into Image.FromStream(). Repeat.

What I meant was that the data you are giving image.fromstream is NOT in the format it accepts.
ex:
asdfghjkl is the image data it expects, and you are sending it something else, like 10000009asdfghjkl or a different encoding (but it should be fine if it is a JPEG)

I hope you dont mind me asking, but can you give me the example for the code for that, becuase from what I have logged, as in BYTES READ, the amount varies, this what an example would look like, this is from my log file:


2/9/2010 4:53:35 AM StartStream()
2/9/2010 4:53:35 AM Connecting!
2/9/2010 4:53:35 AM Connected
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 7867
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8009
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8039
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8003
2/9/2010 4:53:36 AM 4384
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 3649
2/9/2010 4:53:36 AM 4
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 7995
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 2032
2/9/2010 4:53:36 AM 6001
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8040
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 6412
2/9/2010 4:53:36 AM 1620
2/9/2010 4:53:36 AM 4384
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 3657
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8041
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8061
2/9/2010 4:53:36 AM 4
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8040
2/9/2010 4:53:36 AM 4384
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 3631
2/9/2010 4:53:36 AM 4
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 7984
2/9/2010 4:53:36 AM 4
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8011
2/9/2010 4:53:36 AM 4
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8033
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8057
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8062
2/9/2010 4:53:36 AM 4
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8084
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 4948
2/9/2010 4:53:36 AM 3128
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8073
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8037
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 8025
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 572
2/9/2010 4:53:36 AM 4380
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 568
2/9/2010 4:53:36 AM 8192
2/9/2010 4:53:36 AM 2329
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 8000
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7988
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 8002
2/9/2010 4:53:37 AM 4
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 8000
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 8026
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 8017
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 4952
2/9/2010 4:53:37 AM 3053
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 8009
2/9/2010 4:53:37 AM 4384
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 3589
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 572
2/9/2010 4:53:37 AM 7400
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7926
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 2032
2/9/2010 4:53:37 AM 5880
2/9/2010 4:53:37 AM 4
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7911
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7954
2/9/2010 4:53:37 AM 4
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7905
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 6412
2/9/2010 4:53:37 AM 1515
2/9/2010 4:53:37 AM 5785
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 2108
2/9/2010 4:53:37 AM 4
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7842
2/9/2010 4:53:37 AM 4384
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 3438
2/9/2010 4:53:37 AM 4
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7828
2/9/2010 4:53:37 AM 4380
2/9/2010 4:53:37 AM 4380
2/9/2010 4:53:37 AM 7300
2/9/2010 4:53:37 AM 4384
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 3361
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7676
2/9/2010 4:53:37 AM 8
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7656
2/9/2010 4:53:37 AM 4384
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 3187
2/9/2010 4:53:37 AM 4
2/9/2010 4:53:37 AM 8192
2/9/2010 4:53:37 AM 7611
2/9/2010 4:53:38 AM 4
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7574
2/9/2010 4:53:38 AM 4380
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 3255
2/9/2010 4:53:38 AM 4384
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 3259
2/9/2010 4:53:38 AM 4384
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 3321
2/9/2010 4:53:38 AM 4384
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 2028
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 747
2/9/2010 4:53:38 AM 4384
2/9/2010 4:53:38 AM 4380
2/9/2010 4:53:38 AM 7229
2/9/2010 4:53:38 AM 4
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7908
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7895
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 572
2/9/2010 4:53:38 AM 7201
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 2032
2/9/2010 4:53:38 AM 4380
2/9/2010 4:53:38 AM 1305
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7646
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7553
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7481
2/9/2010 4:53:38 AM 4
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7335
2/9/2010 4:53:38 AM 4384
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 2860
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7272
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 4952
2/9/2010 4:53:38 AM 2333
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7233
2/9/2010 4:53:38 AM 4
2/9/2010 4:53:38 AM 8192
2/9/2010 4:53:38 AM 7198
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 7137
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 685
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 3488
2/9/2010 4:53:39 AM 5840
2/9/2010 4:53:39 AM 1814
2/9/2010 4:53:39 AM 4380
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 1766
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 568
2/9/2010 4:53:39 AM 5664
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 6239
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 6213
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 6330
2/9/2010 4:53:39 AM 4384
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 2058
2/9/2010 4:53:39 AM 4380
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 2055
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 4380
2/9/2010 4:53:39 AM 4380
2/9/2010 4:53:39 AM 5916
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 2032
2/9/2010 4:53:39 AM 4522
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 6799
2/9/2010 4:53:39 AM 4384
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 2769
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 7424
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 7716
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 7880
2/9/2010 4:53:39 AM 4384
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 3548
2/9/2010 4:53:39 AM 4384
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 3523
2/9/2010 4:53:39 AM 4384
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 3544
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 7927
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 7917
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 7926
2/9/2010 4:53:39 AM 4
2/9/2010 4:53:39 AM 4380
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 3526
2/9/2010 4:53:39 AM 8192
2/9/2010 4:53:39 AM 6408
2/9/2010 4:53:40 AM 4380
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 5071
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 6408
2/9/2010 4:53:40 AM 1520
2/9/2010 4:53:40 AM 4
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 4948
2/9/2010 4:53:40 AM 2999
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 7915
2/9/2010 4:53:40 AM 4384
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 3566
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 7934
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 7942
2/9/2010 4:53:40 AM 4
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 7949
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 7984
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 2032
2/9/2010 4:53:40 AM 4380
2/9/2010 4:53:40 AM 1543
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 7942
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 4952
2/9/2010 4:53:40 AM 3031
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 3488
2/9/2010 4:53:40 AM 4464
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 4948
2/9/2010 4:53:40 AM 3016
2/9/2010 4:53:40 AM 4380
2/9/2010 4:53:40 AM 4380
2/9/2010 4:53:40 AM 7416
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 7957
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 3492
2/9/2010 4:53:40 AM 4488
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 4948
2/9/2010 4:53:40 AM 3051
2/9/2010 4:53:40 AM 8192
2/9/2010 4:53:40 AM 2028
2/9/2010 4:53:40 AM 5926
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7974
2/9/2010 4:53:41 AM 8
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7734
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 3492
2/9/2010 4:53:41 AM 4510
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7868
2/9/2010 4:53:41 AM 90
2/9/2010 4:53:41 AM 4380
2/9/2010 4:53:41 AM 4380
2/9/2010 4:53:41 AM 7442
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 4948
2/9/2010 4:53:41 AM 2989
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7868
2/9/2010 4:53:41 AM 135
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7868
2/9/2010 4:53:41 AM 101
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 8013
2/9/2010 4:53:41 AM 4
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7992
2/9/2010 4:53:41 AM 4
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 2028
2/9/2010 4:53:41 AM 4380
2/9/2010 4:53:41 AM 1558
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7991
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7956
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7970
2/9/2010 4:53:41 AM 4
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7978
2/9/2010 4:53:41 AM 4
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7976
2/9/2010 4:53:41 AM 4
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 6408
2/9/2010 4:53:41 AM 1576
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7970
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 2032
2/9/2010 4:53:41 AM 5905
2/9/2010 4:53:41 AM 4
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7977
2/9/2010 4:53:41 AM 4
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7973
2/9/2010 4:53:41 AM 4384
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 3563
2/9/2010 4:53:41 AM 4
2/9/2010 4:53:41 AM 8192
2/9/2010 4:53:41 AM 7962
2/9/2010 4:53:42 AM 4384
2/9/2010 4:53:42 AM 8192
2/9/2010 4:53:42 AM 3590
2/9/2010 4:53:42 AM 8192
2/9/2010 4:53:42 AM 572
2/9/2010 4:53:42 AM 7396
2/9/2010 4:53:42 AM 8192
2/9/2010 4:53:42 AM 8017
2/9/2010 4:53:42 AM 4384
2/9/2010 4:53:42 AM 8192
2/9/2010 4:53:42 AM 3634

As you can see, there is no consistency to where the 4 is. Also , these numbers are the amount of bytes read

depending on the image, the size varies, because JPEG is a compressed format
for example, a pure black image will be much smaller than a picture where every pixel is a different color
Edit: look at the attachment sizes

test.jpg
plasma.jpg


test.jpg
plasma.jpg

Ok, even with a code that just read 8 bytes at a time, not a single string that I printed into the log had a NUMBER or a 0x00 or whatever the header was, they were all messed up jumbles of characters. I honestly am stuck here, as the image displays once and then doesnot update for anotehr few minutes, then updates, few minutes, one more frame, few minutes, one more frame etc. on and on.

look at it in a byte array, you should see a 1 then 0 0 0 etc…
in a string, this would be a box followed by \0\0\0 (but only in the debugger, output is cut by \0

I did :d, I went through the entire output, I simple just wrote the entire byte array as a string into the log file, there was no 1\0\0\0 in the whole log file.

I also looked for \0. I found one of those in every output, as expected, but not in the beginning of the byte array, they were found near the middle of each one. Also only a few of them were followed by a number like 5, or 4, or 7.