您可以使用以下方法实现此目的DrawingVisual
and DrawingImage
课程:
var random = new Random();
var pixels = new byte[256 * 256 * 4];
random.NextBytes(pixels);
BitmapSource bitmapSource = BitmapSource.Create(256, 256, 96, 96, PixelFormats.Pbgra32, null, pixels, 256 * 4);
var visual = new DrawingVisual();
using (DrawingContext drawingContext = visual.RenderOpen())
{
drawingContext.DrawImage(bitmapSource, new Rect(0, 0, 256, 256));
drawingContext.DrawText(
new FormattedText("Hi!", CultureInfo.InvariantCulture, FlowDirection.LeftToRight,
new Typeface("Segoe UI"), 32, Brushes.Black), new Point(0, 0));
}
var image = new DrawingImage(visual.Drawing);
Image1.Source = image;
不幸的是,您将不得不创建一个新的BitmapSource
因为目前我无法直接向其中写入文本。
或者你可以使用WriteableBitmapEx
: https://writeablebitmapex.codeplex.com/
- 使用以下命令从框架创建 WriteableBitmap
BitmapFactory
(1)
- 创建另一个 WriteableBitmap 并使用上面的方法(2)在其上绘制文本
- 将文本位图 (2) 位块传输到框架 (1) 上
结果相同,但方法不同,不确定方法 2 是否更好,因为它很麻烦。