简体   繁体   English

如何将背景图像和按钮图像嵌入到Visual Studio中?

[英]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. 我是编码新手,目前正在为正在构建的WPF应用程序使用背景图像。 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? 有什么方法可以将此图像嵌入到Visual Studio解决方案中,而不必复制它? 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. 我假设我不会使用UrlKind.Relative以及类似现在的位置,因为它不在目录中。 I'd be ok doing this in XAML too if there's an easier way, I just am kind of new to all this. 如果有更简单的方法,我也可以在XAML中做到这一点,我只是所有这些的新手。

Thanks! 谢谢!

You can embed the image in your project and set it's build action to resource . 您可以将图像嵌入项目中,并将其生成操作设置为resource

Then you can access it by 然后您可以通过

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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