tags:

views:

51

answers:

2
+1  Q: 

wxwidgets saveFile

Hi, 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

A: 

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.

SteveL
+2  A: 

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.

mghie