I have a custom control that draws a gradient and an image. I have overridden the OnPaint event as outlined below and draw the control to an off-screen bitmap before drawing to the screen, which should eliminate flicker. It works pretty well, but there is still a small amount of flicker only on the right edge of the control when I resize the form that it sits on. In the OnResize event of the main form, I simply set the width of the control to the width of the form. Calling the SetStyle function has no effect. It's pretty minor, but I was hoping to completely eliminate it. Any ideas? I can provide the executable if anyone needs to see exactly what is happening.
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rect = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height);
Color lightBlue = Color.FromArgb(40, 146, 235);
Color darkBlue = Color.FromArgb(0, 97, 167);
using (Bitmap bitmapBuffer = new Bitmap(ClientSize.Width, ClientSize.Height))
using (LinearGradientBrush brush = new LinearGradientBrush(rect, lightBlue, darkBlue, LinearGradientMode.Vertical))
using (Graphics g = Graphics.FromImage(bitmapBuffer))
{
g.FillRectangle(brush, rect);
if (BackgroundImage != null)
{
g.DrawImage(BackgroundImage, 0, 0);
}
e.Graphics.DrawImage(bitmapBuffer, 0, 0);
}
}
Question
boogerjones
I have a custom control that draws a gradient and an image. I have overridden the OnPaint event as outlined below and draw the control to an off-screen bitmap before drawing to the screen, which should eliminate flicker. It works pretty well, but there is still a small amount of flicker only on the right edge of the control when I resize the form that it sits on. In the OnResize event of the main form, I simply set the width of the control to the width of the form. Calling the SetStyle function has no effect. It's pretty minor, but I was hoping to completely eliminate it. Any ideas? I can provide the executable if anyone needs to see exactly what is happening.
protected override void OnPaint(PaintEventArgs e) { Rectangle rect = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height); Color lightBlue = Color.FromArgb(40, 146, 235); Color darkBlue = Color.FromArgb(0, 97, 167); using (Bitmap bitmapBuffer = new Bitmap(ClientSize.Width, ClientSize.Height)) using (LinearGradientBrush brush = new LinearGradientBrush(rect, lightBlue, darkBlue, LinearGradientMode.Vertical)) using (Graphics g = Graphics.FromImage(bitmapBuffer)) { g.FillRectangle(brush, rect); if (BackgroundImage != null) { g.DrawImage(BackgroundImage, 0, 0); } e.Graphics.DrawImage(bitmapBuffer, 0, 0); } }Link to comment
Share on other sites
6 answers to this question
Recommended Posts