MeasureString Using Computer Screen
1- Calculate Screen Rectangle
Friend Function ScreenToRectangle() As System.Drawing.Rectangle
Dim rect As System.Drawing.Rectangle = CType(Nothing, System.Drawing.Rectangle)
Dim devices As System.Windows.Forms.Screen() = System.Windows.Forms.Screen.AllScreens
For i As Integer = 0 To devices.Length - 1
Dim device As System.Windows.Forms.Screen = devices(i)
rect = System.Drawing.Rectangle.Union(rect, device.Bounds)
Next
Return rect
End Function
2- Capture Screen Image
Friend Function ScreenToImage() As System.Drawing.Image
Dim screenImage As System.Drawing.Bitmap = New System.Drawing.Bitmap(ScreenToRectangle.Width, ScreenToRectangle.Height)
Using g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenImage)
g.CopyFromScreen(ScreenToRectangle.X, ScreenToRectangle.Y, 0, 0, ScreenToRectangle.Size, System.Drawing.CopyPixelOperation.SourceCopy)
End Using
Return screenImage
End Function
3- MeasureString Using graphics and CharacterRange
Friend Function MeasureTextSize(g As Graphics, textFont As Font, textToMeasure As String, space As Single) As Size
Dim charsRegions As Region() = New Region() {}
Dim rect As RectangleF = New RectangleF(0, 0, ScreenToImage.Width, ScreenToImage.Height)
Dim ranges As CharacterRange() = New CharacterRange() {New CharacterRange(0, textToMeasure.Length)}
Using sf As StringFormat = New StringFormat()
sf.SetMeasurableCharacterRanges(ranges)
charsRegions = g.MeasureCharacterRanges(textToMeasure, textFont, rect, sf)
rect = charsRegions(0).GetBounds(g)
End Using
Return New Size(CInt((rect.Right + space)), CInt(textFont.GetHeight()))
End Function
4- MeasureString
Friend Function MeasureTextSize(textFont As Font, textToMeasure As String, space As Single) As Size
Dim result As Size = Size.Empty
Using g As Graphics = Graphics.FromImage(ScreenToImage)
Dim s As Size = MeasureTextSize(g, textFont, textToMeasure, space)
result = CType(s, Size)
End Using
Return result
End Function
Usage :
Dim textSize As Size = CType(Nothing, Size)
Dim demoText As String = "Demo Text"
textSize = MeasureTextSize(New Font(Me.Font.FontFamily, 30.0F, Me.Font.Style), demoText, 10.0F)
Dim result As String = textSize.Width & "," & textSize.Height
Friend Function ScreenToRectangle() As System.Drawing.Rectangle
Dim rect As System.Drawing.Rectangle = CType(Nothing, System.Drawing.Rectangle)
Dim devices As System.Windows.Forms.Screen() = System.Windows.Forms.Screen.AllScreens
For i As Integer = 0 To devices.Length - 1
Dim device As System.Windows.Forms.Screen = devices(i)
rect = System.Drawing.Rectangle.Union(rect, device.Bounds)
Next
Return rect
End Function
2- Capture Screen Image
Friend Function ScreenToImage() As System.Drawing.Image
Dim screenImage As System.Drawing.Bitmap = New System.Drawing.Bitmap(ScreenToRectangle.Width, ScreenToRectangle.Height)
Using g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenImage)
g.CopyFromScreen(ScreenToRectangle.X, ScreenToRectangle.Y, 0, 0, ScreenToRectangle.Size, System.Drawing.CopyPixelOperation.SourceCopy)
End Using
Return screenImage
End Function
3- MeasureString Using graphics and CharacterRange
Friend Function MeasureTextSize(g As Graphics, textFont As Font, textToMeasure As String, space As Single) As Size
Dim charsRegions As Region() = New Region() {}
Dim rect As RectangleF = New RectangleF(0, 0, ScreenToImage.Width, ScreenToImage.Height)
Dim ranges As CharacterRange() = New CharacterRange() {New CharacterRange(0, textToMeasure.Length)}
Using sf As StringFormat = New StringFormat()
sf.SetMeasurableCharacterRanges(ranges)
charsRegions = g.MeasureCharacterRanges(textToMeasure, textFont, rect, sf)
rect = charsRegions(0).GetBounds(g)
End Using
Return New Size(CInt((rect.Right + space)), CInt(textFont.GetHeight()))
End Function
4- MeasureString
Friend Function MeasureTextSize(textFont As Font, textToMeasure As String, space As Single) As Size
Dim result As Size = Size.Empty
Using g As Graphics = Graphics.FromImage(ScreenToImage)
Dim s As Size = MeasureTextSize(g, textFont, textToMeasure, space)
result = CType(s, Size)
End Using
Return result
End Function
Usage :
Dim textSize As Size = CType(Nothing, Size)
Dim demoText As String = "Demo Text"
textSize = MeasureTextSize(New Font(Me.Font.FontFamily, 30.0F, Me.Font.Style), demoText, 10.0F)
Dim result As String = textSize.Width & "," & textSize.Height
Comments
Post a Comment