C# WPF 从 exe 文件夹加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16071371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WPF Load images from the exe folder
提问by Rebeca Rdi
i want to move my program from a pc to another but the problem is the images are not loaded on any other pc (Source problem) . So i was wondering if i could just create a folder where the exe is placed and name it Resources and to load every image from there.
我想将我的程序从一台电脑移到另一台电脑,但问题是图像没有加载到任何其他电脑上(源问题)。所以我想知道我是否可以创建一个放置 exe 的文件夹并将其命名为 Resources 并从那里加载每个图像。
image2.Source = new BitmapImage(new Uri(@"Res\startoh.png"));
采纳答案by Clemens
You may just add the images as resources to your Visual Studio project. Then they will be packed into the assembly of the executable and you don't need to copy them separately.
您可以将图像作为资源添加到 Visual Studio 项目中。然后它们将被打包到可执行文件的程序集中,您不需要单独复制它们。
Create a folder in your project (let's say called Images) and add your images to that folder.
在您的项目中创建一个文件夹(假设名为Images)并将您的图像添加到该文件夹中。


Make sure that the Build Actionfor the images is set to Resource.
确保图像的构建操作设置为Resource。


Now you can simply create a BitmapImage from such a resource by an appropriate Pack URI:
现在,您可以通过适当的Pack URI从这样的资源中简单地创建一个 BitmapImage :
var uri = new Uri("pack://application:,,,/Images/SomeImage.png");
image.Source = new BitmapImage(uri);
回答by NSGaga-mostly-inactive
You could do something like this:
你可以这样做:
Source="pack://siteoforigin:,,,/Images/someimage.png"
and use images off of your bin/app folder. Take a look at this link for more info...
并使用 bin/app 文件夹中的图像。请查看此链接以获取更多信息...
Custom graphic in WPF application?

