2

I'm working with Windows Phone 8/C# Silverlight and using code similar to this to render text:

TextBlock drawStringInstance = new TextBlock();
drawStringInstance.Text = str;
drawStringInstance.Opacity = 1;

drawStringInstance.Measure(new Size(1000000, 1000000));
WriteableBitmap wb = new WriteableBitmap((int)drawStringInstance.ActualWidth, height);                        
wb.Render(drawStringInstance, null);
wb.Invalidate();

Notice that I don't save the image and draw it directly so there shouldn't be any saving artifacts. If I just place the text block I get much crisper text with less aliasing as such (left is the "good" rendering):

enter image description here

Is there something I can do to improve this or is this an inherent issue with the approach of WriteableBitmap.Render()?

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • 1
    I don't know that there's a way to fix that as the crisp anti-aliased text rendering is a artifact of the display, and when you render to the bitmap, it's not as effective. You could try as an experiment setting the `TextHintingMode` (http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.windows.media.textoptions.texthintingmode(v=vs.105).aspx) – WiredPrairie Feb 05 '14 at 14:07
  • Thanks, I'll try that although it looks to already be the readability default. – Shai Almog Feb 05 '14 at 14:25
  • Maybe the problem is `drawStringInstance.Measure(new Size(1000000, 1000000));` – Alberto Feb 05 '14 at 15:33
  • Why would that cause the problem? Isn't it just calculating the size? – Shai Almog Feb 05 '14 at 16:01

1 Answers1

1

I think you're not supposed to render elements that are not in the visual tree. In fact , your code does not render anything on my emulator.

Just add the textblock somewhere on the page (perhaps set the margin to -1000 so that it does not show) , then render it.

Leonardo C
  • 11
  • 1
  • Thanks. This does work for me but I think the problem is related to the fix I made here: http://stackoverflow.com/questions/21582742/how-to-work-with-pixels-in-windows-phone-c – Shai Almog Apr 19 '14 at 05:24