C#:访问添加到项目文件夹中的图像
我想知道以下内容:我将文件夹“Graphics”添加到我的项目中,并在其中粘贴了 BMP。现在我想从我的代码加载图像,但我不知道如何加载。我知道资源很简单,但是有没有办法不将图像添加到资源中?谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想知道以下内容:我将文件夹“Graphics”添加到我的项目中,并在其中粘贴了 BMP。现在我想从我的代码加载图像,但我不知道如何加载。我知道资源很简单,但是有没有办法不将图像添加到资源中?谢谢
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
我也曾经想过这个问题,所以我弄清楚了并将其放在 博客文章。对于您的示例,它应该是这样的:
记住将图像标记为嵌入资源并在完成后处理图像,这样就不会出现任何泄漏:)
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:
Remember to mark the image as an Embedded Resource and to dispose of the image when done so you don't get any leakage :)
您可以直接从文件系统加载图像
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.
要获取资源:
您可以将其转换为您需要的类型(或使用函数,例如 FromFile)
To get an resource:
You can cast that to the type you need (or use a function for example FromFile)
您应该在文件属性上指定在构建应用程序时必须部署它:
然后您可以使用 Image.FromFile 方法,路径将与您的项目中的路径完全相同。
You should specify on the file properties that it must be deployed when building the application :
You can then access the file using the Image.FromFile method, the path will be the exact same one as in your project.
我用过类似的东西..在 YouTube 教程视频中得到了它。
I used something like this.. got it in a YouTube tutorial video.