1

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 the Ideal 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, than Ideal-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)

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324

2 Answers2

0

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.

  • Do you have a complete example which produces a png? If so, could you please post it on pastebin.com or similar? – Roman Starkov Sep 19 '13 at 18:08
  • Yep, try this http://pastebin.com/8N2pbAHU. I replaced some vars I had in the paste so the code is untested. – Chris Ellingsworth Sep 23 '13 at 15:28
  • I've tried this out. I don't think it has the same effect as `TextFormattingMode.Display` does on an actual screen: specifically, there is no ClearType (no sub-pixel anti-aliasing). Given the latest developments (blurry non-ClearTyped rendering in MS Office 2013 and Windows 8) this isn't too surprising I guess... – Roman Starkov Oct 04 '13 at 14:35
  • Yes, I agree, being able to set the rendering mode would be nice! Because of that, and some other reasons, I'm going the Freetype route to render text offscreen. Freetype just included Adobe's hinting algorithm which looks nice and renders to my needs. – Chris Ellingsworth Oct 07 '13 at 14:19
0

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.

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324