【发布时间】:2011-03-27 12:29:26
【问题描述】:
众所周知,WPF 4.0 修复了 blurry text issue。设置TextOptions.TextFormattingMode="Display" 使用像素提示来排列字符,这对于提高清晰度非常有效。
但是,当程序在会话 0 中作为 Windows 服务运行时,它不起作用;文本回到“理想”渲染,小尺寸完全不可读。下面是两种效果图的对比。
不作为服务运行:
作为服务运行:
渲染代码:
//rtb is a RenderTargetBitmap
//c is a FormatConvertedBitmap with rtb as it's source
while (displayRunning) {
if (lcd != null) {
Dispatcher.Invoke(new ThreadStart(delegate() {
if (dv == null) {
dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen()) {
dc.DrawRectangle(Brushes.White, new Pen(), new Rect(0, 0, 256, 64));
dc.Close();
}
}
rtb.Render(dv);
rtb.Render((Visual)Content);
//bitmap output just for testing
PngBitmapEncoder e = new PngBitmapEncoder();
e.Frames.Add(BitmapFrame.Create(c));
using (FileStream f = File.Open("C:\\test.png", FileMode.Create))
e.Save(f);
WriteableBitmap bitmapdata = new WriteableBitmap(c);
srcdata = new byte[bitmapdata.BackBufferStride * bitmapdata.PixelHeight];
System.Runtime.InteropServices.Marshal.Copy(bitmapdata.BackBuffer, srcdata, 0, srcdata.Length);
}));
try {
framesender.Send(new PicoLCDFrame(srcdata, lcd.OutputReportLength), lcd);
} catch (NullReferenceException) { } // device was unplugged
}
Thread.Sleep(33);
}
我意识到在将字体渲染为服务时没有用于提示的屏幕像素,但它不应该从它渲染到的位图中获取像素提示吗?对此我有什么办法吗?
编辑:显然,它 IS 使用像素提示,但出于某种原因它是抗锯齿的。下面是下采样到 1 位/像素之前的渲染位图。
我确实设置了TextOptions.TextRenderingMode="Aliased",似乎 WPF 在作为服务运行时忽略了这一点?它需要在下采样时看起来不错。怎么强制?
EDIT2:当作为服务运行时,它可能与第 0 层(软件模式)中的 WPF 渲染有关,而当不作为服务运行时,它可能与第 2 层(硬件)中的 WPF 渲染有关。
EDIT3:在 Windows XP 上,作为服务,它呈现如下:
注意边距差异、字体大小差异以及其他方面的完美渲染。什么鬼?
【问题讨论】:
标签: wpf text drawing rendering