C# wpf 画布背景图片动态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9986650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:00:45  来源:igfitidea点击:

wpf canvas background image dynamically

c#wpfwindowscanvas

提问by Hasan Zubairi

In my application I want the user to load an image as the canvas background. How can I assign the image to canvas background?

在我的应用程序中,我希望用户加载一个图像作为画布背景。如何将图像分配给画布背景?

采纳答案by Nikhil Agrawal

Use ImageBrush to set Canvas.Background property

使用 ImageBrush 设置 Canvas.Background 属性

ImageBrush ib = new ImageBrush();
ib.ImageSource = new BitmapImage(new Uri(@"sampleImages\berries.jpg", UriKind.Relative));
mycanvas.Background = ib;