在wxWidgets中如何显示静态图片?

3
我已经尝试了几天在wxWidgets对话框中从内存中显示BMP图像,但是我的所有尝试都失败了。
首先,我尝试在对话框中创建一个wxStaticBitmap控件:
// in class declaration inside header file
  wxStaticBitmap *ibitmap;

// in wxDialog constructor
// MyLogo is an array of `unsigned char`
// contains the bitmap file (Yes, the bitmap file, with BMP header)
ibitmap = new wxStaticBitmap(mainPanel, 4000, wxBitmap(MyLogo, wxBITMAP_TYPE_BMP, 200, 62), wxPoint(10, 10), wxSize(200, 62));

我没有收到错误信息,但是图片没有显示出来。其次,我尝试在对话框的EVT_PAINT事件中绘制这张图片:
// in the class declaration inside header file
  wxBitmap *ibitmap;

// in the events declaration
  EVT_PAINT(OnPaint)

// in wxDialog constructor
ibitmap = new wxBitmap(MyLogo, wxBITMAP_TYPE_BMP, 200, 62);

// event method implementation
void MyDialog::OnPaint(wxPaintEvent &event)
{
  wxPaintDC dc(this);
  dc.DrawBitmap(*ibitmap, 10, 10);
}

现在我收到了这个调试警告: http://img266.imageshack.us/img266/9512/wxerror.jpg 调试器停在了这一行:
// dc.h Ln 271
{ DoDrawBitmap(bmp, x, y, useMask); }

请问有人能指引我吗?
1个回答

1

您的位图未能正确加载。根据wxWidgets文档,您想要使用的wxBitmap构造函数具有以下签名:

 wxBitmap(const char bits[], int width, int height, int depth=1)

所以你最终应该得到类似这样的东西:

wxBitmap(MyLogo, 200, 62, 3)

假设是RGB位图。

我百分之百确定该图像是一个有效的24位位图转换成字节数组。 - Yana D. Nugraha
1
在错误对话框中,您显示调用dc.DrawBitmap断言bmp.Ok(),这意味着无论dc.DrawBitmap被要求绘制的位图是什么,都是OK的。 - heavyd
1
@djzmo:当你不确定时,使用调试器逐步查看wxWidgets代码。我通过这种方法发现了许多未记录的异常(例如wxDbTable中未记录的项目)。 - Thomas Matthews
1
没错 - 这可能是一个正确的数组,但请注意 heavyd 的代码没有使用 wxBITMAP_TYPE_BMP 参数。该参数用于从文件加载位图,但是您已经有了一个内存块,因此不需要它。 - RyanWilcox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接