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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vilas GitePosted Jul 25, 2011, 11:12 AM
Is it possible to prevent flickering, if we put these image files in Debug\Bin folder?
Erick AlvaradoPosted Apr 3, 2016, 3:15 PM
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
Zoran HorvatPosted Jul 26, 2011, 6:06 PM
Did setting DoubleBuffered to true resolve the flickering problem?
Zoran
Zoran HorvatPosted Jul 25, 2011, 11:34 AM
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 HorvatPosted Jul 25, 2011, 6:11 AM
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
jayakrishnan MorayurPosted Jul 25, 2011, 6:04 AM
Zoran HorvatPosted Jul 25, 2011, 5:40 AM
Zoran