Clearing graphics...

  • Thread starter Thread starter Thomas
  • Start date Start date
T

Thomas

I'm trying create a small app using custom graphics, and avoiding
labels where possible and using the drawstring method instead.

I created a handler for the Paint event on my form. In there, I create
a new graphic. One is with a background image, something like:

myBackground = Me.CreateGraphics
myBackground.DrawImage(backgroundImage, Me.ClientRectangle, New
Rectangle(0, 0, Me.backgroundImage.Width, Me.backgroundImage.Height),
GraphicsUnit.Pixel)

OK, that works. I create a new graphic and write the time, such as

clockGraphic.DrawString(Now.ToLongTimeString, clockFont,
ellipse_brush_green, 20, 100)

This works, too. What is the best way to clear this clockGraphic (or
actually refresh the clock without "painting" over the previous text),
leaving the background image? Does the entire screen need to be
repainted?

I have an area that will contain a clock that will be updated
regulalry. Formerly, it was using a label, and I'm trying to
accomplish the same thing using graphics. Obviously, with a label,
it's quite easy to simply reassign the .text property to the label.

Thanks!
Tom
 
Keep the static background as a separate bitmap, then blit it into the
graphics followed by the drawstring.
 
Thanks Chris,

Do you have more information/examples on Blitting?

The issue that I'm having now is flicker ... basically, this is a very
simple app -- a BMP background and clock graphics (just small circles) that
move around (hr/min/sec). I thought doing an off-screen build of a bitmap
and then swapping it out would be flicker-free, but alas, it isn't.

I'm new to CE developement and graphics in general, so I don't know too much
about this.

Thanks,
Tom
 
Actually, one quick followup:

I overloaded the background refresh, which definetly helped but still not
100%....

Tom
 
Couldn't you just redraw the background image where the clock is? That is
what I am doing to create a "transparent" button.
 
Hi Chris,

It seems to be working better now, per my previous post ... I cleaned up
some of my code and it seems to work better, may have been a soft reset, I
don't know. But I do think what I should do is not redraw the entire
bitmap. There's an MSDN article on this, but I don't think I need to go
this far since this redraw is only once a second; if this was a game, for
example, a smarter refresher may be in order. By far the biggest
improvement was via the override on the background redraw.

My onpaint looks like:
-
Dim gxOn As Graphics = Me.CreateGraphics
gxOn.DrawImage(screenBitmap, 0, 0, Me.ClientRectangle, GraphicsUnit.Pixel)
gxOn.Dispose()
screenBitmap.Dispose()
-
The bitmap is created in another routine via timer, which draws a few
circles and what not... the arrays are built once on start up and are
referenced here, and the clock text sub handles the text parts of the
clock -- this simply does the graphics:
-
Dim lsecond As Integer = DateTime.Now.Second
Dim lminute As Integer = DateTime.Now.Minute
Dim lhour As Integer = DateTime.Now.Hour
If lhour > 12 Then lhour = lhour - 12
screenBitmap = New Bitmap(240, 320)
Dim gxOff As Graphics = Graphics.FromImage(screenBitmap)
gxOff.DrawImage(backgroundImage, Me.ClientRectangle, New Rectangle(0, 0,
240, 320), GraphicsUnit.Pixel)
'draw 3 circles
For a As Integer = 0 To 59
gxOff.DrawEllipse(ellipse_pen, objSetting(1, a), objSetting(2, a), cDi, cDi)
gxOff.DrawEllipse(ellipse_pen, objSettingMin(1, a), objSettingMin(2, a),
cDi, cDi)
Next
For a As Integer = 0 To 11
gxOff.DrawEllipse(ellipse_pen, objSettingHr(1, a), objSettingHr(2, a), cDi,
cDi)
Next
'draw "current" circles
gxOff.FillEllipse(ellipse_brush_green, objSetting(1, lsecond), objSetting(2,
lsecond), cDi, cDi)
gxOff.FillEllipse(ellipse_brush_green, objSettingMin(1, lminute),
objSettingMin(2, lminute), cDi, cDi)
gxOff.FillEllipse(ellipse_brush_green, objSettingHr(1, lhour),
objSettingHr(2, lhour), cDi, cDi)
buildClockText(gxOff)
gxOff.Dispose()
-
 
Try maving your screenBitmap and gxOff creation somewhere else, like the
constructor. There's no point in incurring the overhead of creating the
bitmap and graphics object every time you want to draw. I would only call
creating during a resize. That might help.
 
I'll try it out -- many thanks for your help!


Chris Tacke said:
Try maving your screenBitmap and gxOff creation somewhere else, like the
constructor. There's no point in incurring the overhead of creating the
bitmap and graphics object every time you want to draw. I would only call
creating during a resize. That might help.
 
Back
Top