Trouble eliminating flicker



I'm trying to write a simple Windows Forms based clock. Right now if
flickers pretty badly when updated both from the timer and resizing. I
thought the way I buffered the image before blitting would eliminate it
but it does not. What am I doing wrong?

import System
import System.Drawing
import System.Windows.Forms

class Clock(Form):
private timer as Timer
private buffer as Bitmap
private cx as int
private cy as int
private rad as int

def constructor():
timer = Timer()
timer.Tick += TimerEvent
timer.Interval = 200

def TimerEvent():

def InitializeControls():
self.Text = "Clock"

def OnResize(e as EventArgs):
self.buffer = Bitmap(ClientRectangle.Width, ClientRectangle.Height)
cx = ClientRectangle.Width / 2
cy = ClientRectangle.Height / 2
if ClientRectangle.Width < ClientRectangle.Height:
rad = ClientRectangle.Width * .45
rad = ClientRectangle.Height * .45

def PolarToRect(a as double, m as double):

def DrawBuffer():
g = Graphics.FromImage(buffer)
rect = Rectangle(cx-rad, cy-rad, rad*2, rad*2)
g.DrawEllipse(Pen(Color.Black, 3), rect)

def OnPaint(pe as PaintEventArgs):
pe.Graphics.DrawImageUnscaled(buffer, 0, 0)


I also get this exception when I minimize the form but I can't figure
out what is causing it.

System.ArgumentException: Invalid parameter used.
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height,
PixelFormat format)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at clock.Clock.OnResize(EventArgs e) in
D:\Projects\sharpdevelop\clock\ 26
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32
width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WmMove(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)



Bob Powell [MVP]

These are assumptions because I don't know enough about Boo to make a real
diagnosis but I would be disinclined to call the onsize from the constructor
because the window hasn't been created yet.

Use SetStyle to set up double buffering...

Don't paint the background...

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting

Find great Windows Forms articles in Windows Forms Tips and Tricks

Answer those GDI+ questions with the GDI+ FAQ

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question