user478905
user478905

Reputation: 27

How to load gif using wxWidgets c++?

I need your help! Im currently really new to wxWidgets as a uni student. I am trying to load a .gif file using wxWidgets. I tried using the code below... but it only loads 1 frame, it doesn't move/ change to the other frames, it's basically a still picture. The file is .gif extension, and it has 3 frames or so. The location of the image is already correct. Help would be much appreciated!

Thank you

#include "ImageWindow.h"
#include <wx/stdpaths.h>
#include <wx/filename.h>
BEGIN_EVENT_TABLE(ImageWindow, wxWindow)
EVT_PAINT(ImageWindow::OnPaint)
END_EVENT_TABLE()
ImageWindow::ImageWindow(wxFrame *parent)
    : wxWindow(parent, wxID_ANY)
{
    this->SetBackgroundColour(wxColour(*wxWHITE));
    wxImageHandler *gifLoader = new wxGIFHandler();
    wxImage::AddHandler(gifLoader);
    this->LoadPotatoBitmap();
}
ImageWindow::~ImageWindow()
{
    delete potatoBitmap;
}
void ImageWindow::LoadPotatoBitmap()
{
    wxStandardPaths &stdPaths = wxStandardPaths::Get();
    wxString fileLocation = stdPaths.GetExecutablePath();
    wxImage image(wxT("A:\\Projects\\TestGif\\Assets\\Kuning.gif"),
        wxBITMAP_TYPE_GIF);
    potatoBitmap = new wxBitmap(image);
}
void ImageWindow::OnPaint(wxPaintEvent &event)
{
    wxPaintDC pdc(this);
    if (potatoBitmap != nullptr)
    {
        pdc.DrawBitmap(*potatoBitmap, wxPoint(150, 100), true);
    }
}

Upvotes: 2

Views: 1351

Answers (2)

Anil8753
Anil8753

Reputation: 2735

Use wxAnimationCtrl for playing gif animations or

Use extract each frame from the gif image and draw one by one using wxTimer.

Upvotes: 0

J.M. Robles
J.M. Robles

Reputation: 652

You are not specifying the index of the image in the constructor and that's why you see the default one as you can see in the wxWdigets documentation:

wxImage::wxImage    (   const wxString &    name,
    wxBitmapType    type = wxBITMAP_TYPE_ANY,
    int     index = -1 
)   

index Index of the image to load in the case that the image file contains multiple images. This is only used by GIF, ICO and TIFF handlers. The default value (-1) means "choose the default image" and is interpreted as the first image (index=0) by the GIF and TIFF handler and as the largest and most colourful one by the ICO handler.

But, you should also consider wxAnimationCtrl (see this sample)

Upvotes: 1

Related Questions