C# WPF Load images from the exe folder

时光毁灭记忆、已成空白 提交于 2019-11-28 09:02:18

问题


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.

image2.Source = new BitmapImage(new Uri(@"Res\startoh.png"));

回答1:


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.

Create a folder in your project (let's say called Images) and add your images to that folder.

Make sure that the Build Action for the images is set to Resource.

Now you can simply create a BitmapImage from such a resource by an appropriate Pack URI:

var uri = new Uri("pack://application:,,,/Images/SomeImage.png");
image.Source = new BitmapImage(uri);



回答2:


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...

Custom graphic in WPF application?

What is application's site of origin and when to use it



来源:https://stackoverflow.com/questions/16071371/c-sharp-wpf-load-images-from-the-exe-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!