如何使用 wxWidgets c++ 加载 gif

How to load gif using wxWidgets c++?

本文关键字:加载 gif c++ wxWidgets 何使用      更新时间:2023-10-16

我需要你的帮助!我目前对wxWidgets非常陌生,作为一名大学生。我正在尝试使用 wxWidgets 加载.gif文件。我尝试使用以下代码...但它只加载 1 帧,它不会移动/更改为其他帧,它基本上是一张静止图片。该文件是扩展名.gif,它有 3 帧左右。图像的位置已经正确。帮助将不胜感激!

谢谢

#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);
    }
}

您没有在构造函数中指定图像的索引,这就是为什么您看到默认索引的原因,如您在 wxWdigets 文档中看到的那样:

wxImage::wxImage    (   const wxString &    name,
    wxBitmapType    type = wxBITMAP_TYPE_ANY,
    int     index = -1 
)   
索引 在图像

文件包含多个图像的情况下要加载的图像的索引。这仅由GIF,ICO和TIFF处理程序使用。默认值(-1(表示"选择默认图像",被GIF和TIFF处理程序解释为第一个图像(index=0(,被ICO处理程序解释为最大和最丰富多彩的图像。

但是,您还应该考虑 wxAnimationCtrl(请参阅此示例(

使用 wxAnimationCtrl 播放 gif 动画或

使用从 gif 图像中提取每一帧并使用 wxTimer 逐个绘制。