locked
Convert Raw memory pointer to Bitmap/Graphics object ( unsafe code ) RRS feed

  • Question

  • Hi,

    not sure about the best place for this question, but I'll try it here first.

    I have a raw memory array with a IntPtr encapsulating a underlying void* from a C++ application using C++/CLI to a access a C++ lib. The pointer is hold in an IntPtr in my C# application in an unsafe context.

    I now want to attach this pointer to a Bitmap/Image or whatever object to be able to use the Graphics functions for drawing to the buffer.

    Is there a direct way to initalise a Bitmap or something a Bitmap object can use with this raw memory, to avoid unnecessary copy operations ?

    Thanks,
                        Florian






    Wednesday, July 25, 2007 8:03 AM

Answers

  • Did you want a bitmap with direct access to the bytes?  I use this constructor:

    public:
    Bitmap (
    	int width, 
    	int height, 
    	int stride, 
    	PixelFormat format, 
    	IntPtr scan0
    )
    Wednesday, July 25, 2007 9:41 AM

All replies

  • One of the bitmap constructors takes an IntPtr to Scan0.  Set Scan0 to your memory array.
    Wednesday, July 25, 2007 8:58 AM
  • Hello John,

    hmm, I looked for that but didn't found any constructor which achieves this (.Net 2.0). Alternatively I remembered the BitmapData object, which has similar functions. Did you mean that ?

    Bye,
                 Florian

    Wednesday, July 25, 2007 9:11 AM
  • Did you want a bitmap with direct access to the bytes?  I use this constructor:

    public:
    Bitmap (
    	int width, 
    	int height, 
    	int stride, 
    	PixelFormat format, 
    	IntPtr scan0
    )
    Wednesday, July 25, 2007 9:41 AM
  • Hmm, interesting, I didn't found this first in the C# documentation... but it's working. Thanks for your help.

    Bye,
                Florian
    Wednesday, July 25, 2007 10:05 AM
  • You should check to make sure you have the .NET Framework Class Library documentation.  If you show the index in Help and search for bitmap, you should find the Bitmap class with it's Constructor members.  You'll really need this documentation to program in C#.
    Wednesday, July 25, 2007 12:03 PM