+class MyImageFrame : public wxFrame
+{
+public:
+ MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
+ : wxFrame(parent, -1, _T("Double click to save"),
+ wxDefaultPosition, wxDefaultSize,
+ wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX),
+ m_bitmap(bitmap)
+ {
+ SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
+ }
+
+ void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
+ {
+ // do nothing here to be able to see how transparent images are shown
+ }
+
+ void OnPaint(wxPaintEvent& WXUNUSED(event))
+ {
+ wxPaintDC dc( this );
+ dc.DrawBitmap( m_bitmap, 0, 0, TRUE /* use mask */ );
+ }
+
+ void OnSave(wxMouseEvent& WXUNUSED(event))
+ {
+ wxImage image = m_bitmap.ConvertToImage();
+
+ int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
+ _T("Set BMP BPP"),
+ nChoices,
+ bppchoices,
+ this);
+ if ( bppselection == -1 )
+ {
+ // cancelled
+ return;
+ }
+
+ image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]);
+
+ wxString deffilename = bppchoices[bppselection];
+ deffilename.Replace(wxT(" "), wxT("_"));
+ deffilename += wxT(".bmp");
+ wxString savefilename = wxFileSelector( wxT("Save Image"),
+ wxT(""),
+ deffilename,
+ (const wxChar *)NULL,
+ wxT("BMP files (*.bmp)|*.bmp|")
+ wxT("PNG files (*.png)|*.png|")
+ wxT("JPEG files (*.jpg)|*.jpg|")
+ wxT("GIF files (*.gif)|*.gif|")
+ wxT("TIFF files (*.tif)|*.tif|")
+ wxT("PCX files (*.pcx)|*.pcx|")
+ wxT("ICO files (*.ico)|*.ico|")
+ wxT("CUR files (*.cur)|*.cur"),
+ wxSAVE);
+
+ if ( savefilename.empty() )
+ return;
+
+ if ( image.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT) == wxBMP_8BPP_PALETTE )
+ {
+ unsigned char *cmap = new unsigned char [256];
+ for ( int i = 0; i < 256; i++ )
+ cmap[i] = i;
+ image.SetPalette(wxPalette(256, cmap, cmap, cmap));
+
+ delete cmap;
+ }
+
+ bool loaded;
+ wxString extension = savefilename.AfterLast('.').Lower();
+
+ if (extension == _T("cur"))
+ {
+ image.Rescale(32,32);
+ image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
+ image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
+ // This shows how you can save an image with explicitly
+ // specified image format:
+ loaded = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
+ }
+ else
+ {
+ // This one guesses image format from filename extension
+ // (it may fail if the extension is not recognized):
+ loaded = image.SaveFile(savefilename);
+ }
+
+ if ( !loaded )
+ wxMessageBox(_T("No handler for this file type."),
+ _T("File was not saved"),
+ wxOK|wxCENTRE, this);
+ }
+
+private:
+ wxBitmap m_bitmap;
+
+ DECLARE_EVENT_TABLE()
+};
+
+#ifdef wxHAVE_RAW_BITMAP
+
+#include "wx/rawbmp.h"
+
+class MyRawBitmapFrame : public wxFrame
+{
+public:
+ enum
+ {
+ BORDER = 15,
+ SIZE = 150,
+ REAL_SIZE = SIZE - 2*BORDER
+ };
+
+ MyRawBitmapFrame(wxFrame *parent)
+ : wxFrame(parent, -1, _T("Raw bitmaps (how exciting)")),
+ m_bitmap(SIZE, SIZE, 32)
+ {
+ SetClientSize(SIZE, SIZE);
+
+ wxRawBitmapData data(m_bitmap);
+ if ( !data )
+ {
+ wxLogError(_T("Failed to gain raw access to bitmap data"));
+ return;
+ }
+
+ wxRawBitmapIterator p(data);
+
+ p.Offset(BORDER, BORDER);
+
+ for ( int y = 0; y < REAL_SIZE; ++y )
+ {
+ wxRawBitmapIterator rowStart = p;
+
+ int r = y < REAL_SIZE/3 ? 255 : 0,
+ g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
+ b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
+
+ for ( int x = 0; x < REAL_SIZE; ++x )
+ {
+ p.Red() = r;
+ p.Green() = g;
+ p.Blue() = b;
+ p.Alpha() = x;
+
+ ++p; // same as p.OffsetX(1)
+ }
+
+ p = rowStart;
+ p.OffsetY(1);
+ }
+ }
+
+ void OnPaint(wxPaintEvent& WXUNUSED(event))
+ {
+ wxPaintDC dc( this );
+ dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER);
+ dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER);
+ dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER);
+ dc.DrawBitmap( m_bitmap, 0, 0, TRUE /* use mask */ );
+ }
+
+private:
+ wxBitmap m_bitmap;
+
+ DECLARE_EVENT_TABLE()
+};
+
+#endif // wxHAVE_RAW_BITMAP
+