Change individual pixels of a Gtk.Image object

I am trying to set individual pixels in a Gtk.Image widget. The documentation states that the ImageProp property for Gtk.Image returns Gdk.Image, which seems to allow editing individual pixels, but whenever I use this, it returns only null.

My solution so far is to load the image from disk as in System.Drawing.Bitmap, edit it, save it in a temporary file, and then load it into Gtk.Image, but this is obviously not ideal.

For

Gtk.Image image = new Gtk.Image("images/test.png"); Gdk.Image gdkImage = image.ImageProp; 

Why is gdkImage always null?

The image loads and displays correctly.

+4
source share
2 answers

Although I have no experience with GTK # or C #, based on the fact that little is known about Gtk and what I can find on the Internet, I will try to provide some materials.
You can get Gdk.Image for Gtk.Image only if you created Gtk.Image from Gdk.Image using the specified property, otherwise you will get null , as in your case when you create it from a file or as suggested by creating it from Gdk.Pixbuf . In the current case, you can try to get Gdk.Image with Gdk.Drawable using the Gdk.Drawable.GetImage method or use the Gdk.Image.Get method. You can use GdkWindow associated with Gtk.Image as Gdk.Drawable in these cases. In order for Gdk.Window be valid, the widget had to be displayed or implemented. But it is likely that you may end up with null in both cases.
Side Note: The GdkImage API is deprecated in newer versions of Gtk , note that this may not be the case with GTK # for now.
Thus, it would be nice to use Gdk.Pixbuf . You can get pixels for GdkPixbuf and change them in GTK. But, unfortunately, it seems that in the case of GTK # this particular property (pixels) of Gdk.Pixbuf made only as read. One option is perhaps to use Gdk.Pixdata from Gdk.Pixbuf , which is created from an image, change the pixels using the methods in Gdk.Pixdata , create Gdk.Pixbuf and copy it back to the original Gdk.Pixbuf . Unfortunately, I cannot be sure of this, you can give him a chance. Alternatively, you can consider the drawing on Gdk.Drawable . There are examples in which the Gdk.Drawable associated with Gtk.Widget (usually Gtk.DrawingArea ) is updated in the Expose Event callback.
I hope the information provided may provide you with some pointer to continue.

+1
source

First use Pixbuf :

 Gdk.Pixbuf pixbufImage = new Gdk.Pixbuf(@"images/test.png"); Gtk.Image gtkImage = new Gtk.Image(pixbufImage); Gdk.Image gdkImage = gtkImage.ImageProp; 
0
source

Source: https://habr.com/ru/post/1382316/


All Articles