IntPtr to byte[] without copy

This is a discussion on IntPtr to byte[] without copy within the C# Programming forums, part of the General Programming Boards category; let's say I have an IntPtr that points to the raw data of System.Drawing.Bitmap. is there any way to create ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,300

    IntPtr to byte[] without copy

    let's say I have an IntPtr that points to the raw data of System.Drawing.Bitmap. is there any way to create a byte array from that IntPtr without copying the data? I'm a pretty experienced C++ programmer, so I can call ToPointer() on it and convert to a byte* to work with it as a pointer, which is no big deal for me, but using a pointer and doing pointer arithmetic increases the risk of bugs, so I'd prefer not to do it that way if there's another way.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,181
    I admit, I do my very best to avoid C#, but isn't this exact situation the only reason the `Marshal' class has those copy interfaces?

    Soma

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,300
    that's certainly true, but I'm trying to write a pure-C# implementation of a video codec, which needs to run pretty fast (near real time). it seems to me that copying the array would cause a pretty significant performance hit. dealing with a raw pointer isn't especially dangerous in this case, because the only thing I'm doing with the data is to find the average value of all the bytes (crude average brightness), but if I can avoid using raw pointers in C#, that would be preferred.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,181
    O_o

    I can't help but think you are going about it the wrong way. Depending on the color space and representation that isn't just crude but horribly wrong or pointlessly expensive.

    If you can tell me, why are you "guestimating" the brightness? Are you estimating the brightness so that you might better decide which filters to use for processing? Planning to reduce average brightness for eventual movement estimation?

    I don't know C# well enough to know if it is an option, but calculating a packed average is a thing that can be done with "SSE" or friends.

    *shrug*

    Well, anyway, I obviously can't help with the C# so I'll stop talking.

    Soma

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,300
    unfortunately, SSE isn't a real option from within a pure C# program, unless the JIT compiler is smart enough to use SSE for certain types of loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need loop to copy chunks from byte array
    By jjamjatra in forum C# Programming
    Replies: 2
    Last Post: 03-05-2009, 04:42 AM
  2. Marshall HWND to IntPtr
    By Opariti in forum Windows Programming
    Replies: 0
    Last Post: 12-17-2008, 07:32 AM
  3. IntPtr
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-19-2008, 06:18 AM
  4. Copy structure elements into byte array
    By irncty99 in forum C Programming
    Replies: 4
    Last Post: 03-15-2005, 12:58 PM
  5. Byte array to IntPtr : Safely
    By PaulStat in forum C# Programming
    Replies: 0
    Last Post: 08-12-2003, 10:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21