PDA

View Full Version : [SOLVED] [wxWidgets] Loading Icon from Char Array



dodle
December 15th, 2011, 04:09 AM
I'm trying to use a const unsigned char[] that is data from a .png image for an icon:


static const unsigned char tux_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x01, 0x90, 0x00, 0x00, 0x01, 0xdf,
0x08, 0x06, 0x00, 0x00, 0x00, 0x7e, 0xb1, 0x8e, 0x75, 0x00, 0x00, 0xa0,
0xdb, 0x49, 0x44, 0x41, 0x54, 0x78, 0x5e, 0xec, 0xdd, 0x6b, 0x4c, 0x55,
0xd9, 0x1d, 0xc6, 0xe1, 0x97, 0x9b, 0x47, 0xf0, 0x5e, 0x8c, 0x20, 0x58,
0x51, 0x90, 0x86, 0x20, 0x12, 0x2d, 0xa3, 0x1d, 0x7b, 0xb5, 0xde, 0xb0,
0x54, 0xc7, 0x68, 0x13, 0x75, 0x54, 0xda, 0x5a, 0x52, 0x3e, 0xd4, 0xcc,
0x68, 0x89, 0x71, 0xd0, 0x4c, 0xa3, 0xc9, 0x8c, 0x89, 0x75, 0x92, 0x2a,
0x99, 0x89, 0xf3, 0x61, 0x8c, 0x46, 0xc3, 0x28, 0xad, 0x97, 0x8e, 0xd7,
0xa9, 0xb5, 0xca, 0xb4, 0xd6, 0x8c, 0x53, 0x99, 0x58, 0x65, 0x94, 0x18,
0x71, 0x00, 0x15, 0x51,
...

But I can't figure out how to use it with wxIcon. I have read the docs (http://docs.wxwidgets.org/stable/wx_wxicon.html#wxicon) but still have not figured it out. I tried:


wxIcon icon(tux_png);

and


wxIcon icon(tux_png, 400, 479);

and some other things as well. Is there a way to convert data from a const char[] for use with wxIcon or wxBitmap?

Bachstelze
December 15th, 2011, 04:12 AM
Have you tried actually making it const char[], as opposed to const unsigned char[]?

dodle
December 15th, 2011, 04:28 AM
static const char tux_png[] = {
...
...

wxIcon icon(tux_png, 400, 479);
SetIcon(icon);


:/media/Data/Developing/test$ g++ test.cpp `wx-config --cxxflags --libs` -o test
test.cpp: In constructor ‘MainWindow::MainWindow(const wxString&)’:
test.cpp:17:34: error: conversion from ‘const char [41236]’ to ‘const wxString’ is ambiguous
/usr/include/wx-2.8/wx/string.h:692:3: note: candidates are: wxString::wxString(wxChar, size_t) <near match>
/usr/include/wx-2.8/wx/string.h:682:3: note: wxString::wxString(int) <near match>

----- EDIT -----


wxIcon* icon;
icon = tux_png;


:/media/Data/Developing/test$ g++ test.cpp `wx-config --cxxflags --libs` -o test
test.cpp: In constructor ‘MainWindow::MainWindow(const wxString&)’:
test.cpp:16:12: error: cannot convert ‘const char [41236]’ to ‘wxIcon*’ in assignment

cgroza
December 15th, 2011, 10:15 PM
You need to convert your data to a wxString in order to load it.
In general, wxWidgets does not play very well with other string representations, including a char array.

Bachstelze
December 15th, 2011, 10:53 PM
You need to convert your data to a wxString in order to load it.
In general, wxWidgets does not play very well with other string representations, including a char array.

If you read the documentation link provided, wxIcon has a constructor that takes a const char[] as parameter. The chars in this case do not represent characters in a string, but the binary data of the pixels in the icon.

dodle
December 16th, 2011, 08:38 PM
I've figured out (with help) how to load the data into a wxImage and wxBitmap. Can I convert one of those to wxIcon? I haven't been able to find a way as of yet, and wxIcon::LoadFile doesn't seem to work like wxImage::LoadFile.


wxPanel *bg = new wxPanel(this);

const int tux_png_size = sizeof(tux_png);
wxMemoryInputStream pngStream(tux_png, tux_png_size);

wxImage tux;
tux.LoadFile(pngStream, wxBITMAP_TYPE_PNG);

wxBitmap bmp(tux);

wxStaticBitmap *img = new wxStaticBitmap(bg, -1, bmp);


----- SOLVED -----

Thank you xaviou (http://forums.wxwidgets.org/viewtopic.php?f=1&t=33158&p=137896#p137896):

You can use the "CopyFromBitmap" member of wxIcon...


const int tux_png_size = sizeof(tux_png);
wxMemoryInputStream pngStream(tux_png, tux_png_size);

wxImage tux;
tux.LoadFile(pngStream, wxBITMAP_TYPE_PNG);

wxIcon icon;
icon.CopyFromBitmap(tux);