Page 1 of 1

Transparency PNG

Posted: Sat Jun 22, 2019 1:35 am
by mxoliveira73
I'm trying create a image png with transparency. Very basic...
But.. in the example bellow, i created a wxPanel black, and over it there are a image PNG with transparency. In true, part white in the image is a
PNG transparent, it's no white... I don't understand what is wrong. It's so basic.... Coul'd someone give one tip, please?

Panel_box = new wxPanel(newPanel, wxID_ANY,wxPoint(0,50),wxSize(200,100));
Panel_box->SetBackgroundColour(wxColor(0,0,0));

wxBitmap bmp;
bmp.LoadFile("image.png", wxBITMAP_TYPE_PNG);

ball = new wxButton(Panel_box, ID_ball, wxEmptyString, wxPoint(50,05), bmp.GetSize(), wxTRANSPARENT_WINDOW | wxBORDER_NONE);

ball->SetBitmap(bmp);

ball.png
ball.png (7.81 KiB) Viewed 314 times

Re: Transparency PNG

Posted: Sat Jun 22, 2019 4:15 am
by New Pagodi
When the "transparent" parts of an image are drawn, what really happens is that the background color of the window is drawn instead. ball thinks its background color is white so that's what gets drawn for the transparent parts of the image. You can try

Code: Select all

ball->SetBackgroundColour(wxColor(0,0,0));
to make the button think it has the same background color as the panel. That works on windows, but it may not work on GTK+ or mac since setting colors for buttons may not be supported on those platforms.

Re: Transparency PNG

Posted: Sun Jun 23, 2019 12:48 pm
by mxoliveira73
One more time, big big thanks, New Pagodi!!!!

Re: Transparency PNG

Posted: Mon Jun 24, 2019 2:52 pm
by ONEEYEMAN
New Pagodi,
So it is best to modify the PNG and give it an appropriate background colour in order to make it cross-platform?

Thank you.

Re: Transparency PNG

Posted: Mon Jun 24, 2019 4:07 pm
by New Pagodi
ONEEYEMAN wrote:
Mon Jun 24, 2019 2:52 pm
New Pagodi,
So it is best to modify the PNG and give it an appropriate background colour in order to make it cross-platform?

Thank you.
That will work if the background color of the panel never changes. The only solution that is guaranteed to work on all ports is to skip the button class altogether, draw the bitmap directly on to the panel, and handle the mouse left up event on the panel and check if it is over the bitmap. But that's complicated enough that if setting the background color works for what OP wants, it might as well be done that way.

Re: Transparency PNG

Posted: Mon Jun 24, 2019 4:48 pm
by ONEEYEMAN
New Pagodi,
Well for my use case I need the button on the toolbar, not the panel.

So I will just modify the PNG background and everybody will be happy. ;-)

Thank you.