LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sending Image from LabVIEW to C# PictureBox

Solved!
Go to solution

Hi. I am trying to create TCP communication between LabVIEW and C# program. In C# (Windows Application Form) program, I am getting a string for path of the image. This part works well. Also I am able to send string constant and it is visible in C# massage box. Therefore, tools are enough to communicate 2 of them. I am sending string as a path to LV, LV opens image and make it grayscale image and I would like to convert it 2D array to send C# program. But in this case I cannot see data flow when I probe the wire. Has anyone tried this? After sending this array to C#, I would like to convert it to image to print in PictureBox.

My C# program is below and the Vi has been attached.

 

Code:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;

namespace TCPwithLV
{
public partial class Form1 : Form
{
static int port = 6000;
static string server = "127.0.0.1";

public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
const int byteSize = 1024;
const int imgSize = 1024;
try
{
TcpClient client = new TcpClient(server, port);
NetworkStream stream = client.GetStream();
byte[] msg = new byte[byteSize];
//int bytesRead = stream.Read(msg, 0, msg.Length);

////textBox1.Text = Encoding.ASCII.GetString(msg, 0, bytesRead);
//MessageBox.Show(Encoding.ASCII.GetString(msg, 0, bytesRead));

string path = textBox1.Text;
byte[] pathByte = new byte[1024];
pathByte = Encoding.ASCII.GetBytes(path);
stream.Write(pathByte, 0, pathByte.Length);
MessageBox.Show("Data has been sent...");
//stream.Close();
//client.Close();

byte[] img = new byte[imgSize];
int imgRead = stream.Read(img, 0, img.Length);
MessageBox.Show("Reading has finished...");
MessageBox.Show(Encoding.ASCII.GetString(img, 0, imgRead));
byte[] byteArray = Encoding.UTF8.GetBytes(img.ToString());
//MemoryStream st = new MemoryStream(byteArray);
Image ret = byteArrayToImage(byteArray);
pictureBox1.Image = ret;
}
catch (Exception c)
{
MessageBox.Show(c.ToString());
}
}

0 Kudos
Message 1 of 3
(2,029 Views)
Solution
Accepted by topic author oguzk

A LabVIEW IMAQ image is NOT a .Net image. You have to create a .Net image in LabVIEW explicitedly, initialize it to the right parameter such as width, height and pixelformat, after getting those data from the IMAQ image, then retrieve the pixmap data from the IMAQ image, massage the data for any inconsistencies between pixel format and row padding between IMAQ and .Net and last but not least assign this massaged data to the .Net image.

If this sounds complicated then you are right. Image manipulation is hard work and there are more formats and various implementations out there than programming languages and each has its own specifics.

 

Which of course gets us to the all important question: Why o why are you trying to make your life so complicated? Why not load that darn picture right in .Net when you want to display it there anyhow?

 

You tried it before with a LabVIEW DLL. But by replacing the DLL with a TCP connection you did not solve the problem at all. The data from the IMAQ image is not automagically translated in memory to a stream of bytes that the .Net image functions understand. .Net expects a stream of bytes that matches a number of very specific formats, which all include a header information that tells it what data follows, what dimension and pixel format the data contains. IMAQ Visions bytestream format is different unless you use an explicit function to write it to one of the available file formats.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 2 of 3
(2,012 Views)

Thanks for reply. Yes, I will probably save this image by using Image Write function in Labview and I will open this file in C#. As you told other method is more complicated.

0 Kudos
Message 3 of 3
(2,007 Views)