[Gtk-sharp-list] From Gtk+ to Gtk#

Dan Winship danw@novell.com
Wed, 09 Mar 2005 09:44:41 -0500


On Wed, 2005-03-09 at 13:28 +0000, Alexandre Gomes wrote:
> Now, about the Color of pixels, it looks to me that isn't implemented
> yet. The property returns a System.IntPtr or (in C language) a int
> pointer.

Not quite. The name is a little confusing. IntPtr doesn't mean "int*",
it means "an integer type that is the same size as a void*" (on x86
Linux that would be a long). Since C# doesn't have pointer types
(outside of unsafe code), we have to cast the guchar* returned by
gdk_pixbuf_get_pixels to some integer type, and that's precisely what
IntPtr is for.

> The documentation states that this pointer points to the real
> data, so you can use it as you would do C - who knows a copy paste would
> work... lol. I've no experience in using C code from C# side, so I can't
> tell you for sure if you will not need to use the "unsafe" option in mcs
> to work out this pointer.

Right, the issue is that we *could* turn the data into a byte[], but
we'd have to copy all of the data, and then any time you changed it we'd
have to copy it all back again, and it wouldn't be very efficient. So if
you need to work with the raw pixel data, you have to use unsafe code.
Something like:

        unsafe {
                byte* pixel = (void*)pb.Pixels;
                
                // You should be able to just cut and paste that C code here I think
        }

-- Dan