Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:59:14+00:00 2026-06-09T08:59:14+00:00

Hey I am trying to draw a rectangle on top of an image when

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:59:16+00:00Added an answer on June 9, 2026 at 8:59 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone, my first time trying to draw a multi-color gradient in actionscript 3.
Hey i'm trying to display a date/time from a timestamp field using: echo $date
Hey i'm trying to build an app in which the user can input some
Hey guys I'm trying to replace the image loaded on the html5 canvas on
Hey trying to read stream into image control can any one help also I
Hey i'm trying to make a very simple GUI using SFML, and i want
Hey I have a situation I am picking a image from camera and trying
Hey just wondering how I can draw multiple rectangle objects in C# but have
Hey guys i'm trying to use drawString() function to draw the result from a
Hey guys i'm trying to figure out how to draw a bitmap that i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.