C#WinForm开发:如何将图片添加到项目资源文件(Resources)中

C#WinForm开发:如何将图片添加到项目资源文件(Resources)中

  1. 引言

  2. 操作步骤

  3. 实例应用

  4. 功能延展

  5. 引言
    在C#Winform开发中,有时需要在控件中插入一些图片,常见的有pictureBox控件的Image属性。那么,我们应当如何将现有图片添加到项目资源文件中?

  6. 操作步骤
    在VS 2017中新建一个“Windows窗体应用”项目,然后保存两张图片到桌面。这里,项目名称为“cyTest”,图片名称为“JapaneseBoy”和“WhiteBoard”,至此准备工作完成。
    在这里插入图片描述
    在“解决方案资源管理器”视图中,双击“Properities -> Resources.resx”选项:
    在这里插入图片描述
    在弹出的“Resources.resx”窗口中,单击"添加资源"选项后的“倒三角”图标。然后,在出现的下拉菜单中,单击"添加现有文件"选项:
    在这里插入图片描述
    找到图片保存路径,选中两张图片,单击“打开”按钮即可:
    “解决方案资源管理器”视图中“cyTest”项目下,自动生成“Resources”文件夹及已添加的两张图片。至此,已成功将图片添加至项目资源文件中。
    在这里插入图片描述
    实例应用
    以在Form中插入两个图片为例:

在Form中拖放两个pictureBox控件:
在这里插入图片描述
编写如下程序代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cyTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Image cyImage1, cyImage2;
private void Form1_Load(object sender, EventArgs e)
{
cyImage1 = Properties.Resources.JapaneseBoy;
pictureBox1.Image = cyImage1;
pictureBox1.Height = cyImage1.Height;
pictureBox1.Width = cyImage1.Width;
pictureBox1.Location = new Point(10, 10);

        cyImage2 = Properties.Resources.WhiteBoard;
        pictureBox2.Image = cyImage2;
        pictureBox2.Height = cyImage2.Height;
        pictureBox2.Width = cyImage2.Width;
        pictureBox2.Location = new Point(700, 10);
    }
}

}

C# WinForm 添加 Microsoft Forms 2.0 Frame 的步骤如下: 1. 打开 Visual Studio,创建一个新的 Windows 窗体应用程序。 2. 在工具箱找到“COM”选项卡,并在其找到“Microsoft Forms 2.0 Frame”控件。 3. 将“Microsoft Forms 2.0 Frame”控件从工具箱拖动到窗体设计器。 4. 在窗体设计器“Microsoft Forms 2.0 Frame”控件,然后打开属性窗口。 5. 在属性窗口,设置“BorderStyle”属性为“None”,这样就可以隐藏控件的边框。 6. 在代码,你可以使用以下代码来操作 Microsoft Forms 2.0 Frame 控件: ``` // 创建 Microsoft Forms 2.0 Frame 控件对象 AxHost.State state = new AxHost.State(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.axMsFormsFrame1 = new AxMSForms.AxFrame(); ((System.ComponentModel.ISupportInitialize)(this.axMsFormsFrame1)).BeginInit(); this.SuspendLayout(); // 设置 Microsoft Forms 2.0 Frame 控件的属性 this.axMsFormsFrame1.Enabled = true; this.axMsFormsFrame1.Location = new System.Drawing.Point(12, 12); this.axMsFormsFrame1.Name = "axMsFormsFrame1"; this.axMsFormsFrame1.OcxState = state; this.axMsFormsFrame1.Size = new System.Drawing.Size(260, 210); this.axMsFormsFrame1.TabIndex = 0; // 将 Microsoft Forms 2.0 Frame 控件添加到窗体 this.Controls.Add(this.axMsFormsFrame1); ((System.ComponentModel.ISupportInitialize)(this.axMsFormsFrame1)).EndInit(); this.ResumeLayout(false); ``` 以上代码,axMsFormsFrame1 是你在窗体设计器添加的 Microsoft Forms 2.0 Frame 控件的名称,你可以根据自己的实际情况进行修改。 请注意,Microsoft Forms 2.0 Frame 控件需要在计算机上安装 Microsoft Office 才能使用。如果你的计算机上没有安装 Microsoft Office,你需要先安装 Microsoft Office 后才能使用 Microsoft Forms 2.0 Frame 控件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光怪陆离的节日

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值