使用命名管道将图像从C++发送到 C# 应用程序

Send image from C++ to C# application using Named Pipe

本文关键字:C++ 应用程序 图像 管道      更新时间:2023-10-16

我已经了解了命名管道,并尝试从C++和C#应用程序发送消息,它工作正常。但是我无法使用命名管道将图像从C++端发送到 C#。谁能告诉我我该怎么做。

命名管道的 C# 代码

static void Main(string[] args)
{
Console.WriteLine("Creating Client Pipe");
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
Console.WriteLine("Pipe Created Successfully, Connecting to Server");
pipe.Connect();
Console.WriteLine("Successfully, Connected to Server");
using (StreamReader rdr = new StreamReader(pipe, Encoding.Unicode))
{
System.Console.WriteLine("Message from Server: " + rdr.ReadToEnd());
}
Console.ReadKey();
}

C++代码

int _tmain(int argc, _TCHAR* argv[])
{
cout << "Server Creating Pipen";
HANDLE hPipe = ::CreateNamedPipe(_T("\\.\pipe\HyperPipe"),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
PIPE_UNLIMITED_INSTANCES,
4096,
4096,
0,
NULL);
cout << "Server Created Succesfully";
ConnectNamedPipe(hPipe, NULL);
LPTSTR data = _T("Hello");
cout << "Sending Message to Client";
DWORD bytesWritten = 0;
WriteFile(hPipe, data, _tcslen(data) * sizeof(TCHAR), &bytesWritten, NULL);
CloseHandle(hPipe);
return 0;
}

我想要的是当我在 C# 端输入路径时,它会将其发送到C++并使用 OpenCV 库,我将从内存中获取该图像并发送到 C#。

提前致谢

您是否尝试过将图像转换为字节数组,然后以与文本相同的方式发送它?