gpt4 book ai didi

c# - XAML WPF 如何在 FlowDocument 上添加内嵌背景图片?

转载 作者:行者123 更新时间:2023-11-30 22:04:01 26 4
gpt4 key购买 nike

下面的代码是给一个Flow Document添加背景图片

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<FlowDocument.Background>
<ImageBrush ImageSource="C:\licorice.jpg" />
</FlowDocument.Background>
<Paragraph>
<Run>Hello World!</Run>
</Paragraph>
</FlowDocument>

问题是,如何更改 ImageSource 以便将图像数据作为字符串存储在 xaml 文件中? ImageBrush 是密封的,所以我无法从中派生。我正在寻找这样的东西:

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<FlowDocument.Background>
<InlineImageBrush base64Source="iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCA..." /> <!--does not work-->
</FlowDocument.Background>
<Paragraph>
<Run>Hello World!</Run>
</Paragraph>
</FlowDocument>

最佳答案

您可以通过以下机制(桌面 WPF)执行此操作:

  1. 创建自定义 DependencyObject一个子类 DependencyProperty - base 64 图像字符串。给你的类(class)打电话,例如 ImageData .完成此操作后,您可以将类的命名实例添加到您的 FlowDocument.ResourcesWindow.Resources (或 Grid.Resources 或其他),使用直接在 XAML 中初始化的 base64 字符串。

  2. 创建自定义 IValueConverter,将 base64 字符串转换为 BitmapImage .将此添加到您的 Window.Resources作为命名的静态资源。

  3. 对于要用作流程文档背景的每个图像,添加 ImageData到流文档本身的静态资源,或更高级别的控件,如窗口。 (注意——在我的旧版 Visual Studio 上,如果将图像资源添加到流文档本身,表单设计者会感到困惑。尽管如此,应用程序还是成功编译并运行了。)

  4. 最后,添加一个DataBinding对于 Background.ImageBrush.ImageSource ,将其链接到您命名的 ImageData 的 base 64 字符串属性资源并使用您的自定义转换器将其转换为图像。

详情如下。首先,自定义ImageData类很简单:

public class ImageData : DependencyObject
{
public static readonly DependencyProperty Base64ImageDataProperty =
DependencyProperty.Register("Base64ImageData",
typeof(string),
typeof(ImageData));

public string Base64ImageData
{
get { return (string)(GetValue(Base64ImageDataProperty)); }
set { SetValue(Base64ImageDataProperty, value); }
}
}

接下来,自定义转换器和一些辅助实用程序:

public class Base64ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string base64String = value as string;
if (base64String == null)
return null;
return ImageHelper.Base64StringToBitmapImage(base64String);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

public static class ImageHelper
{
public static BitmapImage Base64StringToBitmapImage(string base64String)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(Convert.FromBase64String(base64String));
bitmapImage.EndInit();
return bitmapImage;
}

public static string FileToBase64String(string filename)
{
using (var stream = File.Open(filename, FileMode.Open))
using (var reader = new BinaryReader(stream))
{
byte[] allData = reader.ReadBytes((int)reader.BaseStream.Length);
return Convert.ToBase64String(allData);
}
}
}

将它放在窗口或应用程序的静态资源中,或其他一些对您方便且可以在整个应用程序中重复使用的中心位置:

<Window x:Class="RichTextBoxInputPanel.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:w="clr-namespace:RichTextBoxInputPanel"
Title="RichTextBoxInputPanel" Height="600" Width="1200" Loaded="Window_Loaded">
<Window.Resources>
<w:Base64ImageConverter x:Key="Base64ImageConverter"></w:Base64ImageConverter>
</Window.Resources>

您还可以将它添加到流文档本身的静态资源中,与 ImageData 一起如下所示。

现在,添加一个 ImageData到您的流程文档的资源:

            <FlowDocument >
<FlowDocument.Resources>
<w:ImageData x:Key="DocumentBackground" Base64ImageData="iVBORw0K...etc etc">
</w:ImageData>
</FlowDocument.Resources>

最后,为背景添加绑定(bind)属性:

               <FlowDocument.Background>
<ImageBrush>
<ImageBrush.ImageSource>
<Binding Converter="{StaticResource Base64ImageConverter}"
Source="{StaticResource DocumentBackground}"
Path="Base64ImageData" Mode="OneWay"></Binding>
</ImageBrush.ImageSource>
</ImageBrush>
</FlowDocument.Background>

最后,正如我上面提到的,将静态 ImageData流文档本身上的资源会导致 WPF 表单设计器(在 VS2008 上)抛出一个虚假错误。尽管出现错误,应用程序仍能成功编译和运行。移动 ImageData从流文档到更高级控件(例如包含它的 RichTextBox 或 FlowDocumentReader)的静态资源解决了这个问题。

关于c# - XAML WPF 如何在 FlowDocument 上添加内嵌背景图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25557891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com