如何在代码隐藏中设置边框背景图像画笔

发布于 2024-12-20 04:53:58 字数 665 浏览 4 评论 0原文

我有一个在 Windows 应用程序中使用的 WPF 自定义用户控件。该控件有一个边框作为主要元素,并且该边框有一个默认的背景图像。下面的代码显示了如何将此图像设置为默认图像。默认图像是资源元素 (Images/BlueRoad.jpg)。

我希望能够使用图像文件名作为字符串(例如“C:\Pictures\myCustomPic.bmp”)以编程方式更改边框背景的图像。我需要使用 Visual Basic 在代码隐藏中执行此操作,除非有非常简单的方法可以在 XAML 中执行此操作。无论哪种方式,图片都会加载到控件的启动代码中。

我对 WPF 不太了解,这只是应用程序的一个小元素,因此希望尽可能简单、快速地完成此任务。

非常感谢!

<Border Name="mainBorder" Opacity="1" BorderBrush="SteelBlue" BorderThickness="3">
    <Border.Background>
        <ImageBrush  ImageSource="Images/BlueRoad.jpg"></ImageBrush>
    </Border.Background>

     Grid and other stuff goes here...

 </Border> 

I have a WPF custom user control that is used in a Windows Application. The control has a border as the main element, and this border has a default background image. The code below shows how this image is set as a default. The default image is a resource element (Images/BlueRoad.jpg).

I want to be able to programmatically change the image of the border background using an image filename as string (e.g. "C:\Pictures\myCustomPic.bmp"). I need to do this in code-behind using Visual Basic, unless there is a VERY simple way to do it in XAML. Either way, the picture will load in the startup code for the control.

I do not know much about WPF and this is just a small element of the application, so want to get this done as simply and quickly as possible.

Many Thanks!

<Border Name="mainBorder" Opacity="1" BorderBrush="SteelBlue" BorderThickness="3">
    <Border.Background>
        <ImageBrush  ImageSource="Images/BlueRoad.jpg"></ImageBrush>
    </Border.Background>

     Grid and other stuff goes here...

 </Border> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℡寂寞咖啡 2024-12-27 04:53:58
using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;
...
var imagePath = @"pack://application:,,,/MyProject;component/Resources/BorderImage.png";
ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(imagePath, UriKind.Absolute)));
MyBorder.Background = brush;
using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;
...
var imagePath = @"pack://application:,,,/MyProject;component/Resources/BorderImage.png";
ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(imagePath, UriKind.Absolute)));
MyBorder.Background = brush;
走过海棠暮 2024-12-27 04:53:58

您可以使用 ImageBrush 和 BitmapImage 将画笔设置为边框背景
首先,使用 uri 创建 BitmapImage 并将此 BitmapImage 发送到 ImageBrush 并将 ImageBrush 分配给边框背景

you can use ImageBrush and BitmapImage to set brush to border background
first you create BitmapImage with uri and send this BitmapImage to ImageBrush and assign ImageBrush to border background

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文