A memory device context provides a means to draw graphics onto a bitmap.
When drawing in to a mono-bitmap, using WHITE
, WHITE_PEN
and WHITE_BRUSH
will draw the background colour (i.e. 0) whereas all other colours will draw the foreground colour (i.e. 1).
A bitmap must be selected into the new memory DC before it may be used for anything. Typical usage is as follows:
# Create a memory DC
temp_dc = wx.MemoryDC()
temp_dc.SelectObject(test_bitmap)
# We can now draw into the memory DC...
# Copy from this DC to another DC.
old_dc.Blit(250, 50, BITMAP_WIDTH, BITMAP_HEIGHT, temp_dc, 0, 0)
Note that the memory DC must be deleted (or the bitmap selected out of it) before a bitmap can be reselected into another memory DC.
And, before performing any other operations on the bitmap data, the bitmap must be selected out of the memory DC:
temp_dc.SelectObject(wx.NullBitmap)
This happens automatically when wx.MemoryDC object goes out of scope.
Note that the scaling factor of the bitmap determines the scaling factor used by this device context, so when using a memory device context as a back buffer for a window, you should typically create the bitmap using the same scale factor as used by the window, e.g.
class MyWindow(wx.Window):
...
def OnPaint(self, event):
bmp = wx.Bitmap();
bmp.CreateWithDIPSize(self.GetClientSize(), self.GetDPIScaleFactor())
memdc = wx.MemoryDC(bmp)
... use memdc to draw on the bitmap ...
dc = wx.PaintDC(self)
dc.DrawBitmap(bmp, wx.Point(0, 0))
See also
Constructs a new memory device context. |
|
Allow using this device context object to modify the given bitmap contents. |
|
Selects the given bitmap into the device context, to use as the memory bitmap. |
wx.
MemoryDC
(DC)¶Possible constructors:
MemoryDC()
MemoryDC(dc)
MemoryDC(bitmap)
A memory device context provides a means to draw graphics onto a bitmap.
__init__
(self, *args, **kw)¶__init__ (self)
Constructs a new memory device context.
Use the wx.DC.IsOk
member to test whether the constructor was successful in creating a usable device context. Don’t forget to select a bitmap into the DC before drawing on it.
__init__ (self, dc)
Constructs a new memory device context having the same characteristics as the given existing device context.
This constructor creates a memory device context compatible with dc in wxMSW, the argument is ignored in the other ports. If dc is None
, a device context compatible with the screen is created, just as with the default constructor.
dc (wx.DC) –
__init__ (self, bitmap)
Constructs a new memory device context and calls SelectObject
with the given bitmap.
Use the wx.DC.IsOk
member to test whether the constructor was successful in creating a usable device context.
bitmap (wx.Bitmap) –
SelectObject
(self, bitmap)¶Allow using this device context object to modify the given bitmap contents.
Note that if you need to only use the existing bitmap contents instead of modifying it, you should use SelectObjectAsSource
instead.
Before using the updated bitmap data, make sure to select it out of context first either by selecting wx.NullBitmap
into the device context or destroying the device context entirely.
If the bitmap is already selected in this device context, nothing is done. If it is selected in another context, the function asserts and drawing on the bitmap won’t work correctly.
bitmap (wx.Bitmap) –
SelectObjectAsSource
(self, bitmap)¶Selects the given bitmap into the device context, to use as the memory bitmap.
Selecting the bitmap as source into a memory DC allows you to copy its contents to another device context using wx.DC.Blit
. Note that using wx.DC.DrawBitmap
or wx.DC.DrawIcon
is a simpler way to do the same thing.
If the argument is wx.NullBitmap
(or some other uninitialised wx.Bitmap) the current bitmap is selected out of the device context, allowing the current bitmap to be destroyed safely.
bitmap (wx.Bitmap) –
Note
Modifying a bitmap selected only as a source may not work correctly and can notably modify the other bitmaps sharing the same data due to the use of reference counting (see Reference Counting).
SelectedBitmap
¶