Introduction
Change mdi form background picture at run time
This article demonstrate how to change MDI Container background image at run time
Step 1: Create Windows Application Project
Please do the following to create a Windows Application project:
- Select File menu -> New -> Project.
- Choose C# from Project Types pane.
- In the Templates pane, choose Windows Application for Visual C# projects.
Step 2:
Add Controls
1. Menustrip Control
2. OpenFileDialog Control
Change IsMdiContainer Property = True
Step 3: Coding Sections
- Open Image file
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
string Backpath = null;
try
{
openFileDialog1.Filter = "Jpg|*.jpg";
openFileDialog1.ShowDialog();
Backpath = openFileDialog1.FileName;
// calling Image background method
Change_BackgroundImage(Backpath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
- Change_BackgroundImage(Backpath) Method
private void Change_BackgroundImage(string _path )
{
string imagepath = _path;
System.IO.FileStream fs;
// MDI Form image background layout change here
//(Remember control imagebakground layout take default form background layount )
this.BackgroundImageLayout = ImageLayout.Stretch;
// Checking File exists if yes go --->
if (System.IO.File.Exists(imagepath))
{
// Read Image file
fs = System.IO.File.OpenRead(imagepath);
fs.Position = 0;
// Change MDI From back ground picture
foreach (Control ctl in this.Controls)
{
if (ctl is MdiClient)
{
//ctl.BackColor = Color.AntiqueWhite;
ctl.BackgroundImage = System.Drawing.Image.FromStream(fs);
break;
}
}
}
}
Conclusion
I am looking forward to receive any comments/suggestion you have for me.
Thank you for reading; I sincerely hope this article will help you a bit or two to know reporting services better through my applied approach