Windows Forms Background Image

This article explains how to change a Windows Forms background image.

This article explains how to change a Windows Forms background image every second and how to change the background color every second.

Step 1

Go to the VS 2012 Menu and select "File" -> "New" -> "Project..." then select Visual C# -> Window Forms Application and create the Windows Forms application.

windows form application

Step 2

Then we will select the resources.resx file from the Solution Explorer.

resources

Then go to the top of the resources file where we can see multiple options. Here I will select the images option because we want to add an existing file in the resources.resx file.

If you want to store global variables, global image files, global text files and so on in the Windows application then you should go to the resource file for the setting of all these configurations.

add existing file

I have two images in the resource file and then select all the images and press F4 for setting the properties of the images. Then select persistence properties to embed them in the .resx.

select persistence properties

Step 3

Then go to the Form1.cs file and write this code to change the background image every second.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace DemoBackgroundImageChange  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             this.BackgroundImage = Properties.Resources.im;  
  17.             InitializeComponent();  
  18.             var timer = new Timer();  
  19.             //change the background image every second  
  20.             timer.Interval = 1000;  
  21.             timer.Tick += new EventHandler(timer_Tick);  
  22.             timer.Start();  
  23.         }  
  24.         void timer_Tick(object sender, EventArgs e)  
  25.         {  
  26.             //add image in list from resource file.  
  27.             List<Bitmap> lisimage = new List<Bitmap>();  
  28.             lisimage.Add(Properties.Resources.im);  
  29.             lisimage.Add(Properties.Resources.rite);  
  30.             var indexbackimage = DateTime.Now.Second % lisimage.Count;  
  31.             this.BackgroundImage = lisimage[indexbackimage];  
  32.         }  
  33.     }  
  34. }  

Output:

image

Step 4

Code to change the form background color every second. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace DemoBackgroundImageChange  
  11. {  
  12.     public partial class Form2 : Form  
  13.     {  
  14.     public Form2()  
  15.         {  
  16.             this.BackColor = Color.Green;  
  17.             InitializeComponent();  
  18.             var timer = new Timer();  
  19.             timer.Interval = 1000;  
  20.             timer.Tick += new EventHandler(timer_Tick);  
  21.             timer.Start();  
  22.   
  23.         }  
  24.         void timer_Tick(object sender, EventArgs e)  
  25.         {  
  26.   
  27.             //set color in background  
  28.             var colors = new[] { Color.CornflowerBlue, Color.Green, Color.Aqua, Color.Azure, Color.CadetBlue };  
  29.             var index = DateTime.Now.Second % colors.Length;  
  30.             this.BackColor = colors[index];  
  31.         }}  
Output: 

output