Xojo make mask code no longer working?

I have been using some variation of this code to create masked PNG images for years
Now all of a sudden it doesn’t work, heck doesn’t even find the right values to begin with

Dim dlg     As OpenDialog
Dim f       As FolderItem
Dim pic     As picture
Dim pic_rgb As RGBSurface
Dim msk     As picture
Dim msk_rgb As RGBSurface
Dim x       As Integer
Dim y       As Integer

dlg       = New OpenDialog
dlg.Title = "Select an Image file"
f = dlg.ShowModal
If f <> Nil Then
  pic     = picture.Open(f)
  pic_rgb = pic.RGBSurface
  msk     = New picture(pic.Width,pic.Height)
  msk_rgb = msk.Rgbsurface
  For x=0 To pic.Width
    For y=0 To pic.Height
      Dim q As Color =  pic_rgb.Pixel(x,y)
      If q=&cFF000000 Then 
        // if main pic color is RED mask it to transparent
      End If
    Next y
  Next x
  pic.ApplyMask msk
  MsgBox "done"
End If

The idea is is scans the main image (pic) looking for RED pixels (&cFF000000) and sets the corresponding pixel to transparent.

The odd thing… “Q” is never RED, as a matter of fact it is only ever WHITE or BLACK when in fact the image is made up of 3,135 different colors

Can you see something that I don’t (Xojo 2019r1.1)

for what its worth

Does it make a difference if you take off the last “00” in “If q=&cFF000000”? Maybe it doesn’t like the alpha channel value in the if statement.

No it makes no differnce…

I tried

  • &cFF0000
  • &CFF000000
  • color.red

Copying your code as-is, and saving the red doorway as a .png,

If q=&cFF000000

ends up being true multiple times in 2019r3.2 (I don’t have r1.1 installed).

wow… I just ran in 2022.r1 and it still not work00

breakpoint on


never happens

Have you tried resaving the image you uploaded here to see if that works? I feel like something must be wrong with your image?

WOW! not sure how that happened… but I told my paint program to flood with RED

and instead of &CFF0000 I got &CFB0007
no wonder it didn’t work


Now my app can have tunnels and windows :slight_smile:

1 Like

The most likely cause of the difference in values, is probably color spaces. IIRC this is an area that Xojo is weak at and to handle correctly requires the use of a 3rd party library (even though support is built-into the OS).

1 Like

Xojo is optimised for displaying graphics on-screen which means there is a high chance the pixels you access in the picture are not the same as what was stored in the image:

• If your source image is a PNG, a gamma value could be getting applied.
• If your source image has an alpha channel the pixels may be getting pre-multiplied.
• If you are on a Mac, the source image may be getting converted to the device colour space.

I think the only way to avoid this is to work at a lower level than what Xojo provides - ie: the operating system APIs or libraries such as libPNG, libTIFF, libJPEG.

We mainly use the MBS plugins to facilitate this but Einhuger also has similar plugins.

Why not just use a transparent PNG?