C# WPF Load images from the exe folder

后端 未结 2 1996
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 06:20

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 fo

相关标签:
2条回答
  • 2020-12-21 06:34

    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

    0 讨论(0)
  • 2020-12-21 06:52

    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.

    enter image description here

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

    enter image description here

    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);
    
    0 讨论(0)
提交回复
热议问题