I have a buffer (uint8[] of BGR pixel data) in C holding a video frame. A pointer to this buffer is passed back by the C code to C# code as an IntPtr. I require to add a text overlay to the each frame and then pass on a pointer to the frame for further processing. I believe what I need to do (in C#) is to copy each frame to a bitmap object, get the device context of the bitmap and use then use TextOut (etc) to write text to the bitmap. I would then copy the modified bitmap frame data back to my original array.
My question is twofold:
Thanks.
The fastest way is by not copying the data. That requires that your data is in a supported pixel format, BGR sounds a bit scary but odds are high it is actually PixelFormat.Format24bppRgb.
Which then allows you to use the Bitmap(int, int, int, PixelFormat, IntPtr constructor).
I can't comment on your approach, but the fastest way to copy data using two pointers would be to do a Platform Invoke call to the memcpy function in msvcrt.dll
Code example below, taken from the WriteableBitmapEx source
internal static class NativeMethods
{
internal static unsafe void CopyUnmanagedMemory(byte* srcPtr, int srcOffset,
byte* dstPtr, int dstOffset, int count)
{
srcPtr += srcOffset;
dstPtr += dstOffset;
memcpy(dstPtr, srcPtr, count);
}
// Win32 memory copy function
[DllImport("msvcrt.dll", EntryPoint = "memcpy",
CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
private static extern unsafe byte* memcpy(byte* dst, byte* src, int count);
}
To convert an IntPtr to byte* simply use
unsafe
{
IntPtr myPtr;
byte* bytePtr = (byte*)myPtr.ToPointer();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With