MDI Background image problem
I have an application using c sharp and i added a background image for mdi form but when ever a form is loaded the mdi image is blinking why
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vilas GiteRank #951 · 2.1K reputationPosted Jul 25, 2011, 7:12 AM EDT
Is it possible to prevent flickering, if we put these image files in Debug\Bin folder?
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 1:40 AM EDT
Zoran
jayakrishnan Morayur156 reputationPosted Jul 25, 2011, 2:04 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 2:11 AM EDT
First try setting form's DoubleBuffered property to true. It is a protected property which is by default set to false. So you have to set it to true in form's constructor.
Zoran
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 7:34 AM EDT
using System.Drawing;
using System.Windows.Forms;
namespace ImageTest
{
class Program
{
private class MyForm: Form
{
public MyForm()
{
_backgroundImage = new Bitmap(500, 500);
using (Graphics gr = Graphics.FromImage(_backgroundImage))
{
gr.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(0, 0, 500, 500));
gr.FillEllipse(new SolidBrush(Color.DarkRed), new Rectangle(0, 0, 500, 500));
}
this.BackgroundImage = _backgroundImage;
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
_backgroundImage.Dispose();
}
private Image _backgroundImage;
}
static void Main(string[] args)
{
MyForm f = new MyForm();
f.ShowDialog();
}
}
}
Wait for it to open the form and then maximize the form. You'll see flickering. It comes from the form acting upon Window message to draw background - instead of drawing the image immediately, it first draws background in solid brush. This is probably done because .NET doesn't know whether the background image contains some transparent parts, etc. so Form tries to be sure that its background will be solidly filled even after the image is applied. Anyway, that kind of worry leads to flickering.
Now, modify MyForm and add this method:
protected override void OnPaintBackground(PaintEventArgs e)
{
//base.OnPaintBackground(e);
for (int x = 0; x < ClientSize.Width; x += _backgroundImage.Width)
for (int y = 0; y < ClientSize.Height; y += _backgroundImage.Height)
e.Graphics.DrawImage(_backgroundImage, new Point(x, y));
}
And repeat the test - open the form, then maximize it, resize it manually, etc. You'll see that flickering has gone. Off course, if you uncomment the base.OnPaintBackground call, flickering will be back again.
This example shows how to stop flickering in some cases, but in every particular case it is up to the programmer to know what should be done by the form.
Same effect in this case can be obtained by adding this to the constructor:
this.DoubleBuffered = true;
However, you don't have DoubleBuffered property available at all times. If you're coding custom control then your control will be instantiated by a form which might have DoubleBuffered set to false and you can't do anything about it.
Zoran
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 26, 2011, 2:06 PM EDT
Did setting DoubleBuffered to true resolve the flickering problem?
Zoran
Erick Alvarado2 reputationPosted Apr 3, 2016, 11:15 AM EDT
Hola, yo tenía el mismo problema. Mi solución es que en el evento "ResizeBegin" de tu formulario establezcas la propiedad "BackgroudImage" en Nothing, luego, en el evento "ResizeEnd" vuelve a establecer la propiedad con la imagen que deseas de fondo y agrega esta línea: ResizeRedraw = True