如何从托管 c++ 编组字节数组到 c#

发布于 2024-11-18 03:58:19 字数 303 浏览 3 评论 0原文

我想使用 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) 

提前谢谢您!

I'd like to use .Net Graphic functionality from C++ managed project. I'd like to create Windows.Drawing.Image (based on MemoryStream) from inmemory c++ array of bytes.

How can I make this piece of code work:

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

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰認得朕 2024-11-25 03:58:19

你可以这样做:

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);

You can do it something like that:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文