[Gtk-sharp-list] Pixbuf's Pixels property.

Miguel de Icaza miguel@ximian.com
30 Apr 2003 21:58:04 -0400


> Hello, I want to iterate through the full set of pixels from a pixbuf, I
> though I could get them with the Pixels property from it, but now my
> problem is how to move the "pointer" in order to get every pixels from
> the Pixbuf, I could not find in the docs if the when you use Pixels the
> next time you call it it gives you the same or the next one.

The return value is a pointer to the RGB(a) buffer.

So you have to iterate manually.  The way to do so is by using:

	int rowstride = pixbuf.RowStride;
	int width = pixbuf.Width;
	int height = pixbuf.Height;
	byte *line = pixbuf.Pixels;

	for (y = 0; y < height; y++){
		for (x = 0; x < width; x++){
			byte *rgb = line [x*3];
			*rgb++ = 255;
			*rgb++ = 0;
			*rgb++ = 0;
		line += rowstride;

The example above fills the pixbuf with a red color, and assumes that
the image is an RGB image, as opposed to an RGBA image.  For RGBA
images, use x*4.