Skip to content

Instantly share code, notes, and snippets.

@sehrgut
Last active December 21, 2015 13:19
Show Gist options
  • Save sehrgut/6311694 to your computer and use it in GitHub Desktop.
Save sehrgut/6311694 to your computer and use it in GitHub Desktop.
`System.Drawing.Graphics#MeasureCharacterRanges` has a hard limit of 32 `CharacterRange`s for measurement, so here's a workaround. MS has stated they have no intention of fixing this, so get used to it, kids. http://connect.microsoft.com/VisualStudio/feedback/details/96250/setmeasurablecharacterranges-crashes-with-more-than-32-characters#tabs
' This document is free and open-source software licensed simultaneously under the MIT License,
' the GPL v2, and the two-clause BSD License.
Public Function MeasureAllCharacterRanges(ByVal g As Graphics, ByVal txt As String, ByVal font As Font, ByVal dest As RectangleF,
ByVal format As StringFormat, ByVal ranges As IEnumerable(Of CharacterRange)) As IEnumerable(Of Region)
Dim out As New List(Of Region)(ranges.Count)
' Defensive copy to not side-effect caller based on shared GDI format struct
Using fmt As StringFormat = format.Clone
While ranges.Any
fmt.SetMeasurableCharacterRanges(ranges.Take(32).ToArray)
out.AddRange(g.MeasureCharacterRanges(txt, font, dest, fmt))
ranges = ranges.Skip(32)
End While
End Using
Return out
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment