Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

WPF - Import image as resource

In WinForms it is possible to import an image as a resource, and the image would still work when compiled in the /bin/Debug folder.

I can't figure out how to get this working in WPF, when I run the application the image doesn't load, because the image is saved in /Projects/AppName/images/, and the application is compiled into /Projects/AppName/bin/Debug when I run it in Debug mode.

Do I simply need to make a copy of my Images folder and put it where the application is compiled? Or is there another way. Here is my code which displays my image:

<Image Width="300">
    <Image.Source>
        <BitmapImage DecodePixelWidth="300" UriSource="/images/jamsnaps-dark.png" />
    </Image.Source>
</Image>
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • Create a folder (e.g. images) in your Visual Studio Project.
  • Add the image file(s) to that folder.
  • Set their Build Action to Resource (in the Properties window, see second image in this answer).

Then write the UriSource property like you already did:

UriSource="/images/jamsnaps-dark.png"

That URI is effectively a Resource File Pack URI, where the prefix is automatically added by WPF.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...