C# 如何加载自定义控件中使用的图像?

C# 如何加载自定义控件中使用的图像?,c#,image,visual-studio-2012,bitmap,custom-controls,C#,Image,Visual Studio 2012,Bitmap,Custom Controls,我正在创建一个自定义控件,一个用于C Windows窗体的媒体播放器工具 我的控件具有标准按钮的自定义图像,例如播放、暂停、停止、下一步、上一步、随机。我的问题是无法正确加载这些图像。由于我是在控件的构造函数中执行此操作的,因此在设计期间,当我从工具箱中将控件拖动到窗体上时,它会失败 当我使用实际文件的完整路径加载图像时,它可以正常工作 Image bmpPlay = Image.FromFile(@"C:\Users\CybeX\Documents\Visual Studio 2012\Pro

我正在创建一个自定义控件,一个用于C Windows窗体的媒体播放器工具

我的控件具有标准按钮的自定义图像,例如播放、暂停、停止、下一步、上一步、随机。我的问题是无法正确加载这些图像。由于我是在控件的构造函数中执行此操作的,因此在设计期间,当我从工具箱中将控件拖动到窗体上时,它会失败

当我使用实际文件的完整路径加载图像时,它可以正常工作

Image bmpPlay = Image.FromFile(@"C:\Users\CybeX\Documents\Visual Studio 2012\Projects\WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug\images\play.bmp");
但我不想这样做,因为很明显,路径会根据应用程序安装的位置而有所不同。我试着使用相对路径

Image bmpPause = Image.FromFile(@"Images\pause.bmp");
但这会引发异常,表示找不到指定的文件

我尝试将这些图像复制到应用程序的调试文件夹中,但出现了相同的错误

如何在运行时创建这些映像,使其无论应用程序安装在何处都能可靠工作

为了完整起见,这里是我控制的代码

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;
using System.IO;

namespace WindowsFormsApplication3
{
    public partial class mediaplayerbox : System.Windows.Forms.Button
    {    
        const int buttonwidth = 150;
        const int buttonheight = 150;        

        public mediaplayerbox()
        {
            InitializeComponent();

            Button btnPlay = new Button();
            Button btnPause = new Button();
            Button btnStop = new Button();
            Button btnNext = new Button();
            Button btnPrevious = new Button();
            Button btnPlayRandom = new Button();

            btnPlay.SetBounds(0, 0, buttonwidth, buttonheight);
            btnPause.SetBounds(btnPlay.Left + buttonwidth, 0, buttonwidth, buttonheight);
            btnStop.SetBounds(btnPause.Left + buttonwidth, 0, buttonwidth, buttonheight);
            btnNext.SetBounds(btnStop.Left + buttonwidth, 0, buttonwidth, buttonheight);
            btnPrevious.SetBounds(btnNext.Left + buttonwidth, 0, buttonwidth, buttonheight);
            btnPlayRandom.SetBounds(btnPrevious.Left + buttonwidth, 0, buttonwidth, buttonheight);

            Image bmpPlay = Image.FromFile(@"C:\Users\CybeX\Documents\Visual Studio 2012\Projects\WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug\images\play.bmp");
            Image bmpPause = Image.FromFile(@"Images\pause.bmp");
            Image bmpStop = Image.FromFile(@"Images\stop.bmp");
            Image bmpNext = Image.FromFile(@"Images\next.bmp");
            Image bmpPrevious = Image.FromFile(@"Images\previous.bmp");
            Image bmpPlayRandom = Image.FromFile(@"Images\play_random.bmp");

            btnPlay.Image = bmpPlay;
            btnPause.Image = bmpPause;
            btnStop.Image = bmpStop;
            btnPrevious.Image = bmpNext;
            btnNext.Image = bmpPrevious;
            btnPlayRandom.Image = bmpPlayRandom;

            btnPlay.ImageAlign = ContentAlignment.MiddleCenter;
            btnPause.ImageAlign = ContentAlignment.MiddleCenter;
            btnStop.ImageAlign = ContentAlignment.MiddleCenter;
            btnNext.ImageAlign = ContentAlignment.MiddleCenter;
            btnPrevious.ImageAlign = ContentAlignment.MiddleCenter;
            btnPlayRandom.ImageAlign = ContentAlignment.MiddleCenter;

            btnPlay.FlatStyle = FlatStyle.Flat;
            btnPlay.FlatStyle = FlatStyle.Flat;
            btnPlay.FlatStyle = FlatStyle.Flat;
            btnNext.FlatStyle = FlatStyle.Flat;
            btnPrevious.FlatStyle = FlatStyle.Flat;
            btnPlayRandom.FlatStyle = FlatStyle.Flat;    
        }
    }
}

不要使用硬编码路径!相反,创建一个资源文件并将图像嵌入其中,然后像往常一样加载资源。

您的错误消息会准确地告诉您问题所在。那么,为什么不提供图像的实际路径呢。。例如,在app.config文件中创建一个具有文件路径的条目这似乎非常简单,这正是我不想要的,这意味着对确切的文件路径进行硬编码,否则我如何通过使用名为images的子文件夹中的按钮来创建此控件,将其添加到工具箱中,它不会给我此错误,很明显,我没有看到,但是谢谢你的回答,我感谢你的帮助,毕竟这就是我问这个问题的目的,不是反对票,也没有帮助。你听说过资源文件吗。。?在谷歌上搜索如何使用它C@MethodMan谢谢你的建议,我并不是故意不尊重你,但如果你提出建议,请给出你建议的理由,我是大学二年级的学生,我们仍在学习fig.console.writeline,所以我不完全理解这些概念,那么,如果你能很好地解释一下这个问题,以及为什么你建议使用一个res.文件,为什么这会解决这个问题呢?让我用另一种方式重申我的问题。。创建console应用程序时,是否有作为项目/解决方案一部分的app.config文件。。?如果没有,那么我将阅读如何在.config文件中使用System.COnfiguration类。您可以添加键值对,并使用ConfigurationManager.AppSettings[您的密钥]访问它们。我希望这是有意义的。请给我一个示例,说明我必须如何做,我是第二耳大学的学生,我们仍在努力。writeline@user253935作为一名学生还不足以让你不知道如何在谷歌上进行简单的搜索。。。我已经给了你关键词;-别傻了。他正在运行一个表单应用程序,而您将@cybex链接到WPF应用程序资源。他需要将这些图像作为强类型嵌入图像添加到他的应用程序中,然后他可以从他的资源名称空间中简单地获取它们。这是我的一个答案,有一个特定的选项可以将它们添加为System.Drawing.Bitmap实例。