简体   繁体   中英

How to embed background images and button images into Visual Studio?

I'm pretty new to coding and I am currently using a background image for a WPF application that I am building. Right now I have it working like this:

this.Background = new ImageBrush(new BitmapImage(new Uri("Background 1.jpg", UriKind.Relative)));

But this would require copying the image over to the final file when I go to publish this. Is there some way that I can embed this image in the visual studio solution so that I don't have to copy it? Or am I just stuck copying it over every time? Also, if I can embed an image into the file, how do I call it? I assume I wouldn't use UrlKind.Relative and the location like I am now because it wouldn't be right in the directory. I'd be ok doing this in XAML too if there's an easier way, I just am kind of new to all this.

Thanks!

You can embed the image in your project and set it's build action to resource .

Then you can access it by

Uri uri = new Uri("pack://application:,,,/Background 1.jpg");
BitmapImage bitmapImage = new BitmapImage(uri);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM