wxBitmap alpha channel is not working on buttons.

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
User avatar
snacker
In need of some credit
In need of some credit
Posts: 4
Joined: Tue May 26, 2020 5:09 pm

wxBitmap alpha channel is not working on buttons.

Post by snacker » Tue May 26, 2020 5:26 pm

I am creating a toolbar with buttons, which have RGBA-format PNG bitmaps applied like this:

Code: Select all

   static const long TOOLBAR_STYLE = wxTB_FLAT | wxTB_HORIZONTAL | wxTB_TEXT;

   wxImage::AddHandler(new wxPNGHandler);

   static wxBitmap newbitmap("new.png", wxBITMAP_TYPE_PNG);
   static wxBitmap openbitmap("open.png", wxBITMAP_TYPE_PNG);
   static wxBitmap savebitmap("save.png", wxBITMAP_TYPE_PNG);
   static wxBitmap runbitmap("run.png", wxBITMAP_TYPE_PNG);
   static wxBitmap pausebitmap("pause.png", wxBITMAP_TYPE_PNG);
   static wxBitmap stopbitmap("stop.png", wxBITMAP_TYPE_PNG);

   assert(newbitmap.GetDepth() == 32);
   //assert(newbitmap.GetMask()); // GetMask returns NULL.

   wxToolBar* toolbar=CreateToolBar(TOOLBAR_STYLE);

   toolbar->AddTool(0, "New", newbitmap, "New");
   toolbar->AddTool(1, "Open", openbitmap, "Open");
   toolbar->AddTool(2, "Save", savebitmap, "Save");

   toolbar->AddSeparator();

   toolbar->AddTool(3, "Run", runbitmap, "Run");
   toolbar->AddTool(5, "Pause", pausebitmap, "Pause / Continue");
   toolbar->AddTool(6, "Stop", stopbitmap, "Stop");

   toolbar->Realize();

The bitmap objects do all have an alpha channel, as evidenced by the assertion check I have included on GetDepth() returning 32.

However, the alpha channel seems to be ignored when the toolbar and buttons are drawn:

s1.png
s1.png (12.63 KiB) Viewed 319 times

The alpha-masked pixels are showing up black, whereas they should be transparent.

I am using MSW in this case, but I have previously tried this exact same code on Linux and there is no problem on Linux. Is this because the MSW backend does not support the alpha channel in bitmaps? Is there any workaround for this?

Thanks.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4245
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxBitmap alpha channel is not working on buttons.

Post by ONEEYEMAN » Tue May 26, 2020 6:31 pm

Hi,
Can you build and run the image sample and check whether it displays the alpha channel correctly?

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 14976
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxBitmap alpha channel is not working on buttons.

Post by doublemax » Tue May 26, 2020 6:58 pm

Try this:

From the wxToolBar docs:
If you wish to use 32-bit images (which include an alpha channel for transparency) use:
wxSystemOptions::SetOption("msw.remap", 2);
https://docs.wxwidgets.org/trunk/classwx_tool_bar.html
Use the source, Luke!

User avatar
snacker
In need of some credit
In need of some credit
Posts: 4
Joined: Tue May 26, 2020 5:09 pm

Re: wxBitmap alpha channel is not working on buttons.

Post by snacker » Tue May 26, 2020 7:45 pm

doublemax wrote:
Tue May 26, 2020 6:58 pm
Try this:

From the wxToolBar docs:
If you wish to use 32-bit images (which include an alpha channel for transparency) use:
wxSystemOptions::SetOption("msw.remap", 2);
https://docs.wxwidgets.org/trunk/classwx_tool_bar.html
This did not have any effect unfortunately.

I did build and run the images sample, and it seems to work fine.
However, this is using wxImages. So perhaps wxImage behaves differently to wxBitmap?
a2.png

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2469
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxBitmap alpha channel is not working on buttons.

Post by PB » Tue May 26, 2020 8:18 pm

Seems to work just fine for me, on Win10 with wxWidgets master and the "toucan.png" from the image sample.
toolbar-toucan.png
toolbar-toucan.png (32.62 KiB) Viewed 292 times

Code: Select all

#include <wx/wx.h>

class MyApp : public wxApp
{
public:
    bool OnInit() override
    {
        wxFrame* frame = new wxFrame(nullptr, wxID_ANY, "Toolbar test");
        wxBitmap bitmap;

        wxInitAllImageHandlers();

        if ( bitmap.LoadFile("toucan.png", wxBITMAP_TYPE_PNG) )
        {
            wxToolBar* toolBar = new wxToolBar(frame, wxID_ANY);

            toolBar->AddTool(wxID_ABOUT, "", bitmap);
            toolBar->AddTool(wxID_EXIT, "", bitmap);
            toolBar->Realize();
            frame->SetToolBar(toolBar);
        }

        frame->Show();
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);
I remember a similar report but the reporter failed to embed the proper application manifest so he effectively used the Win95 look. I think this is not your case as your toolbar has the flat look?
EDIT: I see that you request the flat look. Well, I guess you would have noticed if the application did not have the manifest, as it would look out of the place on modern Windows.

User avatar
snacker
In need of some credit
In need of some credit
Posts: 4
Joined: Tue May 26, 2020 5:09 pm

Re: wxBitmap alpha channel is not working on buttons.

Post by snacker » Tue May 26, 2020 8:56 pm

That code produces this result here:
s2.png
s2.png (17.3 KiB) Viewed 283 times
I'm using wxWidgets-3.1.3.
I don't know how to create a manifest. I'm using MinGW and GCC to build the application.

User avatar
snacker
In need of some credit
In need of some credit
Posts: 4
Joined: Tue May 26, 2020 5:09 pm

Re: wxBitmap alpha channel is not working on buttons.

Post by snacker » Tue May 26, 2020 9:13 pm

Okay, I figured out how to use MinGW to create and link the required Windows resource file, by reading through that thread which PB linked to.
This was indeed the problem, so the issue is now resolved. Thank you everyone for your help!
s3.png
s3.png (12.9 KiB) Viewed 278 times

Post Reply