TextOptions.TextFormattingMode="Display"
使用像素提示来排列字符,这对于提高清晰度非常有效。
但是,当程序作为 Windows 服务在会话 0 中运行时,它不起作用;文本回到“理想”渲染,在小尺寸下完全无法阅读。下面是两个效果图的比较。不作为服务运行:
作为服务运行:
渲染代码:
//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"
EDIT2: 当作为服务运行时,它可能与第 0 层(软件模式)中的 WPF 渲染有关,而不是与第 2 层(硬件)中的 WPF 渲染有关。
EDIT3:
在Windows XP上,作为一项服务,它呈现如下:
注意边距差异、字体大小差异以及其他完美的渲染。什么鬼?
RenderTargetBitmap 始终在软件中渲染。不确定这是否是原因……或者是一个错误,但无论如何,结果是 RTB 似乎不支持文本选项。
Windows 服务中的用户界面代码不是一个好主意,它是一个不受支持的场景。 GDI+ (System.Drawing.*) 也存在同样的限制,WPF 也存在同样的限制。