如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
How transferring an image to buffer in c++(dll) and then read/write in buffer in C#?
如何将图像传输到c++(dll(中的缓冲区,然后在c#中的缓冲区时读/写并实时返回到c++(.dll(?我正在寻找的过程如下:
1-首先,我从硬盘上读取图像;Mat inputImage=read("/,…./Test.jpg"(;
2-放入缓冲区:
imencode(".jpg",inputImage,inputBuff,paramBuffer(;
3-将表格c++发送到c#???(我不知道(。
4-从缓冲区读取c#???(我不知道(。
5-将通过c++和c#发生的更改写入缓冲区???(我不知道(。
我正在使用Opencv c++。
我真的很感谢你。
您可以在C#中使用System.Runtime.InteropServices
来调用外部库中的函数,例如
c++代码
extern "c"
{
__declspec(dllexport) void __cdecl Read(unsigned char*& buffer)
{
//allocate and write to buffer
}
__declspec(dllexport) void __cdecl Write(unsigned char* buffer)
{
//do something with the buffer
}
}
并将其编译为dll
C#代码
using System.Runtime.InteropServices;
class Main
{
[DllImport(DLL_FILE, CallingConvention = CallingConvention.Cdecl)]
private static extern void Read(out IntPtr buffer);
[DllImport(DLL_FILE, CallingConvention = CallingConvention.Cdecl)]
private static extern void Write(byte[] buffer);
public static void ReadFromExtern()
{
IntPtr bufferPtr = IntPtr.Zero;
Read(bufferPtr);
int length = LENGTH;
byte[] buffer = new byte[length];
Marshal.Copy(bufferPtr, buffer, 0, length);
//do something with buffer
}
public static void WriteToExtern(byte[] buffer)
{
Write(buffer);
//or do something else
}
}
注:
如果在c++代码中使用动态分配的内存,请记住还要编写一个包装函数来释放它,否则会导致内存泄漏。
__declspec(dllexport)
是特定于Windows的,用于将函数导出到dll。extern "C"
是为了避免c++编译器对名称的篡改,如果使用c编译器,则可以省略对于字符串传输,在c++中使用
char*
,在c#中使用System.Text.StringBuilder
相关文章:
- C++字符*缓冲区的大小
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- ostream过载时的缓冲区冲洗
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- Xaudio2在更改缓冲区或循环时弹出声音
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 将整个二进制文件读入缓冲区,然后以特定格式解析
- IStream COM 读取到缓冲区,然后通过 winsock 发送
- C - 分配一个未签名的炭缓冲区,然后用字符串填充
- 录制音频,存储在缓冲区中,然后通过PulseAudio将字节写入声卡
- 从C/ c++在内存中写入缓冲区,然后在没有JNI的情况下用java从它中读取
- 从文件中读取或将文件读入缓冲区,然后使用缓冲区(在c++中)
- Opengl:基于着色器的渲染到帧缓冲区,然后是固定管道渲染