how to get the color of a pixel(x,y) on the window?



I use gtkmm on Windows platform,and have tried three method ,but the three methods return three different result,and none of them is correct,wh?
(1) 
Gdk::Color result;
Glib::RefPtr<Gdk::Image>  image=get_window()->get_image(0,0,allocation.get_width(),allocation.get_height());
 guint32 pixel=image->get_pixel(x,y);
 gushort red=(pixel& 0xFF000000)>>16;
 gushort green=(pixel& 0x00FF0000)>>8;
 gushort blue =(pixel& 0x0000FF00);
 
result.set_rgb(red,green,blue);
 
(2)
  int width1, height, rowstride, n_channels;
  guint8 *pixels, *p;
  n_channels= pixbuf->get_n_channels();   //pixbuf created from a .png file
  width = pixbuf->get_width();
  height = pixbuf->get_height();
   rowstride = pixbuf->get_rowstride();
  pixels= pixbuf->get_pixels();
  p = pixels + y* rowstride + x * n_channels;
gushort red=p[0]<<8;
gushort green=p[1]<<8;
gushort blue=p[2]<<8;
 
result.set_rgb(red,green,blue);
(3)
 int w=allocation.get_width();
 int h=allocation.get_height();
 Glib::RefPtr<Gdk::Drawable>  d=get_window();
 Glib::RefPtr<Gdk::Pixbuf> pixbuf=Gdk::Pixbuf::create(d,0,0,allocation.get_width(),allocation.get_height()); //pixpuf created from window

  int width, height, rowstride, n_channels;
  guint8 *pixels, *p;
  n_channels = pixbuf->get_n_channels();
  width = pixbuf->get_width();
  height = pixbuf->get_height();
  rowstride = pixbuf->get_rowstride();
  pixels = pixbuf->get_pixels();
  p = pixels + y* rowstride + x * n_channels;
gushort red=p[0]<<8;
gushort green=p[1]<<8;
gushort blue=p[2]<<8;

result.set_rgb(red,green,blue);
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]