[Gtk-sharp-list] Images as buttons

Rachel Hestilow rachel@nullenvoid.com
Mon, 18 Aug 2003 18:34:05 -0700


Ramin Zabih (rdz@cs.cornell.edu) wrote:

> I'm writing an image-processing application using gtk-sharp, hopefully to run under Linux and Windows. I'd like to have a table of image thumbnails, where the user can click on different thumbnails to select them and then process the selected images. My current idea is to create a table of buttons, where for each button b I call b.Add(image) with image a Gtk.Image (created from a Pixbuf). This seems to work, and by doing 
> 	b.Clicked += new EventHandler (callback);
> I can cause the function callback to be executed each time b is clicked.
> 
> My question is how to efficiently change the thumbnail when the user clicks on it. I can get my hands on the original Pixbuf from callback, but I would like to use unsafe code for efficiency, and I'd like to be able to modify the raw bytes.  Here is my current attempt, based on code that works in C# with Systems.Windows.Forms:
> 
>                  Gtk.Image image = (Gtk.Image)child;
>                  Gdk.Pixbuf pixbuf = image.Pixbuf;
>                  System.IntPtr ptr = pixbuf.Handle;

This is not the right property; it gives a pointer to the C GdkPixbuf*.
Unfortunately I don't think the right property is exposed to .NET.

>                  int stride = pixbuf.Rowstride;
>                  unsafe {
>                    byte *p = (byte *)(void *)ptr;
>                    int nWidth = pixbuf.Width*3;
>                    int nHeight = pixbuf.Height;
>                    int nOffset = stride - nWidth;
>                         
>                    for (int y = 0; y < nHeight;++y)
>                      {
>                        for (int x=0; x < nWidth; ++x )
>                          {
>                                 p[0] = (byte)(255-p[0]);
>                                 ++p;
>                           }
>                         p += nOffset;
>                       }
>                    }
> 
> Does anyone know the right way to do this? The code above does not work... Alternatively, is there an efficient way (i.e., without copying image arrays) to invoke a C DLL on the raw bytes?

Yes, you can call into DLLs using DllImport (see the Gtk# source code for 
examples). In C you could write:

void modify_pixbuf (GdkPixbuf *pixbuf) {
	... (not sure here, check the C docs)
}

and in C#:
[DllImport ("mydll")]
static extern void modify_pixbuf (IntPtr pixbuf);

which you could then call with:

modify_pixbuf (pixbuf.Handle);

Once you have modified the pixbuf you need to tell the Image you have done 
so. Unfortunately I cannot remember how to do this so you will need to 
check the docs.

> Thanks,
> Ramin
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list