Easy way to scale and save images

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
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Easy way to scale and save images

Post by Wanderer82 »

Hello again

I've run into a new problem and been searching for quite a while. All I want to do is the following:

User can choose an image (jpg, bmp, png, gif) on the computer. In my program the image has to be displayed in a specific size. So I need the program to scale the chosen image down or up to the specific size and save it - preferrably as a jpg. Then show it to the user so that he can decide whether the image still looks okay in this size.

I tried this (just to try out, the scaling part is still missing):

Code: Select all

wxInitAllImageHandlers();
    wxImage img;
    img.LoadFile("C:\\programmieren\\1160818l.jpg", wxBITMAP_TYPE_ANY, -1);

    img.SetOption(wxIMAGE_OPTION_QUALITY, 80);
    img.SaveFile(wxT("C:\\programmieren\\test\\1160818l.jpg"), wxBITMAP_TYPE_JPEG);

But the image won't save.

Thanks for any help.
Thomas
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Easy way to scale and save images

Post by PB »

Does the image even load properly, i.e., LoadFile() and IsOk() return true?

When saving the image, I assume that SaveFile() returns false: Are there any errors reported? Have you tried to save the image under different name?

As for scaling, there is obvious wxImage::Scale() and similar.
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Easy way to scale and save images

Post by Wanderer82 »

Well I tried the following code:

Code: Select all

  bool isok = img.IsOk();

    if (isok == TRUE)
    {
        wxMessageBox("true");
    }

    if (isok == FALSE)
    {
        wxMessageBox("false");
    }
I don't get any wxMessageBox, so neither "true" nor "false". LoadFile() is the same. But I think I'm doing something wrong here to get the bool value?

Oh and how / where can I see errors from a "false"? I use Code::Blocks.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Easy way to scale and save images

Post by PB »

Please use regular C++, something like this

Code: Select all

if ( img.IsOk() )
   wxLogMessage("Image is OK");
else
   wxLogError("Image is NOT OK");
Is the application a GUI one and if not, are wxWidgets properly initialized. AFAIK, any errors during loading or saving a wxImage are reported by wxLogXXX functions.
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Easy way to scale and save images

Post by Wanderer82 »

Yes, it's a GUI. I put a wxImage in this project by using the symbol of wxSmith. This is my entire code:

Code: Select all

/***************************************************************
 * Name:      createprodukt_testMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    Thomas ()
 * Created:   2018-04-15
 * Copyright: Thomas ()
 * License:
 **************************************************************/

#include "createprodukt_testMain.h"
#include <wx/msgdlg.h>
#include <wx/bitmap.h>
#include <wx/log.h>

//(*InternalHeaders(createprodukt_testFrame)
#include <wx/intl.h>
#include <wx/string.h>
#include <wx/image.h>
#include <wx/dcclient.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(createprodukt_testFrame)
const long createprodukt_testFrame::ID_BUTTON1 = wxNewId();
const long createprodukt_testFrame::ID_PANEL1 = wxNewId();
const long createprodukt_testFrame::idMenuQuit = wxNewId();
const long createprodukt_testFrame::idMenuAbout = wxNewId();
const long createprodukt_testFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(createprodukt_testFrame,wxFrame)
    //(*EventTable(createprodukt_testFrame)
    //*)
END_EVENT_TABLE()

createprodukt_testFrame::createprodukt_testFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(createprodukt_testFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    Button1 = new wxButton(Panel1, ID_BUTTON1, _("Label"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer2->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel1->SetSizer(BoxSizer2);
    BoxSizer2->Fit(Panel1);
    BoxSizer2->SetSizeHints(Panel1);
    BoxSizer1->Add(Panel1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    SetSizer(BoxSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    Image1 = new wxImage();
    Image1_BMP = new wxBitmap();
    BoxSizer1->Fit(this);
    BoxSizer1->SetSizeHints(this);

    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&createprodukt_testFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&createprodukt_testFrame::OnAbout);
    //*)
}

createprodukt_testFrame::~createprodukt_testFrame()
{
    //(*Destroy(createprodukt_testFrame)
    //*)
}

void createprodukt_testFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void createprodukt_testFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void createprodukt_testFrame::OnButton1Click(wxCommandEvent& event)
{
    wxInitAllImageHandlers();
    wxImage img;
    bool Loadimage = img.LoadFile("C:\\programmieren\\images.jpg", wxBITMAP_TYPE_ANY, -1);

    if ( img.IsOk() )
     wxLogMessage("Image is OK");
    else
     wxLogError("Image is NOT OK");

    img.SetOption(wxIMAGE_OPTION_QUALITY, 80);
    img.SaveFile(wxT("c:\\programmieren\\test\\test.jpg"), wxBITMAP_TYPE_JPEG);
}
I'm sorry, I'm quite a beginner and I even don't know where to find the errors that the wxLog functions might set... :(
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Easy way to scale and save images

Post by PB »

Wanderer82 wrote:I'm sorry, I'm quite a beginner and I even don't know where to find the errors that the wxLog functions might set... :(
The errors and messages produced by wxLog..() functions are in GUI programs shown using a message dialog, so you really cannot miss them.

Have you verified that OnButton1Click() is actually executed? The code you posted has A TON of noise, so it is possible I missed from where it is supposed to be called...
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Easy way to scale and save images

Post by Wanderer82 »

Oh gosh :oops: , normally if I create something with wxSmith everything is connected right away but this time it really wasn't. So your tip was absolutely "brilliant"... the button wasn't connected to OnButton1Click.

It's working now. Thank you!
Post Reply