我的 UWP Win2D 应用程序出现问题。我在 CanvasAnimatedControl 中显示了一个图像和文本,当我调整窗口大小时,它会拉伸图像和文本而不是保持它们的比例:
这是加载和绘制图像的代码的一部分:
private void Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
{
if (!IsLoadInProgress())
{
args.DrawingSession.DrawImage(_bitmap);
DrawText(args, "...TESTING TEXT...");
}
}
private void DrawText(CanvasAnimatedDrawEventArgs args, string text)
{
using (var textFormat = new CanvasTextFormat()
{
FontSize = 96,
FontFamily = "Segoe UI",
FontWeight = FontWeights.Medium
})
{
args.DrawingSession.DrawText(
text,
new Vector2(20, (float)_bitmap.Size.Height / 2),
Colors.White,
textFormat);
}
}
private async Task SetImageAndSize()
{
if (_imagePath != null)
{
_bitmap = await CanvasBitmap.LoadAsync(AnimatedControl, new Uri(_imagePath, UriKind.RelativeOrAbsolute));
// Set the controls size according to image pixels to display for image
AnimatedControl.Height = _bitmap.SizeInPixels.Height;
AnimatedControl.Width = _bitmap.SizeInPixels.Width;
}
}
示例项目可以在这里下载:
我需要更改什么以使图像和文本不会在调整大小时拉伸?