C#:访问添加到项目文件夹中的图像

发布于 2024-08-24 14:05:23 字数 105 浏览 4 评论 0 原文

我想知道以下内容:我将文件夹“Graphics”添加到我的项目中,并在其中粘贴了 BMP。现在我想从我的代码加载图像,但我不知道如何加载。我知道资源很简单,但是有没有办法不将图像添加到资源中?谢谢

I would like to know following: I added the folder "Graphics" into my project and stuck a BMP in it. Now I would like to load the image from my code, but I cannot figure out how. I know its simple with resources but is there a way without adding the image into resources? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

万人眼中万个我 2024-08-31 14:05:24

我也曾经想过这个问题,所以我弄清楚了并将其放在 博客文章。对于您的示例,它应该是这样的:

var a = Assembly.GetExecutingAssembly(); // Or another Get method if you need to get it from some other assembly

var image = Image
    .FromStream(a.GetManifestResourceStream("DefaultNameSpace.Graphics.image.bmp"));

记住将图像标记为嵌入资源并在完成后处理图像,这样就不会出现任何泄漏:)

I once wondered about this too, so I figured it out and put it in a blog post. For your example, it should be something like this:

var a = Assembly.GetExecutingAssembly(); // Or another Get method if you need to get it from some other assembly

var image = Image
    .FromStream(a.GetManifestResourceStream("DefaultNameSpace.Graphics.image.bmp"));

Remember to mark the image as an Embedded Resource and to dispose of the image when done so you don't get any leakage :)

孤星 2024-08-31 14:05:24

您可以直接从文件系统加载图像

Image img = Image.FromFile( "\Graphics\ImageName.bmp" );

MSDN 文档位于此处

http://msdn.microsoft .com/en-us/library/system.drawing.image.fromfile.aspx

显然,您需要知道从中加载的目录和名称。

You can load an image directly from the filesystem

Image img = Image.FromFile( "\Graphics\ImageName.bmp" );

The MSDN documentation is here

http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx

Obviously you would need to know the directory and name you are loading from.

李白 2024-08-31 14:05:24

要获取资源:

myNamespace.Properties.Resources.images.<imagename>

您可以将其转换为您需要的类型(或使用函数,例如 FromFile)

To get an resource:

myNamespace.Properties.Resources.images.<imagename>

You can cast that to the type you need (or use a function for example FromFile)

下雨或天晴 2024-08-31 14:05:24

您应该在文件属性上指定在构建应用程序时必须部署它:

复制到输出目录:始终复制

然后您可以使用 Image.FromFile 方法,路径将与您的项目中的路径完全相同。

You should specify on the file properties that it must be deployed when building the application :

Copy to Output Directory : Copy always

You can then access the file using the Image.FromFile method, the path will be the exact same one as in your project.

我做我的改变 2024-08-31 14:05:24

我用过类似的东西..在 YouTube 教程视频中得到了它。

return new BitmapImage(new Uri($"pack://application:,,,/Images/imageFile.png"));

I used something like this.. got it in a YouTube tutorial video.

return new BitmapImage(new Uri(
quot;pack://application:,,,/Images/imageFile.png"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文