我找到了一种让图像中的文本正常工作的好方法。
我使用网格控件将图像和两个文本框相互映射。
确保首先声明图像,这意味着它首先呈现。
<Grid Grid.Row="1" x:Name="Capture_Grid">
<Image Binding="{Binding Image}" />
<TextBox x:Name="UpperCaptionBorder_TextBox" Style="{StaticResource CaptionTextBoxStyle}"
Text="text" TextWrapping="Wrap" FontSize="32"
HorizontalAlignment="Center" VerticalAlignment="Bottom"
SelectionChanged="UpperCaption_TextBox_SelectionChanged"
/>
<TextBox x:Name="LowerCaptionBorder_TextBox" Style="{StaticResource CaptionTextBoxStyle}"
Text="text" TextWrapping="Wrap" FontSize="32"
HorizontalAlignment="Center" VerticalAlignment="Bottom"
SelectionChanged="LowerCaption_TextBox_SelectionChanged"
/>
</Grid>
然后我可以将整个 Grid 控件保存为“Scresn Shot”,但它只会与 Grid 实际的宽度和高度一样。
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(Capture_Grid);
var pixels = await renderTargetBitmap.GetPixelsAsync();
Grid element = Capture_Grid;
//文件扩展名修正
var file = await KnownFolders.PicturesLibrary.CreateFileAsync("pic.png", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await
BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
byte[] bytes = pixels.ToArray();
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
96, 96, bytes);
await encoder.FlushAsync();
}
它还将图像(网格 + 2 个文本框)保存在图像中心。