<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>JMSoftware &#187; TextRenderer</title>
	<atom:link href="http://www.jmsoftware.co.uk/blog/tag/textrenderer/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jmsoftware.co.uk</link>
	<description>Software Development</description>
	<lastBuildDate>Mon, 26 Sep 2011 13:38:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>TextRenderer is slow, DrawString is wrong</title>
		<link>http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong</link>
		<comments>http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong#comments</comments>
		<pubDate>Thu, 30 Aug 2007 22:42:38 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[api_functions]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[DrawString]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[string_array]]></category>
		<category><![CDATA[TextRenderer]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong</guid>
		<description><![CDATA[After struggling with WinAPI code and hacks in .NET 1.0 I was really looking forward to the TextRenderer class when I first heard about it. But as it turns out, not only is TextRenderer horrible at measuring character size it &#8230; <a href="http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After struggling with WinAPI code and hacks in .NET 1.0 I was really looking forward to the <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.textrenderer.aspx">TextRenderer</a> class when I first heard about it. But as it turns out, not only is TextRenderer horrible at measuring character size it is also monumentally slow at <em>rendering text</em>.<span id="more-18"></span></p>
<p>All I needed to do was to draw a series of characters onto an image, each one having a different Color value. Easy right? Straightforward code, plug it in to the Ascgen and&#8230; wait&#8230; we get the output required. After a quick profile of the application, it was clear that the problem was with the external function and there was nothing that could be done to speed things up to an acceptable level.</p>
<p>Before digging into api functions, I decided to look back to the older method of drawing strings: <a href="http://msdn2.microsoft.com/en-us/library/system.drawing.graphics.drawstring.aspx">Graphics.DrawString</a>.</p>
<p>The code is less straightforward, but, despite having to create a new SolidBrush object for every Color, it works quickly. An image that took around 10 seconds with TextRenderer takes milliseconds with DrawString. The only obvious problem was that DrawString shifted the text right a fraction, but a quick google leads to a solution: using StringFormat.GenericTypographic in the call.</p>
<p>Now, the next obvious step was to use DrawString to improve the speed of drawing text all in one Color, and that&#8217;s where it all goes wrong: DrawString adds extra space between characters, and there doesn&#8217;t seem to be a way to stop it.</p>
<p>Applying the same code as before and printing each character separately (after first having to split it into a string array) gives the text spaced correctly. However it&#8217;s actually marginally slower then calling TextRenderer once on the whole string, and far more complicated.</p>
<p>Ideally I&#8217;d like to put the two together, so I need to either find a way to speed up TextRenderer or a way to fix the spacing in DrawString.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

