您可以通过以下机制(桌面 WPF)做到这一点:
创建一个自定义的DependencyObject 子类,其中包含一个DependencyProperty - 基数为64 的图像字符串。给你的班级打电话,例如,ImageData。完成此操作后,您可以将类的命名实例添加到 FlowDocument.Resources 或 Window.Resources(或 Grid.Resources 或其他),并在 XAML 中直接初始化 base64 字符串。
创建一个自定义 IValueConverter,将 base64 字符串转换为 BitmapImage。将此作为命名静态资源添加到您的 Window.Resources。
对于要用作流文档背景的每个图像,在流文档本身的静态资源或窗口等更高级别的控件中添加ImageData。 (注意——在我的旧版 Visual Studio 中,如果将图像资源添加到流文档本身,表单设计器会感到困惑。不过,应用程序编译并成功运行。)
-
最后,为Background.ImageBrush.ImageSource 添加一个DataBinding,将其链接到您命名的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>
最后,为背景添加绑定属性:
<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)可以解决此问题。