Search code examples
c++codeblockswxwidgets

How to Stretch Bitmap into Parent Panel (wxWidgets Custom)


Screenshot of Panel and a custom inside of it

I set a wxButton and it loads bitmap,png formats to Panel at right. But image is overfitting. I want to stretc image to Panel when it's loaded by button.

Here is button's function, just in case:

void rrFrame::testClick(wxCommandEvent& event)
{
    wxBitmap bmp;
    wxString strPath = wxT("img\\img1.png");

    bmp.LoadFile(strPath, wxBITMAP_TYPE_ANY);
    camFrame_wx->DrawBitmap(bmp, wxPoint(0, 0), false);
    //camFrame_wx is the variable name of 'Custom'
}

I suppose i need a stretch or fit function in constructor. How to do this?


Solution

  • I think the easiest way to do this would be to load the image file into a wxImage first, then rescale the wxImage, and finally convert the wxImage to a wxBitmap. Like so:

    void rrFrame::testClick(wxCommandEvent& event)    
    {
        wxString strPath = "img\\img1.png";
        wxImage im(strPath,wxBITMAP_TYPE_ANY );
        wxSize sz = camFrame_wx->GetSize();
        im.Rescale(sz.GetWidth(),sz.GetHeight(),wxIMAGE_QUALITY_HIGH );
        wxBitmap bmp(im);
        camFrame_wx->DrawBitmap(bmp, wxPoint(0, 0), false);
        //camFrame_wx is the variable name of 'Custom'
    }
    

    Two additional comments:

    1. You shouldn't need the wxT macro for string literals in wxWidgets 3.0 or later.
    2. You can use other options such as wxIMAGE_QUALITY_NEAREST instead of wxIMAGE_QUALITY_HIGH if you need faster rescaling. The full list is available here.