Good way to draw rectangle on mouse move on StaticBitmap

Trying to draw rectangle with mouse on top of StaticBitmap.
main flow:
mouse left click - set start coordinates
mouse move - draw new rectangle on top of static bitmap, current code:
dc = MemoryDC()
bitmap = self.bitmap
bitmap = bitmap.GetSubBitmap(Rect(0,0, bitmap.GetWidth(), bitmap.GetHeight()))
bdc = BufferedDC(dc)
bdc.SelectObject(bitmap)
x,y = event.GetX(), event.GetY()
bdc.SetBrush(TRANSPARENT_BRUSH)
bdc.DrawRectangle(self.coords[0], self.coords[1], x - self.coords[0], y - self.coords[1])
bdc.SelectObject(NullBitmap)
self.staticbitmap.SetBitmap(bitmap)

But drawing is a bit laggy is there a better\faster way to do that?

It’s not surprising that it’s laggy – you’re doing a hell of a lot
of work there in response to each mouse move message, and those can
happen hundreds of times per second. A better solution would be to
have your mouse move handler just remember that the coordinates
changed, and do the drawing somewhere else. You could use a timer
tick, or do Refresh and handle it in a paint handler.
Do you really need the rectangle stored in a static bitmap? Can’t
you just refresh the bitmap and draw the rectangle directly on the
screen?

···

ambi wrote:

  Trying to draw rectangle with mouse on top of

StaticBitmap.

  main flow:

  mouse left click - set start coordinates

  mouse move - draw new rectangle on top of static bitmap, current

code:

  dc = MemoryDC()

    bitmap = self.bitmap

    bitmap = bitmap.GetSubBitmap(Rect(0,0, bitmap.GetWidth(),

bitmap.GetHeight()))

    bdc = BufferedDC(dc)

    bdc.SelectObject(bitmap)

    x,y = event.GetX(), event.GetY()

    bdc.SetBrush(TRANSPARENT_BRUSH)

    bdc.DrawRectangle(self.coords[0], self.coords[1], x -

self.coords[0], y - self.coords[1])

    bdc.SelectObject(NullBitmap)

    self.staticbitmap.SetBitmap(bitmap)



  But drawing is a bit laggy is there a better\faster way to do

that?

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

first:

Trying to draw rectangle with mouse on top of StaticBitmap.
main flow:
mouse left click - set start coordinates
mouse move - draw new rectangle on top of static bitmap, current code:
dc = MemoryDC()
bitmap = self.bitmap
bitmap = bitmap.GetSubBitmap(Rect(0,0, bitmap.GetWidth(),
bitmap.GetHeight()))

you're getting a sub-bit map that is the whole thing?

I'd probably do:

bitmap = wx.EmptyBitmap( self.bitmap.GetWidth(), self.bitmap.GetHeight()
bdc.SelectObject( bitmap)
bdc.DrawBitmap(self.bitmap)

...

It's not surprising that it's laggy -- you're doing a hell of a lot of work
there in response to each mouse move message, and those can happen hundreds
of times per second. A better solution would be to have your mouse move
handler just remember that the coordinates changed, and do the drawing
somewhere else.

you may want to use a wx.Overlay for this sort of thing (see enclosed
for a sample)

But I'm still surprised that this is slow -- is this a really big bitmap?

A hint -- StaticBitmap is generally used for what it sounds like,
bitmaps that don't change, and ar generally small, icons and the like.
If you are working with somethign that manipulates bitmaps, etc, I
suspect you would be better off with a custom wx.Window that renders
the bitmap, and does whatever else.

What is your use-case?

-Chris

Overlaytest2.py (2.99 KB)

···

Do you really need the rectangle stored in a static bitmap? Can't you just
refresh the bitmap and draw the rectangle directly on the screen?

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Main problem is that i have StaticBitmap inside ScrolledWindow so that rectangle should also be drawn outside of visible area.
So think like user draws rectangle on StaticBitmap and user can scroll ScrollWindow widget to see rest bitmap and if it comes back - rectangle should be visible.

But thaks for help i will try both ways.

···

On Tuesday, February 5, 2013 8:12:54 PM UTC+2, Tim Roberts wrote:

ambi wrote:

  Trying to draw rectangle with mouse on top of

StaticBitmap.

  main flow:

  mouse left click - set start coordinates

  mouse move - draw new rectangle on top of static bitmap, current

code:

  dc = MemoryDC()

    bitmap = self.bitmap

    bitmap = bitmap.GetSubBitmap(Rect(0,0, bitmap.GetWidth(),

bitmap.GetHeight()))

    bdc = BufferedDC(dc)

    bdc.SelectObject(bitmap)

    x,y = event.GetX(), event.GetY()

    bdc.SetBrush(TRANSPARENT_BRUSH)

    bdc.DrawRectangle(self.coords[        0], self.coords[1], x -

self.coords[0], y - self.coords[1])

    bdc.SelectObject(NullBitmap)

    self.staticbitmap.SetBitmap(bitmap)



  But drawing is a bit laggy is there a better\faster way to do

that?

It's not surprising that it's laggy -- you're doing a hell of a lot

of work there in response to each mouse move message, and those can
happen hundreds of times per second. A better solution would be to
have your mouse move handler just remember that the coordinates
changed, and do the drawing somewhere else. You could use a timer
tick, or do Refresh and handle it in a paint handler.

Do you really need the rectangle stored in a static bitmap?  Can't

you just refresh the bitmap and draw the rectangle directly on the
screen?

-- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Main problem is that i have StaticBitmap inside ScrolledWindow so that
rectangle should also be drawn outside of visible area.
So think like user draws rectangle on StaticBitmap and user can scroll
ScrollWindow widget to see rest bitmap and if it comes back - rectangle
should be visible.

I think you'd be better off dealing with the issue separately :

1) how to draw a rect as the mouse is moving -- wx.Overlay is good for this

2) how to draw the final image with the rectangle on it -- I'd keep
the coordinates of teh rectangle around, so it can be re-drawn when
required.

Again, I'd probably drop StaticBitmap and use a wx.Window with some
custom rendering.

-Chris

···

On Tue, Feb 5, 2013 at 10:48 PM, ambi <kirill.strelkov@gmail.com> wrote:

But thaks for help i will try both ways.

On Tuesday, February 5, 2013 8:12:54 PM UTC+2, Tim Roberts wrote:

ambi wrote:

Trying to draw rectangle with mouse on top of StaticBitmap.
main flow:
mouse left click - set start coordinates
mouse move - draw new rectangle on top of static bitmap, current code:
dc = MemoryDC()
bitmap = self.bitmap
bitmap = bitmap.GetSubBitmap(Rect(0,0, bitmap.GetWidth(),
bitmap.GetHeight()))
bdc = BufferedDC(dc)
bdc.SelectObject(bitmap)
x,y = event.GetX(), event.GetY()
bdc.SetBrush(TRANSPARENT_BRUSH)
bdc.DrawRectangle(self.coords[0], self.coords[1], x - self.coords[0], y -
self.coords[1])
bdc.SelectObject(NullBitmap)
self.staticbitmap.SetBitmap(bitmap)

But drawing is a bit laggy is there a better\faster way to do that?

It's not surprising that it's laggy -- you're doing a hell of a lot of
work there in response to each mouse move message, and those can happen
hundreds of times per second. A better solution would be to have your mouse
move handler just remember that the coordinates changed, and do the drawing
somewhere else. You could use a timer tick, or do Refresh and handle it in
a paint handler.

Do you really need the rectangle stored in a static bitmap? Can't you
just refresh the bitmap and draw the rectangle directly on the screen?

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov