问题
I'm drawing certain images in WPF which will be displayed by a game (developed by a third party). I currently produce the images using a RenderTargetBitmap
. Unfortunately it seems that this only supports the Ideal
text formatting mode, resulting in blurry small fonts. The application is a third-party game and thus there's no way around using images.
Can I tell the RenderTargetBitmap
to assume that it's drawing an image destined for one of the current montiors? Is there another way to get WPF to use the Display
rendering mode for off-screen drawing?
I understand why this might seem wrong in the theoretical sense, but in practice there are reasons why I think this is not an unreasonable thing to do:
- One of the things the
Display
mode allows is aliased text, which looks better at small sizes than theIdeal
rendering, and is completely independent of monitor properties such as gamma. - A screenshot of small
Display
-mode text rendered in ClearType looks far better on any screen, even those with different gamma, thanIdeal
-mode text.
Can the WPF rendering engine do this, or do I have to fall back onto GDI? (which has no difficulties with using Aliased or ClearType rendering off-screen)
回答1:
There is certainly no obvious way of doing this. I guess drawing to images was never a goal of WPF; the fact that it can actually do this fairly well most of the time must be accidental.
回答2:
It seems that this works now. Could someone else verify? Here's the relevant code:
var textBlock = new TextBlock();
textBlock.Text = "Hello World";
textBlock.FontFamily = new System.Windows.Media.FontFamily("Arial");
textBlock.Background = System.Windows.Media.Brushes.Transparent;
textBlock.Foreground = System.Windows.Media.Brushes.Black;
textBlock.FontSize = 50;
// . Set Formatting Mode Works! Setting the rendering mode doesn't.
System.Windows.Media.TextOptions.SetTextFormattingMode(textBlock, System.Windows.Media.TextFormattingMode.Display);
Edit: Forgot to mention I'm using the .NET 4.5 framework
Edit2: The difference between Display and Ideal is especially noticeable at smaller font sizes.
来源:https://stackoverflow.com/questions/9083982/can-i-get-textformattingmode-display-while-drawing-off-screen-e-g-rendertarget