【问题标题】:How to load gif using wxWidgets c++?如何使用 wxWidgets c++ 加载 gif?
【发布时间】:2017-11-12 06:41:30
【问题描述】:

我需要你的帮助!作为一名大学生,我目前对 wxWidgets 非常陌生。我正在尝试使用 wxWidgets 加载 .gif 文件。我尝试使用下面的代码......但它只加载一帧,它不会移动/更改为其他帧,它基本上是一张静止的图片。该文件是.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);
    }
}

【问题讨论】:

  • static int GetImageCount (const wxString &amp;filename, wxBitmapType type=wxBITMAP_TYPE_ANY) 然后你必须手动绘制每一帧(图像)。

标签: c++ wxwidgets


【解决方案1】:

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

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

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

但是,您还应该考虑 wxAnimationCtrl(请参阅 this sample

【讨论】:

  • 我加了-1,但结果还是一样。 ://
  • 默认值为-1,表示引用的意思。如果您省略放置任何内容,则使用默认值。请检查 0,1,2 等以获得不那么无聊的东西。
【解决方案2】:

使用wxAnimationCtrl 播放 gif 动画 或

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

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多