How can I copy a block of memory?
-
Hi, I am trying to use the third party VideoCog componet with C# and I need to copy a image frame? Videocog drops the frame into an rgb array in memory and I can then fast process the pixels using pointers. How can I create another copy of the frame from memory so that I can also access that via another pointer? Thanks...
- private void videoCogControl1_FrameReady(double SampleTime, IntPtr pBuffer,
-
int BufferSize, int Width, int Height)
-
{
-
unsafe
-
{
-
byte\* p = (byte\*)(void\*)pBuffer;
-
int nWidth = Width \* 3;
-
for (int y = 0; y < Height; y++)
-
{
-
for (int x = 0; x < nWidth; x++)
-
{
-
p\[0\] = (byte)(255 - p\[0\]);
-
p++;
-
}
-
}
-
}
-
}
-
Hi, I am trying to use the third party VideoCog componet with C# and I need to copy a image frame? Videocog drops the frame into an rgb array in memory and I can then fast process the pixels using pointers. How can I create another copy of the frame from memory so that I can also access that via another pointer? Thanks...
- private void videoCogControl1_FrameReady(double SampleTime, IntPtr pBuffer,
-
int BufferSize, int Width, int Height)
-
{
-
unsafe
-
{
-
byte\* p = (byte\*)(void\*)pBuffer;
-
int nWidth = Width \* 3;
-
for (int y = 0; y < Height; y++)
-
{
-
for (int x = 0; x < nWidth; x++)
-
{
-
p\[0\] = (byte)(255 - p\[0\]);
-
p++;
-
}
-
}
-
}
-
}
-
Have you tried the Marshal.Copy[^] method from the interop namespace? regards
Thanks for the reply, I am very new to C# and I am just working off the Videocog tutorials, do you have an example of how Marshal.Copy works?
-
Thanks for the reply, I am very new to C# and I am just working off the Videocog tutorials, do you have an example of how Marshal.Copy works?
-
The documentation has. MSDN Library: Marshal.Copy method[^]
Despite everything, the person most likely to be fooling you next is yourself.
Thanks, I got it to work, but there must be an faster/easyer way to copy a block of memory? This is what I did to get it to work with Marshal.ReadByte, it gives me p2 which I can use on the copied image:
byte[] bytes = new byte[BufferSize];
for (int i = 0; i < BufferSize; i++)
bytes[i] = Marshal.ReadByte(pBuffer, i);fixed (byte* pTmp = bytes)
{
System.IntPtr pBuffer2 = new IntPtr((void*)pTmp);
byte* p2 = (byte*)(void*)pBuffer2;
} -
Thanks, I got it to work, but there must be an faster/easyer way to copy a block of memory? This is what I did to get it to work with Marshal.ReadByte, it gives me p2 which I can use on the copied image:
byte[] bytes = new byte[BufferSize];
for (int i = 0; i < BufferSize; i++)
bytes[i] = Marshal.ReadByte(pBuffer, i);fixed (byte* pTmp = bytes)
{
System.IntPtr pBuffer2 = new IntPtr((void*)pTmp);
byte* p2 = (byte*)(void*)pBuffer2;
}Use
Marshal.Copy
instead ofMarshal.ReadByte
. Now you are making a method call for every byte that you copy, that is bound to be slow. Why are you converting the pointer to an IntPtr value, then back to a pointer? Just copy the pointer:fixed (byte* pTmp = bytes) {
byte* p2 = pTmp;
}Despite everything, the person most likely to be fooling you next is yourself.
-
Use
Marshal.Copy
instead ofMarshal.ReadByte
. Now you are making a method call for every byte that you copy, that is bound to be slow. Why are you converting the pointer to an IntPtr value, then back to a pointer? Just copy the pointer:fixed (byte* pTmp = bytes) {
byte* p2 = pTmp;
}Despite everything, the person most likely to be fooling you next is yourself.
Thanks, I tried to use Marshal.Copy but I could not get it to work, could you please show how to implement it? Cheers,
-
Hi, I am trying to use the third party VideoCog componet with C# and I need to copy a image frame? Videocog drops the frame into an rgb array in memory and I can then fast process the pixels using pointers. How can I create another copy of the frame from memory so that I can also access that via another pointer? Thanks...
- private void videoCogControl1_FrameReady(double SampleTime, IntPtr pBuffer,
-
int BufferSize, int Width, int Height)
-
{
-
unsafe
-
{
-
byte\* p = (byte\*)(void\*)pBuffer;
-
int nWidth = Width \* 3;
-
for (int y = 0; y < Height; y++)
-
{
-
for (int x = 0; x < nWidth; x++)
-
{
-
p\[0\] = (byte)(255 - p\[0\]);
-
p++;
-
}
-
}
-
}
-
}
Have you tried the C++ forum?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hi, I am trying to use the third party VideoCog componet with C# and I need to copy a image frame? Videocog drops the frame into an rgb array in memory and I can then fast process the pixels using pointers. How can I create another copy of the frame from memory so that I can also access that via another pointer? Thanks...
- private void videoCogControl1_FrameReady(double SampleTime, IntPtr pBuffer,
-
int BufferSize, int Width, int Height)
-
{
-
unsafe
-
{
-
byte\* p = (byte\*)(void\*)pBuffer;
-
int nWidth = Width \* 3;
-
for (int y = 0; y < Height; y++)
-
{
-
for (int x = 0; x < nWidth; x++)
-
{
-
p\[0\] = (byte)(255 - p\[0\]);
-
p++;
-
}
-
}
-
}
-
}
Hi, I found a very quick method in the end using RtlMoveMemory:
[DllImport("kernel32")]
public static extern void RtlMoveMemory(IntPtr dest, IntPtr src, int len);System.IntPtr pBuffer2 = Marshal.AllocHGlobal(BufferSize); //Assign unmanaged memory
RtlMoveMemory(pBuffer2, pBuffer, BufferSize); //Copy memory
byte* p2 = (byte*)(void*)pBuffer2;
// Use p2 to access image pixels
Marshal.FreeHGlobal(pBuffer2); //Free unmanaged memoryThanks, for the help and hope this helps some one else.