g.Pixel error?

I’m just taking baby steps with Xojo (I’ve got a design and filemaker background). I picked up the book “I wish I knew how to program the canvas control” and I’ve gotten to example 1-6 which is just a simple canvas with the paint event and the following code:

'Draw a green filled rectangle
g.ForeColor = RGB(10, 240, 10) 'Green
g.FillRect(49, 49, 100, 100) 'Filled Rectangle

'Colour the centre pixel red
g.Pixel(99, 99) = RGB(240, 10, 10)

'Get the pixel values outside and inside the Rectangle
MsgBox "The colour inside the rectangle is: " + CStr(g.Pixel(60,60)) + chr(13) +"The colour outside the Rectangle is: " + CStr(g.Pixel(20,20))

This crashes on the last line, something to do with the g.Pixel(60,60), any idea why?


This item has been DEPRECATED since version 2016r1 and should no longer be used.
Please use RGBSurface as a replacement.

What is the error message?

Exception Message: Graphics.Pixel can only be used with a Picture’s graphics object.

and it has to do with READING the pixel… not WRITING it

dim c as color=g.pixel(60,60)

gives the same error

Yes, that’s the error. Anyone know how to use RGBSurface to get it?


g here (in the paint event) can be considered a temporary graphic.
You can send to it, but you cant read from it.

If you want to know what pixels are what, you are better off creating a picture
Drawing to the picture’s rgbsurface
Reading from the picture’s rgbsurface

and in the Paint event where you have this g, just g.drawpicture thepicture, 0,0 to display your picture

[quote=381218:@Glen Newbury]Yes, that’s the error. Anyone know how to use RGBSurface to get it?


myColor = thepicture.RGBSurface.pixel(x,y)


In practice, replace picture.graphics by picture.rgbsurface.

Thanks Jeff and Michel, I’ll try it later today. I was trying it last night using that link and getting pretty frustrated, two steps forward, one back. :slight_smile:

Once you allow one thing to frustrate you ,everything seems insurmountable.