C#窗体Winform,如何嵌入图片添加图片,使用图片资源?

1.首先,打开工具箱,找到PictureBox控件

2.打开PictureBox的属性面板,设置Image属性

3.准备嵌入图片资源

图片资源有两个地方,一个是全局的在Properties下的Resources.resx文件。

另一个是在专属于某窗体使用的图片资源,位置在窗体下的resx文件中,如上图的第2个红圈就是。

4.设置Image属性,打开弹窗

本地资源,对应的是专属于某窗体使用的资源。

项目资源文件,则是大家都可以使用的资源。

5.Resources文件夹

不论是本地资源,还是项目资源,添加后,都会传入到Resources文件夹下,所以,注意命名不要相同。

6.代码编写

如果是以项目资源的方式加载,它对应的代码编写是

global::WindowsFormsApp1.Properties.Resources.资源名

如果是以本地资源的方式加载,他对应的代码编写是

ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
this.pictureBox1.Image = ((Image)(resources.GetObject("Image")));

由ComponentResourceManager进行管理。

7.还有另外一种简便的加载资源的方式

将任意一张图片放入项目中,如下图,以PipeRack-32.png为例子

修改它的属性为嵌入的资源:

最后使用下面的代码读取即可,其中“RevitDevelopment.res.PipeRack-32.png”就是文件在你项目中的位置,格式为:“命名空间.文件夹名称.文件名”:

Assembly a = Assembly.GetExecutingAssembly();
Stream s = a.GetManifestResourceStream("RevitDevelopment.res.PipeRack-32.png");
BitmapSource b = BitmapFrame.Create(s);

8.如果您是.net core下的wpf使用,那么还需要自己添加资源文件,有了资源文件后,将图片放到资源文件中,然后直接C#代码访问该类即可,该类会生成资源文件静态属性

在资源文件中添加各种图片资源

随后,打开该资源文件的Designer.cs文件

您会看到该资源文件下的静态属性,有了图片的静态属性,您肯定会使用了。

9.如果您是.net 5.0/6.0 或者.net core,那么System.Drawing命名空间,您将不再适用,那么图片如何取出来并赋给PictureBox呢?

首先,.net core下的操作图片,需要安装其它nuget包,有好几种包都能使用,如IMageSharp等,但我这里推荐您使用微软自带的Microsoft.Maui.Graphics。安装就不说了,nuget搜索安装即可。

您不能安装System.Drawing.Common,虽然它原本是为了处理跨平台的,但其本质还是windows平台,会遇到很多你很麻烦的问题。

为了一次性解决,您务必安装真正的跨平台图像处理类库,如上述所推荐。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhongzhouShaonian.Ziyuan.Expression.Expressions;
using Microsoft.Maui.Graphics.Platform;
using Microsoft.Maui.Graphics;
using System.Reflection;
using System.IO;

namespace ZhongzhouShaonian.Ziyuan.Component.Controls
{
    /// <inheritdoc cref="IChoiceControl"/>
    public class ChoiceControl : ClassControl, IChoiceControl
    {
        /// <inheritdoc/>
        public IImage Icon { get; set; }

        /// <inheritdoc cref="ChoiceControl"/>
        public ChoiceControl()
        {
            IImage image;
            Assembly assembly = GetType().GetTypeInfo().Assembly;
            using (Stream stream = assembly.GetManifestResourceStream("Component.Controls.Classification.Icons.Choice.png"))
            {
                image = PlatformImage.FromStream(stream);
            }

            Icon = image;
        }
    }
}

上述代码,可以让您访问得到一个“IImage”对象,通过这个对像,可以使用下面的代码,给PictureBox赋值:

using Microsoft.Maui.Graphics;
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 Winform.UI.UserControls
{
    /// <summary>
    /// 表示入口主控件
    /// </summary>
    public partial class EntranceUserControl : UserControlBase
    {
        /// <inheritdoc cref="EntranceUserControl"/>
        public EntranceUserControl()
        {
            InitializeComponent();
        }

        /// <inheritdoc/>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ChoiceControl control = new ChoiceControl();
            IImage icon = control.Icon;
            pictureBox1.Image = Image.FromStream(icon.AsStream(ImageFormat.Png));
        }

    }
}

最后,贴上一张效果图:

祝您用餐愉快。

  • 28
    点赞
  • 154
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值