Durga Durga - 12 days ago 10
C# Question

Setting MdiParent Background Image properly

I am using below code to set background image of MdiParent form ,and it is working well ,but when I click on maximize button than BackgroundImage is repeating at rightside and bottom edge(i.e right side and bottom side Image portion is repeating) ,how do I avoid this and display Image properly ?

public Parent()
{
InitializeComponent();

foreach (Control ctl in this.Controls)
{
if (ctl is MdiClient)
{
ctl.BackgroundImage = Properties.Resources.bg;
ctl.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
break;
}
}
}

Answer
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;

this points to Form.

Myself too noticed the same behavior you mentioned. It seems just a painting issue. Add the following code to fix it.

protected override void OnSizeChanged(EventArgs e)
{
    base.OnSizeChanged(e);
    this.Refresh();
}