项目需要使用的图片
1. 新建WindowsForm项目,并将要使用的图片放入到项目/bin/debug文件夹下。
2. 双击PictureBox在界面中添加一个图片控件。
3. 选择PictureBox的image属性,点击后边的三个点按钮。
4. 在弹出框中选择本地资源–>导入
5. 浏览文件夹,选择根目录/bin/debug下的normal图片,点击打开。
6. 这时可以看到图片被导入进来了,点击确定。
7. 点击PictureBox确保图片处于选中状态,然后选择SizeMode的加载方式,让图片适应控件。
8. 双击Button控件向界面中添加按钮,用于控制图片的切换。
9. 双击button1,向代码中添加按钮的点击事件。
10. 添加代码
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 = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "light.png");//light.png图片
Image normalImage = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "normal.png");//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;
}
}
}
}
11. 运行软件,就可以正常运行程序了。