此代码生成以下图像。为什么所有的坐标和大小都很奇怪?
DrawingVisual visual = new DrawingVisual();
DrawingContext ctx = visual.RenderOpen();
FormattedText txt = new FormattedText("45", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Verdana"), 100, Brushes.Red);
ctx.DrawRectangle(Brushes.White, new Pen(Brushes.White, 10), new System.Windows.Rect(0, 0, 400, 400));
ctx.DrawText(txt, new System.Windows.Point((300 - txt.Width)/2, 10));
ctx.Close();
RenderTargetBitmap bity = new RenderTargetBitmap(300, 300, 40, 40, PixelFormats.Default);
bity.Render(visual);
BitmapFrame frame = BitmapFrame.Create(bity);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(frame);
MemoryStream ms = new MemoryStream();
encoder.Save(ms);
如果位图是300×300,为什么不白色矩形(0,0,400,400)仅占据它的一小部分? 为什么不是文字居中?
我甚至不确定谷歌有什么条款。我寻求智慧。
尝试使用ActualWidth而不是宽度 – Tigran
@Tigran,这可能会解决问题,但它不会传达任何理解*为什么会发生这种情况。 – Amy
@lnuyasha:宽度是尺寸声明,ActualWidth是实际在您的机器的显示器上呈现的尺寸。 – Tigran