How to dereference a pointer as a structure
I've an IntPtr which points to the location where a Bitmap stores its raw image, i.e. not pointer to the bitmap object, but pointer to the image data itself (which I obtained through BitmapData.Scan0)
Now I've a structure like this
VBCode Code:
Structure RGB
B As Byte
G As Byte
R As Byte
End Structure
I need to be able to access the individual pixels (which I locate using the pointer) on the image as this RGB Structure.
In other words, it's like casting the pointer as a structure type. In C# it may be something like (RGB*) bmpptr;
Anyone know how?
Thanks in advance.
Re: How to dereference a pointer as a structure
Doesn't the MSDN help topic for the Scan0 property essentially show you how to do that? The code example it provides gets all the RGB data into a Byte array. You would then create multiple instances of your structure from the elements of that array.
Re: How to dereference a pointer as a structure
Thanks for your reply.
I know that method; that's precisely the method I want to avoid. I want to change it directly, right away, on the spot, with the help of a structure. This is why I want to dereference the pointer as a structure which I've defined. Copying to an array incurs a lot of overhead. My code runs in a tight loop so I hope to be able to change the data as fast as possible.
Re: How to dereference a pointer as a structure
I only hope VB.net has a native method to do this, and I would like to know what it is. I dont want to use Marshal class's WriteByte or the WriteInts, nor do I want to use FillMemory API because they incur tiny bits of overheads.
Unless, the JIT inlines the function call, which I'm not sure.
Re: How to dereference a pointer as a structure
Sounds to me like VB is the wrong language for this. I'd suggest writing a library in C# using unsafe code and then referencing that from your VB app, if that's an option.
Re: How to dereference a pointer as a structure
thanks jmcilhinney for the advice.:wave:
unfortunately I'm not using C#.
The fact that they all are .NET compliant leads me to think VB.NET might have something like that, too.
I guess I'll just drop the idea and look for alternatives.
Speed is still an issue with VB.net, it seems.:(
Re: How to dereference a pointer as a structure
VB.NET was never intended to support pointers. If wanted to do even medium-duty work with pointers in VB the code would get ugly very quickly. The language is simply not designed for it.