Mike
Mike

Reputation: 11

wxwidgets saveFile

Im trying to draw a simple picture and save it to a file in wxwidgets. This is the code i have so far. However this code will never create the test.png picture.

    wxBitmap myBitmap;
    wxMemoryDC dc;
    dc.SelectObject(myBitmap);
    wxFont font=dc.GetFont();
    font.SetPointSize(15);
    dc.SetTextForeground(*wxBLACK);
    dc.DrawRectangle(0,0,50,100);
    wxString s(_T("A"));
    dc.DrawText(s, 5,5);
    wxString test(_T("images/test.png"));
    myBitmap.SaveFile(test, wxBITMAP_TYPE_PNG);

Can somebody please help me out in what I'm doing wrong... /Mike

Upvotes: 1

Views: 2638

Answers (4)

bmadden
bmadden

Reputation: 1

You may need to unselect the bitmap from the DC before saving it too:

dc.SelectObject(wxNullBitmap);
myBitmap.SaveFile(test, wxBITMAP_TYPE_PNG);

Upvotes: 0

Júlio Jamil
Júlio Jamil

Reputation: 107

Update:

"test" = image output name.

".png" image output extention

"wxBITMAP_TYPE_PNG" = wxBitmap image type. see: http://docs.wxwidgets.org/trunk/gdicmn_8h.html#a90a1eb6d85b5044a99b706fd979f27f5 for more explanation of types !

"SaveFile" = wxBitmap save file.

"wxBitmap myBitmap;" = variable in your example.

"wxString myPath(wxGetCwd());" = varibale wxString equivalent : "wxstring myPath; myPath = wxGetCwd(); Returns the path of the app folder in myPath variable. wxString myPath is variable type wxString wxGetCwd() return path app folder. wxString myPath(wxGetCwd()); is path for app folder sets in myPath variable!

example complete:

wxString myPath(wxGetCwd());
wxBitmap myBitmap;
myBitmap.SaveFile(localAtual + wxT("//test.png"), wxBITMAP_TYPE_PNG);

Upvotes: -1

mghie
mghie

Reputation: 32334

First a piece of general advice: Use the debug version of wxWidgets, it will often assert whenever you did something wrong.

First you call the default wxBitmap constructor, resulting in an object that can not be used as-is. If you change your first line to

wxBitmap myBitmap(200, 200);

you will instead create a bitmap of 200 pixels width and height, which can actually be selected into the dc and be painted on. Alternatively you can call wxBitmap::Create() before you use the bitmap.

Also, in order to save as a PNG file you need to register the PNG image handler (see the wxImage documentation) first. It's probably easiest to register all default image handlers by calling ::wxInitAllImageHandlers().

Finally you need to make sure that there is a subdirectory images under your current directory. Or use a file name with an absolute path. You would however get a message box at runtime if the saving of the bitmap failed.

Upvotes: 4

SteveL
SteveL

Reputation: 1821

Firstly I would check the return of the call to SaveFile, if it returns true then it should be succeeding, otherwise there is a failure somewhere else.

Secondly try giving a full path rather than just images/test.png as depending on the current working directory of you program it might not put the file where you think it should.

Upvotes: -1

Related Questions