如何将图像文件(添加到项目中)加载到Image对象中? [英] How to load image files (added to the project) into Image objects?

查看:114
本文介绍了如何将图像文件(添加到项目中)加载到Image对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中添加了一个新文件夹(名为Images),其中包含一些JPG文件.现在,我需要浏览该文件夹中的每个图像并将每个图像加载到Image对象中,然后再将其传递到PictureBox进行显示.能否请您提供一些执行此操作的示例代码?我已经看到一些使用System.Reflection.Assembly类和Bitmap.FromStream方法获取图像的代码.但是我想我不了解有关这项工作的所有重要注解,除了空值流之外什么也没得到.
非常感谢!

I have added a new folder (named Images) to my project, it contains some JPG files. Now I need to browse each image in that folder and load each one into an Image object before passing this to a PictureBox to show. Could you please provide me with some sample code doing that? I have seen some code using System.Reflection.Assembly class and Bitmap.FromStream method to get the image. But I think I didn''t know all significant notes about this work and couldn''t have gotten anything except null value streams.
Thank you so much!

推荐答案

这里查看 [ ^ ],不错的CodeProject成员在上面写了一篇文章.
Look here[^], a nice CodeProject member has written an article on it.


更容易:

Even easier:

string myImagePath = @"images\whatever.jpg";
pictureBox1.Image = Image.FromFile(myImagePath);



重要:您必须确保将图像文件的构建设置设置为:

内部版本:内容
复制到输出目录:如果较新则复制

注意:这是 不是 的默认设置.

如果没有将文件设置为复制到输出目录,则可能在运行时没有访问权限.



Important: You have to make sure the build settings for your image files are set to:

Build: Content
Copy to Output Directory: Copy if Newer

Note: This is not the default setting.

If you don''t have the file set to be copied to your output directory, you probably won''t have acces to them at runtime.


您可以执行其他操作:

You can do a different thing:


  1. 不要将图像文件手动添加到您的项目中,只需将其存储在解决方案之外的某个地方即可.
    创建.resx文件.
  2. 在.resx文件设计器资源中,使用添加现有文件".
  3. 添加所有图像文件.它们将被自动1)物理复制到同一目录,2)添加到项目,3)在.resx文件中由其相对文件名相应引用.图像文件不会在项目中获得复制"构建操作,因此该构建不会将其复制到输出目录中.这些文件将作为资源嵌入.
  4. 在.resx文件节点下查看自动生成的C#文件.找到生成的静态类和几个静态属性;这些属性的名称将类似于原始图像文件的文件名.
  5. 使用这些属性!
  6. 利润!
  7. —SA

  1. Don''t add image file to you project manually, just store it somewhere outside solution, to start from.
    Create .resx file.
  2. In .resx file designer Resource, use "Add existing file".
  3. Add all you image file. They will be automatically 1) physically copied to the same directory, 2) added to the project, 3) referenced in the .resx file by its relative file names accordingly. The image files won''t get "Copy" build action in the project, so the build won''t copy them in the output directory; the files will be embedded as resources.
  4. Look at the auto-generated C# file under the .resx file node. Locate generated static class and several static properties; the names of these properties will be similar to the file names of original image files.
  5. Use these properties!
  6. PROFIT!
  7. —SA


这篇关于如何将图像文件(添加到项目中)加载到Image对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆