我刚开始学习c#两个月前,我得到了当前项目(windows窗体)的代码来设置背景图片:
public FormMain()
{
this.BackgroundImage = Properties.Resources.image;
InitializeComponent();
var timer = new Timer();
////change the background image every second
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
//add image in list from resource file.
List<Bitmap> lisimage = new List<Bitmap>();
lisimage.Add(Properties.Resources.image);
var indexbackimage = DateTime.Now.Second % lisimage.Count;
this.BackgroundImage = lisimage[indexbackimage];
}
我的问题是:你是否必须每秒更换背景图像,或者如果我只是写(我只有一个背景图像)就足够了:
public FormMain()
{
this.BackgroundImage = Properties.Resources.image;
InitializeComponent();
}
因为它似乎有效。
答案 0 :(得分:2)
如果要迭代一系列图像以创建动画,则只需要这样的计时器。
你所拥有的就足以设置一次图像。
答案 1 :(得分:1)
正如AaronLS所写,设置背景一次就足够了。我更进一步解释为什么你所拥有的额外代码没有多大意义(假设这是整个代码)。
void timer_Tick(object sender, EventArgs e)
{
//add image in list from resource file.
List<Bitmap> lisimage = new List<Bitmap>(); //this line creates a new list
lisimage.Add(Properties.Resources.image); //fill the NEWLY created list with the one image from the resources
//note, that resources are usually static, so it's always the same resource
var indexbackimage = DateTime.Now.Second % lisimage.Count; //choose an index from the list, but the list only contains that one image, so the index will always be 0
this.BackgroundImage = lisimage[indexbackimage]; //pick the same image that was set initially
}
正如您所看到的,代码相当荒谬 - 它没有做任何事情。它看起来有人想创建一种每秒切换图像的机制,但即使这样也很难编码。