项目需要使用的图片
1. 新建WindowsForm项目,双击PictureBox在界面中添加一个图片控件。
2. 选择PictureBox的image属性,点击后边的三个点按钮。
3. 在弹出框中选择项目资源文件–>导入。
4. 选择准备好的两个图片,点击打开。就可以看到图片被加载进来了。
5. 点击确定按钮,将图片加载到项目里。
6. 点击PictureBox确保图片处于选中状态,然后选择SizeMode的加载方式,让图片适应控件。
7. 双击Button控件向界面中添加按钮,用于控制图片的切换。
8. 双击button1,向代码中添加按钮的点击事件。
9. 添加代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LoadPictureApplication
{
public partial class Form1 : Form
{
Image lightImage = Properties.Resources.light;//light.png图片
Image normalImage = Properties.Resources.normal;//light.png图片
bool boo = true;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
String pbName = "pictureBox" + (i + 1);
if (boo)
{
PictureBox pb = ((PictureBox)this.Controls.Find(pbName, true)[0]);
pb.Image = lightImage;
boo = false;
}
else {
PictureBox pb = ((PictureBox)this.Controls.Find(pbName, true)[0]);
pb.Image = normalImage;
boo = true;
}
}
}
}
10. 运行程序,点击按钮,就可以切换图片。