Issue saving bitmaps to file Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Morat20
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Jan 07, 2014 8:43 pm

Issue saving bitmaps to file

Post by Morat20 »

Attempting to save a static bitmap to file generates an all black result (whether saved as bitmap, jpg, etc). I Here's the stripped down broken version. The bitmaps come from the resource file.

Code: Select all

//This bit declared elsewhere
wxStaticBitmap myBitmap->SetBitmap(wxBitmap("useful_bitmap",wxBITMAP_TYPE_BMP_RESOURCE));   


//This bit the function
wxInitAllImageHandlers();
wxImage image = myBitmap->GetBitmap().ConvertToImage();
wxString ext=BitmapFilename.AfterLast('.');
if (ext=="bmp")      {image.SaveFile(BitmapFilename,wxBITMAP_TYPE_BMP);}
else if (ext=="jpg") {image.SaveFile(BitmapFilename,wxBITMAP_TYPE_JPEG);}
else if (ext=="png") {image.SaveFile(BitmapFilename,wxBITMAP_TYPE_PNG);}
The end result is an entirely black bitmap, png, or jpg file. The bitmaps themselves are pretty simple line drawings on a white background. But the end
result is, as noted, a black square.

What's weird is if I reload the bitmap from the resource file rather than using the static bitmap like this:

Code: Select all

wxImage image("useful_bitmap", wxBITMAP_TYPE_BMP_RESOURCE,-1);
It works fine. This is wxWidgets 2.9.5 under Windows 7.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Issue saving bitmaps to file

Post by ONEEYEMAN »

Hi,
Does the original image has an Alpha?
Also - what platform are you trying this on? And how did you build wxWidgets?

Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Issue saving bitmaps to file

Post by PB »

The code looks OK, I am assuming you are running debug build of wxWidgets which would assert if the bitmap or image were invalid.

My guess would be that the issue is somewhere else than in the posted code (assuming you did not omit anything relevant) and you would not be able to reproduce it in an actual minimal compilable example like this

Code: Select all

#include <wx/wx.h>

class MyApp : public wxApp
{
public:
    bool OnInit()
    {    
        const wxSize size(48, 48);
        wxBitmap bitmap(size);
        wxMemoryDC dc(bitmap);
        const wxBrush brush(*wxRED);
        const wxPen pen(*wxBLUE, 4);
        wxDCPenChanger penChanger(dc, pen);
        wxDCBrushChanger brushChanger(dc, brush);
                
        dc.DrawRectangle(wxRect(size));
        dc.SelectObject(wxNullBitmap);
        
        const wxImage image = bitmap.ConvertToImage();

        wxInitAllImageHandlers();
        if ( image.SaveFile("wxImage.png", wxBITMAP_TYPE_PNG) )
            wxLogMessage("Image saved.");
        return false;
    }
}; wxIMPLEMENT_APP(MyApp);
Just make sure you are not making a silly mistake like saving the file somewhere else then you think and banging the head against the wall looking at the irrelevant old images produced by old code. Happened to me at least once, since then I always check timestamps...

BTW, why using ancient unstable wxWidgets 2.9.5?
Morat20
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Jan 07, 2014 8:43 pm

Re: Issue saving bitmaps to file

Post by Morat20 »

ONEEYEMAN wrote: Mon Apr 01, 2019 9:34 pm Hi,
Does the original image has an Alpha?
Also - what platform are you trying this on? And how did you build wxWidgets?

Thank you.
Windows 7, wxWidgets 2.9.5, static libraries.

And I have no idea of the original images have an alpha. They predate me by some time. For that matter, I'm not even sure how to check.

I do know the bulk of the image is transparent (while white in a preview image, the background is the same background color as the GUI when added), and I'm about 80% certain they were probably generated in MS paint. Replacing them with higher quality images is on the "to-do" list.
Morat20
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Jan 07, 2014 8:43 pm

Re: Issue saving bitmaps to file

Post by Morat20 »

PB wrote: Mon Apr 01, 2019 10:01 pm The code looks OK, I am assuming you are running debug build of wxWidgets which would assert if the bitmap or image were invalid.

My guess would be that the issue is somewhere else than in the posted code (assuming you did not omit anything relevant) and you would not be able to reproduce it in an actual minimal compilable example like this

Just make sure you are not making a silly mistake like saving the file somewhere else then you think and banging the head against the wall looking at the irrelevant old images produced by old code. Happened to me at least once, since then I always check timestamps...

BTW, why using ancient unstable wxWidgets 2.9.5?
Timestamps were the first thing I checked, and the sample code provided works just fine.

This is rather old legacy code, originally built under a much, much older version of wxWidgets, and updating to the 2.9 branch was the best that could be done in the time allotted. A further update is scheduled sometime in the future.

I don't know why

Code: Select all

myStaticBitmap->SetBitmape(wxBitmap(bitmapname,wxBITMAP_TYPE_BMP_RESOURCE));
image = myStaticBitmap->GetBitmap().ConvertToImage();
doesn't work but

Code: Select all

image(bitmapname,wxBITMAP_TYPE_BMP_RESOURCE,-1)
does. Since we swap out the displayed bitmap a lot (there's some 400 or 500 of the things, depending on the specifics chosen), trying to carry around the correct bitmap name (or derive it from the controls) when I need to print it would be...painful.
Morat20
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Jan 07, 2014 8:43 pm

Re: Issue saving bitmaps to file

Post by Morat20 »

ONEEYEMAN wrote: Mon Apr 01, 2019 9:34 pm Hi,
Does the original image has an Alpha?
Also - what platform are you trying this on? And how did you build wxWidgets?

Thank you.
I believe the problem is Alpha. Loading:

Code: Select all

MyStaticBitmap->SetBitmap(wxBitmap("myBitmap",wxBITMAP_TYPE_RESOURCE));
wxImage image = MyStaticBitmap->GetBitmap().ConvertToImage();
bool testAlpha = image.HasAlpha();
Generates "false" for "testAlpha".

But

Code: Select all

 wxImage image2("myBitmap", wxBITMAP_TYPE_BMP_RESOURCE,-1);
bool testAlpha2 = image2.HasAlpha();
Generates "true". So loading it from the static bitmap is losing the alpha data.

I'm not sure how to fix that. I have quite a few bitmaps, and if I can't just grab the one on screen (the static bitmap) determining which one is the correct one will be labor intensive. To add to it, I don't really know much about bitmaps and alpha channels in the first place.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Issue saving bitmaps to file

Post by PB »

Morat20 wrote: Mon Apr 01, 2019 11:03 pm I believe the problem is Alpha.
I would expect that the wxBitmap to wxImage conversion would take care of any kind of possible transparency in the bitmap. What is the original bitmap format: BMP (if so does wxBitmap::HasMask() return true?, PNG...? I also think I helped a poster here recently with an issue with drawing bitmapped buttons and I kind of hazily remember some issue with transparency in bitmaps passed to and particularly from wxStaticBitmap, but my memory may be deceiving me and i do not have time to look into it ATM.

I remember a couple of years back wxWidgets had several issues with transparency in bitmaps, but do not remember exactly where (e.g., alpha, mask, drawing, converting...). As a last measure, just to rule out a possible bug in old wxWidgets: Could you isolate the portion of your code to a small compilable example still manifesting the issue and try it on latest stable or master of wxWidgets?
Post Reply