此代碼生成以下圖像。爲什麼所有的座標和大小都很奇怪?
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