Reputation: 111
Hey I am trying to draw a rectangle on top of an image when the user clicks a spot on the graph.
To scroll through different graphs, I’m using a staticBitmap. Unfortunately, just about every try with the DCs have been unsuccessful. Both PaintDC and BufferedDC sometimes cause an infinite loop to occur and other times put the drawing behind the image. ClientDC shows the box I drew, but it disappears when I resize. Creating the drawing using MemoryDC worked when I saved just the image to a file, but failed to place in the staticBitmap.
I have spent about a week working on this problem, reading very many different tutorials and forums to try to find this same problem. I feel like no one else is having this problem.
The most working one, ClientDC, must be redrawn whenever the window is resized, causing flickering. Here is what I have for ClientDC:
self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY,
wx.EmptyBitmap(517,524))
def OnGoSelect(self,e):
print "GO"
img = wx.Image("./poster/"+self.picChoice,wx.BITMAP_TYPE_PNG)
self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
def DrawLine(self):
dc = wx.ClientDC(self.imageCtrl)
dc.SetPen(wx.Pen(wx.BLUE, 2))
dc.DrawLines(((223, 376), (223, 39), (240, 39), (240,376), (223,376)))
The current PaintDC doesn’t go into an infinite loop, but instead the image is placed in the staticBitmap and somehow the drawing is behind the image. So when I size, ComboBoxes erase part of the image and sizing the window to cover the image, erases that part. As I size the window back, the drawing is still there but the image is erased. Here’s what I have:
self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY,
wx.EmptyBitmap(517,524))
def OnGoSelect(self,e):
print "GO"
img = wx.Image("./poster/"+self.picChoice,wx.BITMAP_TYPE_PNG)
self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
self.imageCtrl.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, e):
print "OnPaint Triggered"
dc = wx.PaintDC(self.imageCtrl)
dc.Clear()
dc.SetPen(wx.RED_PEN)
dc.DrawLines(((100, 200), (100, 100), (200, 100), (200,200), (100,200)))
For MemoryDC, I loaded an EmptyBitmap all by itself, drew on it, and then tried to put it into the staticBitmap. It gave me a blank gray screen. If I didn’t draw on the EmptyBitmap, it came out normal, black. Even when I drew on it, I saved it to a file which came out the way it should but still gave me the gray screen inside the application. Here’s the MemoryDC code:
self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY,
wx.EmptyBitmap(517,524))
def Draw(self, e):
print "Draw"
img = wx.Image("./poster/Test2.png", wx.BITMAP_TYPE_ANY)
bit = wx.EmptyBitmap(517,524)
dc = wx.MemoryDC(bit)
dc.SetBackground(wx.Brush(wx.BLACK))
dc.Clear()
dc.SetPen(wx.Pen(wx.RED, 1))
dc.DrawLines(((83, 375), (83, 42), (120, 42), (120,375), (83,375)))
self.imageCtrl.SetBitmap(bit)
bit.SaveFile("bit.bmp", wx.BITMAP_TYPE_BMP)
I’m at my wits end. Any advice is welcome!
Upvotes: 5
Views: 3778
Reputation: 111
I found it!
I didn't know before that when using MemoryDC, I have to deselect the bitmap to which I'm drawing. That's done by passing a wx.NullBitmap to the SelectObject method.
Here's the code for MemoryDC:
def Draw(self, e):
print "Draw"
img = wx.Image("./poster/Test2.png", wx.BITMAP_TYPE_ANY)
bit = wx.EmptyBitmap(517,524)
imgBit = wx.BitmapFromImage(img)
dc = wx.MemoryDC(imgBit)
dc.SetPen(wx.Pen(wx.RED, 1))
dc.DrawLines(((83, 375), (83, 42), (120, 42), (120,375), (83,375)))
dc.SelectObject(wx.NullBitmap)# I didn't know I had to deselect the DC
self.imageCtrl.SetBitmap(imgBit)
imgBit.SaveFile("bit.bmp", wx.BITMAP_TYPE_BMP)
Upvotes: 6