C#怎么用窗口显示图片

By | 2025 年 9 月 9 日

方法:通过资源文件(.resx)添加
右键项目 → 添加 → 新建项‌,选择资源文件(.resx)(如Resources.resx)。
在资源编辑器中点击添加资源 → 添加现有文件,选择图片文件。

1、在类中添加图片资源的引用

public partial class Form1 : Form
    {
        #region 添加对图片资源的引用(2025.09.09)
        private Image image1;
        private Image image2;
        private Image image3;
        #endregion

2、在窗体中初始化这些图片资源

public Form1()
        {
            InitializeComponent();
            #region 初始化这些图片资源(2025.09.09)
            image1 = Properties.Resources.image1; // 假设你的图片名为image1.jpg
            image2 = Properties.Resources.image2; // 假设你的图片名为image2.jpg
            image3 = Properties.Resources.image3; // 假设你的图片名为image3.jpg
            #endregion
        }

3、创建点击按钮,代码中通过Properties.Resources.图片名调用:

private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.image2;
        }