问题描述
众所周知,WPF 4.0修复了。设置 TextOptions.TextFormattingMode = Display
使用像素提示来排列字符,这非常有效地提高了清晰度。
WPF 4.0 notoriously fixed the blurry text issue. Setting TextOptions.TextFormattingMode="Display"
uses pixel hints to line up characters, which works really well to improve clarity.
但是,当程序在会话0中作为Windows服务运行时,它不起作用;文本返回到理想渲染,这在小尺寸下是完全不可读的。下面是这两种渲染的比较。
However, it does not work while the program is running as a Windows service in session 0; the text goes back to "Ideal" rendering, which at small sizes is completely unreadable. Below is a comparison of the two renderings.
未作为服务运行:
作为服务运行:
渲染代码:
//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);
}
我意识到在将字体作为服务呈现时,没有用于提示的屏幕像素,但是它不应该从要渲染到的位图中获取像素提示吗?我有什么能做的吗?
I realize there are no screen pixels for hints when rendering the fonts as a service, but shouldn't it get the pixel hints from the bitmap it is rendering to? Is there anything I can do about this?
编辑:显然,它 IS 使用像素提示,但是它由于某种原因而抗锯齿。下面是渲染的位图在降采样为1 bits / pxel之前。
Apparently, it IS using pixel hints, but it is anti-aliasing for some reason. Below is the rendered bitmap before it is downsampled to 1 bits/pxel.
我确实设置了 TextOptions.TextRenderingMode = Aliased
WPF是作为服务运行时忽略的吗?降采样后,它必须打开才能看起来不错。我该如何强制呢?
I do have TextOptions.TextRenderingMode="Aliased"
set and it seems that WPF is ignoring that when running as a service? It needs to be on to look good when downsampled. How can I force it?
EDIT2:可能与以0级(软件模式)运行的WPF渲染有关服务和方法2(硬件),而不是方法。
It may have something to do with WPF rendering in Tier 0 (software mode) when running as a service and Tier 2 (hardware) when not.
EDIT3:在Windows XP上,作为服务,它呈现如下:
On Windows XP, as a service, it renders like this:
请注意边距差异,字体大小差异以及其他完美的呈现方式。是WTF吗?
Notice the margin difference, the font size difference, and an otherwise perfect rendering. WTF?
推荐答案
Windows服务中的UserInterface代码不是一个好主意,因为它是不受支持的方案。 GDI +(System.Drawing。*)存在相同的限制,WPF也具有相同的限制。
UserInterface code in windows service is not a good idea its an unsupported scenario. The same limitation was there with GDI+ (System.Drawing.*) and same limitation applies to WPF also.
这篇关于WPF文字呈现不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!