Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problemme avec WxWidgets

afficher une image,comment on fait ?

Anonyme
    25 avril 2006 à 10:53:11

    Bonjour a tous.
    JE souhaite a laide de WxWidgets charger une image en .JPEG
    qui se trouve dans le meme dossier que mon application.
    Pour cela j'ai regarde les exemples fournie avec la librairie
    et j'en fait le code suivant:
    main.cpp
    // ----------------------------------------------------------------------------
    // headers
    // ----------------------------------------------------------------------------

    // For compilers that support precompilation, includes "wx/wx.h".
    #include "wx/wxprec.h"

    #ifdef __BORLANDC__
        #pragma hdrstop
    #endif

    // for all others, include the necessary headers (this file is usually all you
    // need because it includes almost all "standard" wxWindows headers)
    #ifndef WX_PRECOMP
        #include "wx/wx.h"
    #endif

    // ----------------------------------------------------------------------------
    // resources
    // ----------------------------------------------------------------------------

    // the application icon (under Windows and OS/2 it is in resources)
    #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
        #include "mondrian.xpm"
    #endif
    #include <wx/image.h>
    #include <wx/file.h>
    #include <wx/filename.h>
    #include <wx/mstream.h>
    #include <wx/wfstream.h>
    #include <wx/quantize.h>
    #include "main.h"

    BEGIN_EVENT_TABLE(BasicFrame, wxFrame)
        EVT_MENU(MQuit,  BasicFrame::OnQuit)
        EVT_MENU(MAbout, BasicFrame::OnAbout)
        EVT_MENU(MOpen, BasicFrame::OnOpen)
    END_EVENT_TABLE()

    BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
      EVT_PAINT(MyCanvas::OnPaint)
    END_EVENT_TABLE()

    IMPLEMENT_DYNAMIC_CLASS( BasicFrame, wxFrame )

    IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)

    // Create a new application object: this macro will allow wxWindows to create
    // the application object during program execution (it's better than using a
    // static object for many reasons) and also declares the accessor function
    // wxGetApp() which will return the reference of the right type (i.e. MyApp and
    // not wxApp)
    IMPLEMENT_APP(MyApp)

    // ============================================================================
    // implementation
    // ============================================================================

    // ----------------------------------------------------------------------------
    // the application class
    // ----------------------------------------------------------------------------

    // 'Main program' equivalent: the program execution "starts" here
    bool MyApp::OnInit()
    {
        // create the main application window
        //BasicFrame *frame = new BasicFrame("Un utilitaire d'images",100, 100,512, 384);
        BasicFrame *frame = new BasicFrame();
    #if wxUSE_LIBPNG
      wxImage::AddHandler( new wxPNGHandler );
    #endif

    #if wxUSE_LIBJPEG
      wxImage::AddHandler( new wxJPEGHandler );
    #endif

    #if wxUSE_LIBTIFF
      wxImage::AddHandler( new wxTIFFHandler );
    #endif

    #if wxUSE_GIF
      wxImage::AddHandler( new wxGIFHandler );
    #endif

    #if wxUSE_PCX
      wxImage::AddHandler( new wxPCXHandler );
    #endif

    #if wxUSE_PNM
      wxImage::AddHandler( new wxPNMHandler );
    #endif

    #if wxUSE_XPM
      wxImage::AddHandler( new wxXPMHandler );
    #endif

    #if wxUSE_ICO_CUR
      wxImage::AddHandler( new wxICOHandler );
      wxImage::AddHandler( new wxCURHandler );
      wxImage::AddHandler( new wxANIHandler );
    #endif
        frame->Show(TRUE);

        return true;
    }

    // ----------------------------------------------------------------------------
    // main frame
    // ----------------------------------------------------------------------------

    // frame constructor
    //BasicFrame::BasicFrame(const wxChar *title,int xpos, int ypos,int width, int height):
    BasicFrame::BasicFrame():
    wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),wxPoint(20,20), wxSize(470,360) )
    {
        // set the frame icon
        SetIcon(wxICON(mondrian));

    #if wxUSE_MENUS
        // create a menu bar
        wxMenu *menuFile = new wxMenu;

        // the "About" item should be in the help menu
        wxMenu *helpMenu = new wxMenu;
        helpMenu->Append(MAbout, _T("&About...\tF1"), _T("Show about dialog"));

        menuFile->Append(MQuit, _T("E&xit\tAlt-X"), _T("Quit this program"));
        menuFile->Append(MOpen, _T("Open File\tAlt-O"), _T("Open a file"));
        // now append the freshly created menu to the menu bar...
        wxMenuBar *menuBar = new wxMenuBar();
        menuBar->Append(menuFile, _T("&File"));
        menuBar->Append(helpMenu, _T("&Help"));

        // ... and attach this menu bar to the frame
        SetMenuBar(menuBar);
    #endif // wxUSE_MENUS
    m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
    //m_canvas = new MyCanvas();
    m_canvas->SetScrollbars( 10, 10, 50, 275 );
    #if wxUSE_STATUSBAR
        // create a status bar just for fun (by default with 1 pane only)
        CreateStatusBar(2);
        SetStatusText(_T("Welcome to wxWindows!"));
    #endif // wxUSE_STATUSBAR
    }

    MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,const wxPoint &pos, const wxSize &size )
    :wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )

    {
        ornella_jpeg =(wxBitmap*)NULL;
        wxBitmap bitmap( 100, 100 );
        colorized_horse_jpeg = (wxBitmap*) NULL;
        wxMemoryDC dc;
        wxImage image = bitmap.ConvertToImage();
        #if wxUSE_LIBJPEG
        image.Destroy();

        if ( !image.LoadFile(_T("./ornella.JPEG")) )
            wxLogError(wxT("Can't load JPG image"));
        else
        {
            ornella_jpeg = new wxBitmap( image );
            wxImage::HSVValue greenHSV = wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
            wxImage::HSVValue redHSV = wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
            image.RotateHue(redHSV.hue - greenHSV.hue);
            colorized_horse_jpeg = new wxBitmap( image );
        }
       
    #endif
    // wxUSE_LIBJPEG
    }

    MyCanvas::~MyCanvas()
    {
    delete ornella_jpeg;   
    }

    MyCanvas::MyCanvas(){}


    main.h
    #ifndef TRUC
    #define TRUC
    class MyApp : public wxApp
    {
    public:
        virtual bool OnInit();
    };

    class MyCanvas: public wxScrolledWindow
    {
    public:
    MyCanvas();
    ~MyCanvas();
    MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
    void OnPaint( wxPaintEvent &event );
    wxBitmap *ornella_jpeg ;
    wxBitmap *colorized_horse_jpeg;
    private:
        DECLARE_DYNAMIC_CLASS(MyCanvas)
        DECLARE_EVENT_TABLE()
    };

    // Define a new frame type: this is going to be our main frame
    class BasicFrame : public wxFrame
    {
    public:
       
       //BasicFrame(const wxChar *title,int xpos, int ypos,int width, int height);
        BasicFrame();
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnOpen(wxCommandEvent& event);
        MyCanvas         *m_canvas;

    private:
        DECLARE_DYNAMIC_CLASS(BasicFrame)
        DECLARE_EVENT_TABLE()
    };



    // ----------------------------------------------------------------------------
    // constants
    // ----------------------------------------------------------------------------

    enum
    {

        MQuit = 1,
        MOpen,
        MAbout= wxID_ABOUT
    };

    #endif


    fonction.cpp
    // ----------------------------------------------------------------------------
    // headers
    // ----------------------------------------------------------------------------

    // For compilers that support precompilation, includes "wx/wx.h".
    #include "wx/wxprec.h"

    #ifdef __BORLANDC__
        #pragma hdrstop
    #endif

    // for all others, include the necessary headers (this file is usually all you
    // need because it includes almost all "standard" wxWindows headers)
    #ifndef WX_PRECOMP
        #include "wx/wx.h"
    #endif

    // ----------------------------------------------------------------------------
    // resources
    // ----------------------------------------------------------------------------

    // the application icon (under Windows and OS/2 it is in resources)
    #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
        #include "mondrian.xpm"
    #endif
    #include "main.h"

    void BasicFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
    {
        // TRUE is to force the frame to close
        Close(TRUE);
    }

    void BasicFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
    {
        wxString msg;
        msg.Printf( _T("This is the About dialog of the minimal sample.\n")
                    _T("Welcome to %s"), wxVERSION_STRING);

        wxMessageBox(msg, _T("About Minimal"), wxOK | wxICON_INFORMATION, this);
    }

    void BasicFrame::OnOpen(wxCommandEvent& event){}

    /*
    BasicFrame::OnPaint(wxPaintEvent& WXUNUSED(event))
    {
        wxPaintDC dc( this );
        PrepareDC( dc );
    }
    */


    void MyCanvas::OnPaint( wxPaintEvent &event )
    {
    wxPaintDC dc( this );
        PrepareDC( dc );
        if (ornella_jpeg && ornella_jpeg->Ok())
        dc.DrawBitmap( *ornella_jpeg, 30, 380 );   
    }


    MAis cela plnate au chragement.
    J'obtient les messages suivant dans une boite de dialogue lie a l'erreur
    (le code de cette partie est dans le main.cpp->le constructeur de MyCanvas):
    -Can't Load JPG image
    (detail:)
    -No handler found for image type
    -Can't Load JPG image

    Je ne vois pas d'ou vient mon erreur.
    Si quelqu'un peut m'aider a charger cette image , je l'en remercie vivement
    P.S:Desole pour le manque de commentaire mais j'ai code ca super vite hier soir.
    • Partager sur Facebook
    • Partager sur Twitter
      26 avril 2006 à 15:12:57

      Essaie de faire ça non pas en C++ mais en C tout court.
      • Partager sur Facebook
      • Partager sur Twitter
        26 avril 2006 à 16:33:08

        Citation : 64enforce

        Essaie de faire ça non pas en C++ mais en C tout court.


        WxWidgets c'est du C++ pas du C.

        Est ce que tu as bien compiler WxWidgets avec le support du JPEG ? Si oui alors essaie d'enlever le bloc de macro :

        //#if wxUSE_LIBJPEG
          wxImage::AddHandler( new wxJPEGHandler );
        //#endif
        • Partager sur Facebook
        • Partager sur Twitter
        Anonyme
          28 avril 2006 à 11:26:40

          Ok merci Garuma ,cela marche...
          • Partager sur Facebook
          • Partager sur Twitter
            28 avril 2006 à 11:31:42

            Citation : Davidbrcz

            Bonjour a tous.
            JE souhaite a laide de WxWidgets charger une image en .JPEG
            qui se trouve dans le meme dossier que mon application.
            Pour cela j'ai regarde les exemples fournie avec la librairie
            et j'en fait le code suivant:


            Question débile, et sans aucun rapport avec le sujet ou le forum, je m'en excuse d'avance : pourquoi ces retours à la ligne au milieu des phrases ? Tu ne peux pas continuer sur toute la ligne ? Le retour à la ligne en fin de page est automatique, tu sais ^^
            • Partager sur Facebook
            • Partager sur Twitter

            Problemme avec WxWidgets

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown