gpt4 book ai didi

c++ - 如何将字节数组从托管 C++ 编码到 C#

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:15 31 4
gpt4 key购买 nike

我想使用 C++ 托管项目中的 .Net Graphic 功能。我想从内存中的 C++ 字节数组创建 Windows.Drawing.Image(基于 MemoryStream)。

我怎样才能使这段代码工作:

System::IO::Stream^ ms = gcnew System::IO::MemoryStream();
BYTE buf[1024 * 8]; // C++
int size; // C++
...
ms->Write(&buf, 0, size)

提前致谢!

最佳答案

你可以这样做:

int size = 1024;

BYTE buf[1024];

// unmanaged buffer
System::IntPtr intPtr = System::IntPtr( buf );

// managed buffer
array<unsigned char>^ managedBuf = gcnew array<unsigned char>(size);

{
// write things to buf[]
}

// copy unmanaged buffer to managed buffer
Marshal::Copy( intPtr, managedBuf, 0, size );

System::IO::Stream^ ms = gcnew System::IO::MemoryStream();

ms->Write( managedBuf, 0, size);

关于c++ - 如何将字节数组从托管 C++ 编码到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6517414/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com