.. include:: headings.inc .. _Bitmap: ========================================================================================================================================== |phoenix_title| **Bitmap** ========================================================================================================================================== This class encapsulates the concept of a platform-dependent bitmap, either monochrome or colour or colour with alpha channel support. If you need direct access the bitmap data instead going through drawing to it using :ref:`MemoryDC` you need to use the `PixelData` class (either NativePixelData for ``RGB`` bitmaps or AlphaPixelData for bitmaps with an additionally alpha channel). Note that many :ref:`Bitmap` functions take a `type` parameter, which is a value of the :meth:`~Bitmap.BitmapType` enumeration. The validity of those values depends however on the platform where your program is running and from the wxWidgets configuration. If all possible wxWidgets settings are used: - wxMSW supports ``BMP`` and ``ICO`` files, ``BMP`` and ``ICO`` resources; - wxGTK supports ``XPM`` files; - Mac supports ``PICT`` resources; - X11 supports ``XPM`` files, ``XPM`` data, ``XBM`` data; In addition, :ref:`Bitmap` can load and save all formats that :ref:`Image` can; see :ref:`Image` for more info. Of course, you must have loaded the :ref:`Image` handlers (see :func:`InitAllImageHandlers` and :meth:`Image.AddHandler` ). Note that all available BitmapHandlers for a given wxWidgets port are automatically loaded at startup so you won't need to use :meth:`Bitmap.AddHandler` . More on the difference between :ref:`Image` and :ref:`Bitmap`: :ref:`Image` is just a buffer of ``RGB`` bytes with an optional buffer for the alpha bytes. It is all generic, platform independent and image file format independent code. It includes generic code for scaling, resizing, clipping, and other manipulations of the image data. OTOH, :ref:`Bitmap` is intended to be a wrapper of whatever is the native image format that is quickest/easiest to draw to a DC or to be the target of the drawing operations performed on a :ref:`MemoryDC`. By splitting the responsibilities between Image/wxBitmap like this then it's easier to use generic code shared by all platforms and image types for generic operations and platform specific code where performance or compatibility is needed. .. seealso:: :ref:`Bitmaps and Icons `, :ref:`Supported Bitmap File Formats `, :meth:`DC.Blit` , :ref:`Icon`, :ref:`Cursor`, :ref:`MemoryDC`, :ref:`Image`, `PixelData` | |class_hierarchy| Inheritance Diagram ===================================== Inheritance diagram for class **Bitmap** .. raw:: html

Inheritance diagram of Bitmap

| |method_summary| Methods Summary ================================ ================================================================================ ================================================================================ :meth:`~Bitmap.__init__` Default constructor. :meth:`~Bitmap.ConvertToDisabled` Returns disabled (dimmed) version of the bitmap. :meth:`~Bitmap.ConvertToImage` Creates an image from a platform-dependent bitmap. :meth:`~Bitmap.CopyFromBuffer` Copy data from a buffer object to replace the bitmap pixel data. :meth:`~Bitmap.CopyFromIcon` Creates the bitmap from an icon. :meth:`~Bitmap.CopyToBuffer` Copy pixel data to a buffer object. See `CopyFromBuffer` for buffer :meth:`~Bitmap.Create` Creates a fresh bitmap. :meth:`~Bitmap.FromBuffer` Creates a `wx.Bitmap` from in-memory data. The data parameter :meth:`~Bitmap.FromBufferAndAlpha` Creates a `wx.Bitmap` from in-memory data. The data and alpha :meth:`~Bitmap.FromBufferRGBA` Creates a `wx.Bitmap` from in-memory data. The data parameter :meth:`~Bitmap.FromRGBA` Creates a new empty 32-bit `wx.Bitmap` where every pixel has been :meth:`~Bitmap.GetDepth` Gets the colour depth of the bitmap. :meth:`~Bitmap.GetHandle` MSW-only method to fetch the windows handle for the bitmap. :meth:`~Bitmap.GetHeight` Gets the height of the bitmap in pixels. :meth:`~Bitmap.GetMask` Gets the associated mask (if any) which may have been loaded from a file or set for the bitmap. :meth:`~Bitmap.GetPalette` Gets the associated palette (if any) which may have been loaded from a file or set for the bitmap. :meth:`~Bitmap.GetSize` Returns the size of the bitmap in pixels. :meth:`~Bitmap.GetSubBitmap` Returns a sub bitmap of the current one as long as the rect belongs entirely to the bitmap. :meth:`~Bitmap.GetWidth` Gets the width of the bitmap in pixels. :meth:`~Bitmap.IsOk` Returns ``True`` if bitmap data is present. :meth:`~Bitmap.LoadFile` Loads a bitmap from a file or resource. :meth:`~Bitmap.SaveFile` Saves a bitmap in the named file. :meth:`~Bitmap.SetDepth` Sets the depth member (does not affect the bitmap data). :meth:`~Bitmap.SetHandle` MSW-only method to set the windows handle for the bitmap. :meth:`~Bitmap.SetHeight` Sets the height member (does not affect the bitmap data). :meth:`~Bitmap.SetMask` Sets the mask for this bitmap. :meth:`~Bitmap.SetMaskColour` :meth:`~Bitmap.SetPalette` Sets the associated palette. :meth:`~Bitmap.SetSize` Set the bitmap size (does not affect the existing bitmap data). :meth:`~Bitmap.SetWidth` Sets the width member (does not affect the bitmap data). :meth:`~Bitmap.__nonzero__` ================================================================================ ================================================================================ | |property_summary| Properties Summary ===================================== ================================================================================ ================================================================================ :attr:`~Bitmap.Depth` See :meth:`~Bitmap.GetDepth` and :meth:`~Bitmap.SetDepth` :attr:`~Bitmap.Handle` See :meth:`~Bitmap.GetHandle` and :meth:`~Bitmap.SetHandle` :attr:`~Bitmap.Height` See :meth:`~Bitmap.GetHeight` and :meth:`~Bitmap.SetHeight` :attr:`~Bitmap.Mask` See :meth:`~Bitmap.GetMask` and :meth:`~Bitmap.SetMask` :attr:`~Bitmap.Palette` See :meth:`~Bitmap.GetPalette` and :meth:`~Bitmap.SetPalette` :attr:`~Bitmap.Size` See :meth:`~Bitmap.GetSize` and :meth:`~Bitmap.SetSize` :attr:`~Bitmap.Width` See :meth:`~Bitmap.GetWidth` and :meth:`~Bitmap.SetWidth` ================================================================================ ================================================================================ | |api| Class API =============== .. class:: Bitmap(GDIObject) This class encapsulates the concept of a platform-dependent bitmap, either monochrome or colour or colour with alpha channel support. **Possible constructors**:: Bitmap() Bitmap(bitmap) Bitmap(bits, width, height, depth=1) Bitmap(width, height, depth=BITMAP_SCREEN_DEPTH) Bitmap(sz, depth=BITMAP_SCREEN_DEPTH) Bitmap(name, type=BITMAP_TYPE_ANY) Bitmap(img, depth=BITMAP_SCREEN_DEPTH) .. method:: __init__(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **__init__** `(self)` Default constructor. Constructs a bitmap object with no data; an assignment or another member function such as :meth:`Create` or :meth:`LoadFile` must be called subsequently. **~~~** **__init__** `(self, bitmap)` Copy constructor, uses :ref:`reference counting `. To make a real copy, you can use: :: newBitmap = oldBitmap.GetSubBitmap( wx.Rect(0, 0, oldBitmap.GetWidth(), oldBitmap.GetHeight())) :param `bitmap`: :type `bitmap`: Bitmap **~~~** **__init__** `(self, bits, width, height, depth=1)` Creates a bitmap from the given array `bits`. You should only use this function for monochrome bitmaps (depth 1) in portable programs: in this case the bits parameter should contain an ``XBM`` image. For other bit depths, the behaviour is platform dependent: under Windows, the data is passed without any changes to the underlying CreateBitmap() API. Under other platforms, only monochrome bitmaps may be created using this constructor and :ref:`Image` should be used for creating colour bitmaps from static data. :param `bits`: Specifies an array of pixel values. :type `bits`: string :param `width`: Specifies the width of the bitmap. :type `width`: int :param `height`: Specifies the height of the bitmap. :type `height`: int :param `depth`: Specifies the depth of the bitmap. If this is omitted, then a value of 1 (monochrome bitmap) is used. :type `depth`: int **~~~** **__init__** `(self, width, height, depth=BITMAP_SCREEN_DEPTH)` Creates a new bitmap. A depth of ``BITMAP_SCREEN_DEPTH`` indicates the depth of the current screen or visual. Some platforms only support 1 for monochrome and ``BITMAP_SCREEN_DEPTH`` for the current colour setting. A depth of 32 including an alpha channel is supported under MSW, Mac and GTK+. :param `width`: :type `width`: int :param `height`: :type `height`: int :param `depth`: :type `depth`: int **~~~** **__init__** `(self, sz, depth=BITMAP_SCREEN_DEPTH)` This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. :param `sz`: :type `sz`: Size :param `depth`: :type `depth`: int **~~~** **__init__** `(self, name, type=BITMAP_TYPE_ANY)` Loads a bitmap from a file or resource. :param `name`: This can refer to a resource name or a filename under MS Windows and X. Its meaning is determined by the `type` parameter. :type `name`: string :param `type`: May be one of the :ref:`BitmapType` values and indicates which type of bitmap should be loaded. See the note in the class detailed description. Note that the ``BITMAP_DEFAULT_TYPE`` constant has different value under different wxWidgets ports. See the bitmap.h header for the value it takes for a specific port. :type `type`: BitmapType .. seealso:: :meth:`LoadFile` **~~~** **__init__** `(self, img, depth=BITMAP_SCREEN_DEPTH)` Creates this bitmap object from the given image. 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 provided colour depth (or that of the current system if depth is ``BITMAP_SCREEN_DEPTH``) which entails that a colour reduction may take place. When in 8-bit mode (PseudoColour mode), the GTK port will use a color cube created on program start-up to look up colors. This ensures a very fast conversion, but the image quality won't be perfect (and could be better for photo images using more sophisticated dithering algorithms). On Windows, if there is a palette present (set with SetPalette), it will be used when creating the :ref:`Bitmap` (most useful in 8-bit display mode). On other platforms, the palette is currently ignored. :param `img`: Platform-independent :ref:`Image` object. :type `img`: Image :param `depth`: Specifies the depth of the bitmap. If this is omitted, the display depth of the screen is used. :type `depth`: int **~~~** .. method:: ConvertToDisabled(self, brightness=255) Returns disabled (dimmed) version of the bitmap. This method is not available when ``USE_IMAGE == 0`` . :param `brightness`: :type `brightness`: int :rtype: :ref:`Bitmap` .. versionadded:: 2.9.0 .. method:: ConvertToImage(self) Creates 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. :rtype: :ref:`Image` .. method:: CopyFromBuffer(self, data, format=BitmapBufferFormat_RGB, stride=-1) Copy data from a buffer object to replace the bitmap pixel data. Default format is plain ``RGB``, but other formats are now supported as well. The following symbols are used to specify the format of the bytes in the buffer: ============================= ================================ wx.BitmapBufferFormat_RGB A simple sequence of ``RGB`` bytes wx.BitmapBufferFormat_RGBA A simple sequence of ``RGBA`` bytes wx.BitmapBufferFormat_ARGB32 A sequence of 32-bit values in native endian order, with alpha in the upper 8 bits, followed by red, green, and blue. wx.BitmapBufferFormat_RGB32 Same as above but the alpha byte is ignored. ============================= ================================ .. method:: CopyFromIcon(self, icon) Creates the bitmap from an icon. :param `icon`: :type `icon`: Icon :rtype: `bool` .. method:: CopyToBuffer(self, data, format=BitmapBufferFormat_RGB, stride=-1) Copy pixel data to a buffer object. See `CopyFromBuffer` for buffer format details. .. method:: Create(self, *args, **kw) |overload| **Overloaded Implementations**: **~~~** **Create** `(self, width, height, depth=BITMAP_SCREEN_DEPTH)` Creates a fresh bitmap. If the final argument is omitted, the display depth of the screen is used. :param `width`: :type `width`: int :param `height`: :type `height`: int :param `depth`: :type `depth`: int :rtype: `bool` :returns: ``True`` if the creation was successful. **~~~** **Create** `(self, sz, depth=BITMAP_SCREEN_DEPTH)` This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. :param `sz`: :type `sz`: Size :param `depth`: :type `depth`: int :rtype: `bool` **~~~** .. staticmethod:: FromBuffer(width, height, data) Creates a `wx.Bitmap` from in-memory data. The data parameter must be a Python object that implements the buffer interface, such as a string, bytearray, etc. The data object is expected to contain a series of ``RGB`` bytes and be at least widthheight3 bytes long. Unlike `wx.ImageFromBuffer` the bitmap created with this function does not share the memory block with the buffer object. This is because the native pixel buffer format varies on different platforms, and so instead an efficient as possible copy of the data is made from the buffer object to the bitmap's native pixel buffer. :rtype: :ref:`Bitmap` .. staticmethod:: FromBufferAndAlpha(width, height, data, alpha) Creates a `wx.Bitmap` from in-memory data. The data and alpha parameters must be a Python object that implements the buffer interface, such as a string, bytearray, etc. The data object is expected to contain a series of ``RGB`` bytes and be at least widthheight3 bytes long, while the alpha object is expected to be widthheight bytes long and represents the image's alpha channel. On Windows and Mac the ``RGB`` values will be 'premultiplied' by the alpha values. (The other platforms do the multiplication themselves.) Unlike `wx.ImageFromBuffer` the bitmap created with this function does not share the memory block with the buffer object. This is because the native pixel buffer format varies on different platforms, and so instead an efficient as possible copy of the data is made from the buffer object to the bitmap's native pixel buffer. :rtype: :ref:`Bitmap` .. staticmethod:: FromBufferRGBA(width, height, data) Creates a `wx.Bitmap` from in-memory data. The data parameter must be a Python object that implements the buffer interface, such as a string, bytearray, etc. The data object is expected to contain a series of ``RGBA`` bytes and be at least widthheight4 bytes long. On Windows and Mac the ``RGB`` values will be 'premultiplied' by the alpha values. (The other platforms do the multiplication themselves.) Unlike `wx.ImageFromBuffer` the bitmap created with this function does not share the memory block with the buffer object. This is because the native pixel buffer format varies on different platforms, and so instead an efficient as possible copy of the data is made from the buffer object to the bitmap's native pixel buffer. :rtype: :ref:`Bitmap` .. staticmethod:: FromRGBA(width, height, red=0, green=0, blue=0, alpha=0) Creates a new empty 32-bit `wx.Bitmap` where every pixel has been initialized with the given ``RGBA`` values. :rtype: :ref:`Bitmap` .. method:: GetDepth(self) Gets the colour depth of the bitmap. A value of 1 indicates a monochrome bitmap. :rtype: `int` .. method:: GetHandle(self) MSW-only method to fetch the windows handle for the bitmap. :rtype: `long` .. method:: GetHeight(self) Gets the height of the bitmap in pixels. :rtype: `int` .. seealso:: :meth:`GetWidth` , :meth:`GetSize` .. method:: GetMask(self) Gets the associated mask (if any) which may have been loaded from a file or set for the bitmap. :rtype: :ref:`Mask` .. seealso:: :meth:`SetMask` , :ref:`Mask` .. method:: GetPalette(self) Gets the associated palette (if any) which may have been loaded from a file or set for the bitmap. :rtype: `Palette` .. seealso:: `Palette` .. method:: GetSize(self) Returns the size of the bitmap in pixels. :rtype: :ref:`Size` .. versionadded:: 2.9.0 .. seealso:: :meth:`GetHeight` , :meth:`GetWidth` .. method:: GetSubBitmap(self, rect) Returns a sub bitmap of the current one as long as the rect belongs entirely to the bitmap. This function preserves bit depth and mask information. :param `rect`: :type `rect`: Rect :rtype: :ref:`Bitmap` .. method:: GetWidth(self) Gets the width of the bitmap in pixels. :rtype: `int` .. seealso:: :meth:`GetHeight` , :meth:`GetSize` .. method:: IsOk(self) Returns ``True`` if bitmap data is present. :rtype: `bool` .. method:: LoadFile(self, name, type=BITMAP_TYPE_ANY) Loads a bitmap from a file or resource. :param `name`: Either a filename or a Windows resource name. The meaning of name is determined by the `type` parameter. :type `name`: string :param `type`: One of the :ref:`BitmapType` values; see the note in the class detailed description. Note that the ``BITMAP_DEFAULT_TYPE`` constant has different value under different wxWidgets ports. See the bitmap.h header for the value it takes for a specific port. :type `type`: BitmapType :rtype: `bool` :returns: ``True`` if the operation succeeded, ``False`` otherwise. .. note:: A palette may be associated with the bitmap if one exists (especially for colour Windows bitmaps), and if the code supports it. You can check if one has been created by using the :meth:`GetPalette` member. .. seealso:: :meth:`SaveFile` .. method:: SaveFile(self, name, type, palette=None) Saves a bitmap in the named file. :param `name`: A filename. The meaning of name is determined by the type parameter. :type `name`: string :param `type`: One of the :ref:`BitmapType` values; see the note in the class detailed description. :type `type`: BitmapType :param `palette`: An optional palette used for saving the bitmap. :type `palette`: Palette :rtype: `bool` :returns: ``True`` if the operation succeeded, ``False`` otherwise. .. note:: Depending on how wxWidgets has been configured, not all formats may be available. .. seealso:: :meth:`LoadFile` .. method:: SetDepth(self, depth) Sets the depth member (does not affect the bitmap data). :param `depth`: Bitmap depth. :type `depth`: int .. todo:: since these functions do not affect the bitmap data, why they exist?? .. method:: SetHandle(self, handle) MSW-only method to set the windows handle for the bitmap. .. method:: SetHeight(self, height) Sets the height member (does not affect the bitmap data). :param `height`: Bitmap height in pixels. :type `height`: int .. method:: SetMask(self, mask) Sets the mask for this bitmap. :param `mask`: :type `mask`: Mask .. note:: The bitmap object owns the mask once this has been called. .. seealso:: :meth:`GetMask` , :ref:`Mask` .. method:: SetMaskColour(self, colour) .. method:: SetPalette(self, palette) Sets the associated palette. (Not implemented under GTK+). :param `palette`: The palette to set. :type `palette`: Palette .. seealso:: `Palette` .. method:: SetSize(self, size) Set the bitmap size (does not affect the existing bitmap data). .. method:: SetWidth(self, width) Sets the width member (does not affect the bitmap data). :param `width`: Bitmap width in pixels. :type `width`: int .. method:: __nonzero__(self) :rtype: `int` .. attribute:: Depth See :meth:`~Bitmap.GetDepth` and :meth:`~Bitmap.SetDepth` .. attribute:: Handle See :meth:`~Bitmap.GetHandle` and :meth:`~Bitmap.SetHandle` .. attribute:: Height See :meth:`~Bitmap.GetHeight` and :meth:`~Bitmap.SetHeight` .. attribute:: Mask See :meth:`~Bitmap.GetMask` and :meth:`~Bitmap.SetMask` .. attribute:: Palette See :meth:`~Bitmap.GetPalette` and :meth:`~Bitmap.SetPalette` .. attribute:: Size See :meth:`~Bitmap.GetSize` and :meth:`~Bitmap.SetSize` .. attribute:: Width See :meth:`~Bitmap.GetWidth` and :meth:`~Bitmap.SetWidth`