This class encapsulates a platform-independent image. An image can be created from data, or using the constructor taking a wxBitmap object. An image can be loaded from a file in a variety of formats, and is extensible to new formats via image format handlers. Functions are available to set and get image bits, so it can be used for basic image manipulation.
A wxImage cannot (currently) be drawn directly to a wxDC. Instead, a platform-specific wxBitmap object must be created from it, and that bitmap drawn on the wxDC, using wxDC::DrawBitmap.
Derived from
Include files
<wx/image.h>
See also
wxBitmap wxInitAllImageHandlers
Members
wxImage::wxImage
wxImage::~wxImage
wxImage::AddHandler
wxImage::CleanUpHandlers
wxImage::ConvertToBitmap
wxImage::Create
wxImage::Destroy
wxImage::FindHandler
wxImage::GetBlue
wxImage::GetData
wxImage::GetGreen
wxImage::GetRed
wxImage::GetHandlers
wxImage::GetHeight
wxImage::GetMaskBlue
wxImage::GetMaskGreen
wxImage::GetMaskRed
wxImage::GetSubImage
wxImage::GetWidth
wxImage::HasMask
wxImage::InitStandardHandlers
wxImage::InsertHandler
wxImage::LoadFile
wxImage::Ok
wxImage::RemoveHandler
wxImage::SaveFile
wxImage::Rescale
wxImage::Scale
wxImage::SetData
wxImage::SetMask
wxImage::SetMaskColour
wxImage::SetRGB
wxImage::operator =
wxImage::operator ==
wxImage::operator !=
wxImage()
Default constructor.
wxImage(const wxImage& image)
Copy constructor.
wxImage(const wxBitmap& bitmap)
Constructs an image from a platform-dependent bitmap. This preserves mask information so that bitmaps and images can be converted back and forth without loss in that respect.
wxImage(int width, int height)
Creates an image with the given width and height.
wxImage(const wxString& name, long type = wxBITMAP_TYPE_PNG)
wxImage(const wxString& name, const wxString& mimetype)
Loads an image from a file.
wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_PNG)
wxImage(wxInputStream& stream, const wxString& mimetype)
Loads an image from an input stream.
Parameters
width
height
name
stream
type
wxBITMAP_TYPE_BMP | Load a Windows bitmap file. |
wxBITMAP_TYPE_PNG | Load a PNG bitmap file. |
wxBITMAP_TYPE_JPEG | Load a JPEG bitmap file. |
The validity of these flags depends on the platform and wxWindows configuration. If all possible wxWindows settings are used, the loading a BMP (Windows bitmap) file, a PNG (portable network graphics) file and a JPEG file is supported on all platforms that implement wxImage.
mimetype
Note : you must call wxImage::AddHandler(new wxJPEGHandler) during application initialization in order to work with JPEGs.
See also
wxPython note: Constructors supported by wxPython are:
wxImage(name, flag) | Loads an image from a file |
wxNullImage() | Create a null image (has no size or image data) |
wxEmptyImage(width, height) | Creates an empty image of the given size |
wxImageFromMime(name, mimetype | Creates an image from the given file of the given mimetype |
wxImageFromBitmap(bitmap) | Creates an image from a platform-dependent bitmap |
~wxImage()
Destructor.
static void AddHandler(wxImageHandler* handler)
Adds a handler to the end of the static list of format handlers.
handler
See also
wxPython note: In wxPython this static method is named wxImage_AddHandler.
static void CleanUpHandlers()
Deletes all image handlers.
This function is called by wxWindows on exit.
wxBitmap ConvertToBitmap() const
Converts the image to a platform-specific bitmap object. This has to be done to actually display an image as you cannot draw an image directly on a window. The resulting bitmap will use the colour depth of the current system which entails that a (crude) colour reduction has to take place. When in 8-bit mode, this routine will use a color cube created on program start-up to look up colors. Still, the image quality won't be perfect for photo images.
bool Create(int width, int height)
Creates a fresh image.
Parameters
width
height
Return value
TRUE if the call succeeded, FALSE otherwise.
bool Destroy()
Destroys the image data.
static wxImageHandler* FindHandler(const wxString& name)
Finds the handler with the given name.
static wxImageHandler* FindHandler(const wxString& extension, long imageType)
Finds the handler associated with the given extension and type.
static wxImageHandler* FindHandler(long imageType)
Finds the handler associated with the given image type.
static wxImageHandler* FindHandlerMime(const wxString& mimetype)
Finds the handler associated with the given MIME type.
name
extension
imageType
mimetype
Return value
A pointer to the handler if found, NULL otherwise.
See also
unsigned char GetBlue(int x, int y) const
Returns the blue intensity at the given coordinate.
unsigned char* GetData() const
Returns the image data as an array. This is most often used when doing direct image manipulation. The return value points to an array of chararcters in RGBGBRGB... format.
unsigned char GetGreen(int x, int y) const
Returns the green intensity at the given coordinate.
unsigned char GetRed(int x, int y) const
Returns the red intensity at the given coordinate.
static wxList& GetHandlers()
Returns the static list of image format handlers.
See also
int GetHeight() const
Gets the height of the image in pixels.
unsigned char GetMaskBlue() const
Gets the blue value of the mask colour.
unsigned char GetMaskGreen() const
Gets the green value of the mask colour.
unsigned char GetMaskRed() const
Gets the red value of the mask colour.
wxImage GetSubImage(const wxRect&rect) const
Returns a sub image of the current one as long as the rect belongs entirely to the image.
int GetWidth() const
Gets the width of the image in pixels.
See also
bool HasMask() const
Returns TRUE if there is a mask active, FALSE otherwise.
static void InitStandardHandlers()
Internal use only. Adds standard image format handlers. It only install BMP for the time being, which is use by wxBitmap.
This function is called by wxWindows on startup, and shouldn't be called by the user.
See also
wxImageHandler wxInitAllImageHandlers
static void InsertHandler(wxImageHandler* handler)
Adds a handler at the start of the static list of format handlers.
handler
See also
bool LoadFile(const wxString& name, long type = wxBITMAP_TYPE_ANY)
bool LoadFile(const wxString& name, const wxString& mimetype)
Loads an image from a file. If no handler type is provided, the library will try to use wxBITMAP_TYPE_BMP.
bool LoadFile(wxInputStream& stream, long type)
bool LoadFile(wxInputStream& stream, const wxString& mimetype)
Loads an image from an input stream.
Parameters
name
stream
type
wxBITMAP_TYPE_BMP | Load a Windows image file. |
wxBITMAP_TYPE_GIF | Load a GIF image file. |
wxBITMAP_TYPE_JPEG | Load a JPEG image file. |
wxBITMAP_TYPE_PCX | Load a PCX image file. |
wxBITMAP_TYPE_PNG | Load a PNG image file. |
wxBITMAP_TYPE_PNM | Load a PNM image file. |
The validity of these flags depends on the platform and wxWindows configuration.
mimetype
Return value
TRUE if the operation succeeded, FALSE otherwise.
See also
wxPython note: In place of a single overloaded method name, wxPython implements the following methods:
LoadFile(filename, type) | Loads an image of the given type from a file |
LoadMimeFile(filename, mimetype) | Loads an image of the given mimetype from a file |
bool Ok() const
Returns TRUE if image data is present.
static bool RemoveHandler(const wxString& name)
Finds the handler with the given name, and removes it. The handler is not deleted.
name
Return value
TRUE if the handler was found and removed, FALSE otherwise.
See also
bool SaveFile(const wxString& name, int type)
bool SaveFile(const wxString& name, const wxString& mimetype)
Saves a image in the named file.
bool SaveFile(wxOutputStream& stream, int type)
bool SaveFile(wxOutputStream& stream, const wxString& mimetype)
Saves a image in the given stream.
Parameters
name
stream
type
wxBITMAP_TYPE_PNG | Save a PNG image file. |
wxBITMAP_TYPE_JPEG | Save a JPEG image file. |
The validity of these flags depends on the platform and wxWindows configuration as well as user-added handlers.
mimetype
Return value
TRUE if the operation succeeded, FALSE otherwise.
Remarks
Depending on how wxWindows has been configured, not all formats may be available.
See also
wxPython note: In place of a single overloaded method name, wxPython implements the following methods:
SaveFile(filename, type) | Saves the image using the given type to the named file |
SaveMimeFile(filename, mimetype) | Saves the image using the given mimetype to the named file |
wxImage Rescale(int width, int height)
Changes the size of the image in-place: after a call to this function, the image will have the given width and height.
See also
wxImage Scale(int width, int height) const
Returns a scaled version of the image. This is also useful for scaling bitmaps in general as the only other way to scale bitmaps is to blit a wxMemoryDC into another wxMemoryDC.
It may be mentioned that the GTK post uses this function internally to scale bitmaps when using mapping mode in wxDC.
Example:
// get the bitmap from somewhere wxBitmap bmp = ...; // rescale it to have size of 32*32 if ( bmp.GetWidth() != 32 || bmp.GetHeight() != 32 ) { wxImage image(bmp); bmp = image.Scale(32, 32).ConvertToBitmap(); // another possibility: image.Rescale(32, 32); bmp = image; }See also
void SetData(unsigned char*data)
Sets the image data without performing checks. The data given must have the size (width*height*3) or results will be unexpected. Don't use this method if you aren't sure you know what you are doing.
void SetMask(bool hasMask = TRUE)
Specifies whether there is a mask or not. The area of the mask is determined by the current mask colour.
void SetMaskColour(unsigned char red, unsigned char blue, unsigned char green)
Sets the mask colour for this image (and tells the image to use the mask).
void SetRGB(int x, int y, unsigned char red, unsigned char blue, unsigned char green)
Sets the pixel at the given coordinate. This routine performs bounds-checks for the coordinate so it can be considered a safe way to manipulate the data, but in some cases this might be too slow so that the data will have to be set directly. In that case you have to get that data by calling GetData().
wxImage& operator =(const wxImage& image)
Assignment operator. This operator does not copy any data, but instead passes a pointer to the data in image and increments a reference counter. It is a fast operation.
Parameters
image
Return value
Returns 'this' object.
bool operator ==(const wxImage& image)
Equality operator. This operator tests whether the internal data pointers are equal (a fast test).
Parameters
image
Return value
Returns TRUE if the images were effectively equal, FALSE otherwise.
bool operator !=(const wxImage& image)
Inequality operator. This operator tests whether the internal data pointers are unequal (a fast test).
Parameters
image
Return value
Returns TRUE if the images were unequal, FALSE otherwise.