简体   繁体   English

Windows窗体ImageList-添加具有相对路径的图像-无文件副本

[英]Windows forms ImageList - Add images with Relative path - No file copy

I'm trying to add images to my tree nodes ( ImageList.Add() ), but just can't figure out a nice way of doing it. 我正在尝试将图像添加到我的树节点( ImageList.Add() ),但无法找出一种很好的方法。

I've read from MSDN help I should use System.Drawing.Image.FromFile(path) . 我已经阅读了MSDN帮助,我应该使用System.Drawing.Image.FromFile(path) But cannot just get a file somewhere. 但是不能只是在某处获取文件。 I'm building a DLL, and want it to be a single file, no bitmaps being copied together with it. 我正在构建一个DLL,并希望它是一个文件,没有位图与它一起复制。

So I've read I should add Image files to the project and mark them with Build Action as "Resource". 因此,我读过我应该将Image文件添加到项目中,并用Build Action将其标记为“ Resource”。 Ok, but where do I get them??? 好的,但是我从哪里得到? I saw people using it in XAML files, but I don't have that. 我看到人们在XAML文件中使用它,但我没有。

Saw people using Resources.SomeName , but can't find those Resources class. 看到使用Resources.SomeName ,但是找不到那些Resources类。

So....How do I do it?? 所以....我该怎么做? I've got the files marked as resources, just need to add them to the ImageList. 我已经将文件标记为资源,只需要将它们添加到ImageList中即可。

By the way, I'd love to use the path relative to the Code File that is adding the images to the ImageList. 顺便说一句,我很想使用相对于将图像添加到ImageList的Code File的路径。 But if not possible, just relative to the assembly root. 但是,如果不可能,则仅相对于程序集根目录。

If you want to use file paths, for items that are in your project, you must set the "Copy to Output Directory" property to "Copy Always" or "Copy if newer", otherwise it won't be in the bin folder, and then you'll be trying to pass a path to a file that doesn't exist. 如果要使用文件路径,对于项目中的项目,必须将“复制到输出目录”属性设置为“始终复制”或“如果更新则复制”,否则它将不在bin文件夹中,然后您将尝试将路径传递到不存在的文件。 Build action isn't all that important in this scenario. 在这种情况下,构建动作并不是那么重要。

If you want to use compiled resources, and reference them via the Resources object, see the rest of my answer. 如果要使用已编译的资源,并通过Resources对象引用它们,请参见我的其余答案。 I assume you are using Visual Studio, 2005 or later. 我假设您使用的是Visual Studio 2005或更高版本。

To add an image as a compiled resource to a clean Windows Forms project, so that you can access it via Resources.SomeName do the following: 要将图像作为已编译资源添加到干净的Windows Forms项目中,以便可以通过Resources.SomeName访问它,请执行以下操作:

  1. In Solution Explorer, under the windows forms project (mine is called WinFormsApplication1), expand the "Properties" folder. 在解决方案资源管理器中,在Windows窗体项目(我的名为WinFormsApplication1)下,展开“属性”文件夹。 By default this folder should contain: AssemblyInfo.cs, Resources.resx, Settings.settings. 默认情况下,此文件夹应包含:AssemblyInfo.cs,Resources.resx,Settings.settings。
  2. Double-click on the Resources.resx file. 双击Resources.resx文件。 This will open an editor for it. 这将为其打开一个编辑器。 You'll probably see a table of strings, with columns "Name", "Value", "Comment". 您可能会看到一个字符串表,其中包含“名称”,“值”,“注释”列。
  3. Click the drop-down arrow on the "Add Resource" button, and select "Existing File", which will allow you to browse to the image you want to add. 单击“添加资源”按钮上的下拉箭头,然后选择“现有文件”,这将使您浏览到要添加的图像。
  4. You should now see the image appear in a gallery of sorts. 您现在应该看到图像出现在各种图库中。 Mine has the name TestImage 我的名字叫TestImage

Now when you edit the code (mine is Fom1.cs), I can access the image as a System.Drawing.Bitmap as Properties.Resources.TestImage. 现在,当您编辑代码(我的代码是Fom1.cs)时,我可以将其作为System.Drawing.Bitmap的图像作为Properties.Resources.TestImage进行访问。

To my mind, this is the best way to do images that you want compiled into the application. 在我看来,这是处理要编译到应用程序中的图像的最佳方法。 If you want user-added images, you'll need to use OpenFileDialog, or something like that to get your file path. 如果要添加用户添加的图像,则需要使用OpenFileDialog或类似的方法来获取文件路径。 Then the Image.FormFile() will be what you want. 然后Image.FormFile()将是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM