RGBSurface is Nil 2023

I have a verified picture created from code from the XOJO documentation. I’m trying to get
pixel colors from the pic. No luck.

I put in the below documentation example code, no luck.

Maybe someone can help me on this.

Thanks!!! (again)

Carl

'surf = aPicture.RGBSurface
'TextFieldColor.text=surf.Pixel(60,60).ToString <— this my ultimate goal

Below fails from the documentation

Var surf As RGBSurface
surf = aPicture.RGBSurface
if surf = Nil then quit 'my addition
Var lastX As Integer = aPicture.Width-1
Var lastY As Integer = aPicture.Height - 1
For y As Integer = 0 To lastY
For x As Integer = 0 To lastX
If surf.Pixel(x, y) = Color.RGB(255, 102, 102) then <— test picture is one color
surf.Pixel(x, y) = &cFFFFFF
End if
Next
Next

Var t As Introspection.TypeInfo = Introspection.GetType(aPicture)
textFieldColor.text=t.Name <— this confirms I have a pic

earlier on:

Var aPicture As New picture(120, 120, 0)
'(X, Y, 0) must be 0!! and the window property is required. (not well documented IMHO)

Not all pictures have RGBSurface.

Picture could be static resource for example which is not mutable or a number of other things.

If Picture does not have RGBSurface then you can always create new picture with the new operator and draw the other picture into the new one. Picture you create your self with the new operator always are mutable and therefore always have RGBSurface.

I would say that it is good practice to get into the habit of creating a new picture or context, then drawing the existing picture into to, then editing it.

The reason being is this is how other tools work, and if you ever get deep enough into photo editing, you’ll need to adopt this kind of workflow so you can control the bit depth, color space and other aspects of image editing that Xojo hides from you.

Specifically, pictures created with the 0 parameter are not pixel based. They have no Graphics or RGBSurface. Either leave the 0 off completely, or use 32 instead.

Var aPicture As New picture(120, 120)
Var aPicture As New picture(120, 120, 32)

This is your vector stuff again.
If you want to examine pixels, draw the object2d data from this picture, into the grapohics of another picture created with a depth of 32, first

Var temppic as new picture (apicture.width,apicture.height,32)
Var surf As RGBSurface
surf = temppic.RGBSurface
temppic.graphics.drawobject apicture.objects,0,0    //probably
if surf = Nil then quit 'my addition
Var lastX As Integer = aPicture.Width-1
Var lastY As Integer = aPicture.Height - 1
For y As Integer = 0 To lastY
For x As Integer = 0 To lastX
If surf.Pixel(x, y) = Color.RGB(255, 102, 102) then <— test picture is one color
surf.Pixel(x, y) = &cFFFFFF
End if
Next
Next

But:
Sometimes, (especially on a Mac), you might discover that the color you drew in the first picture is not the one you read back, due to color profiles.
It can be infuriating. When that happens, it may be better to check for ‘not black’, ‘not white’, or ‘something close to’

Lastly, if the color does stay the same, there are faster ways to convert pixels of one color to another color. For another post

Granted. Another reason would be that if you draw to the original picture, it’s possibly being used/drawn somewhere else and the changes would appear there as well.

1 Like