1

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!

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.