Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I copy a block of memory?

How can I copy a block of memory?

Scheduled Pinned Locked Moved C#
questioncsharpdata-structuresperformance
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CopperCircle
    wrote on last edited by
    #1

    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...

    1. private void videoCogControl1_FrameReady(double SampleTime, IntPtr pBuffer,
    2.                                      int BufferSize, int Width, int Height)
      
    3.  {
      
    4.       unsafe
      
    5.       {
      
    6.            byte\* p = (byte\*)(void\*)pBuffer;
      
    7.            int nWidth = Width \* 3;
      
    8.            for (int y = 0; y < Height; y++)
      
    9.            {
      
    10.                 for (int x = 0; x < nWidth; x++)
      
    11.                 {
      
    12.                     p\[0\] = (byte)(255 - p\[0\]);
      
    13.                     p++;
      
    14.                 }
      
    15.            }
      
    16.        }   
      
    17.  }
      
    L realJSOPR C 3 Replies Last reply
    0
    • C CopperCircle

      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...

      1. private void videoCogControl1_FrameReady(double SampleTime, IntPtr pBuffer,
      2.                                      int BufferSize, int Width, int Height)
        
      3.  {
        
      4.       unsafe
        
      5.       {
        
      6.            byte\* p = (byte\*)(void\*)pBuffer;
        
      7.            int nWidth = Width \* 3;
        
      8.            for (int y = 0; y < Height; y++)
        
      9.            {
        
      10.                 for (int x = 0; x < nWidth; x++)
        
      11.                 {
        
      12.                     p\[0\] = (byte)(255 - p\[0\]);
        
      13.                     p++;
        
      14.                 }
        
      15.            }
        
      16.        }   
        
      17.  }
        
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Have you tried the Marshal.Copy[^] method from the interop namespace? regards

      C 1 Reply Last reply
      0
      • L Lost User

        Have you tried the Marshal.Copy[^] method from the interop namespace? regards

        C Offline
        C Offline
        CopperCircle
        wrote on last edited by
        #3

        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?

        G 1 Reply Last reply
        0
        • C CopperCircle

          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?

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          The documentation has. MSDN Library: Marshal.Copy method[^]

          Despite everything, the person most likely to be fooling you next is yourself.

          C 1 Reply Last reply
          0
          • G Guffa

            The documentation has. MSDN Library: Marshal.Copy method[^]

            Despite everything, the person most likely to be fooling you next is yourself.

            C Offline
            C Offline
            CopperCircle
            wrote on last edited by
            #5

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

            G 1 Reply Last reply
            0
            • C CopperCircle

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

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Use Marshal.Copy instead of Marshal.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.

              C 1 Reply Last reply
              0
              • G Guffa

                Use Marshal.Copy instead of Marshal.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.

                C Offline
                C Offline
                CopperCircle
                wrote on last edited by
                #7

                Thanks, I tried to use Marshal.Copy but I could not get it to work, could you please show how to implement it? Cheers,

                1 Reply Last reply
                0
                • C CopperCircle

                  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...

                  1. private void videoCogControl1_FrameReady(double SampleTime, IntPtr pBuffer,
                  2.                                      int BufferSize, int Width, int Height)
                    
                  3.  {
                    
                  4.       unsafe
                    
                  5.       {
                    
                  6.            byte\* p = (byte\*)(void\*)pBuffer;
                    
                  7.            int nWidth = Width \* 3;
                    
                  8.            for (int y = 0; y < Height; y++)
                    
                  9.            {
                    
                  10.                 for (int x = 0; x < nWidth; x++)
                    
                  11.                 {
                    
                  12.                     p\[0\] = (byte)(255 - p\[0\]);
                    
                  13.                     p++;
                    
                  14.                 }
                    
                  15.            }
                    
                  16.        }   
                    
                  17.  }
                    
                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • C CopperCircle

                    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...

                    1. private void videoCogControl1_FrameReady(double SampleTime, IntPtr pBuffer,
                    2.                                      int BufferSize, int Width, int Height)
                      
                    3.  {
                      
                    4.       unsafe
                      
                    5.       {
                      
                    6.            byte\* p = (byte\*)(void\*)pBuffer;
                      
                    7.            int nWidth = Width \* 3;
                      
                    8.            for (int y = 0; y < Height; y++)
                      
                    9.            {
                      
                    10.                 for (int x = 0; x < nWidth; x++)
                      
                    11.                 {
                      
                    12.                     p\[0\] = (byte)(255 - p\[0\]);
                      
                    13.                     p++;
                      
                    14.                 }
                      
                    15.            }
                      
                    16.        }   
                      
                    17.  }
                      
                    C Offline
                    C Offline
                    CopperCircle
                    wrote on last edited by
                    #9

                    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 memory

                    Thanks, for the help and hope this helps some one else.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups