|
|
|
start date: Mon, 20 Aug 2007 11:00:54 -0500,
posted on: microsoft.public.dotnet.framework.drawing
back
| Thread Index |
|
1
AMDRIT
|
|
2
schneider
|
Looking for a better way
Hello Everyone,
I have a need to generate images similar to what you would see in a periodic
table of elements or on a Scrabble board. It seems that I can get the
painted on the image, but laying the image out properly seems to be a
challenge.
Let's take the Scabble example, I think this would have less moving pieces
and thus the code illustration will be smaller. I have mocked up some code
so that we have something to point at.
For me is seems that the "Letter" is centered on the tile while the "Letter
Value" is in the bottome right hand corner. I do not know what the exact
perportions are, but I venture that the "Letter" is 3/4 the size of the tile
centered vertically and horizontally. Meanwhile the "Letter Value" consists
of 1/4 of the tile size centered in the bottom right corner of the tile.
My problems are: "A" and "W" or "B" and "T" look drastically different and
the values "1" and "10" also have noticable differences. It seems to me
that the text does not scale well. Perhaps I am to use another font, I have
tried many fonts, it just seems that this should look better.
Here is my code. The form contains a picturebox "picTile" who's size is
32x32 and a button "btnFillitIn". My apologies to Scrabbles' owner if I am
violating a copywrite, I just want my code to work better.
Thanks in advance
Private pintLetter As Integer = 64
Private Sub btnFillItIn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFillItIn.Click
Dim bmp As Bitmap
Dim gfx As Graphics
Dim fnt As Font
Dim rect As Rectangle
Dim targetText As String
Dim brush As SolidBrush
Dim format As StringFormat
Dim fSize As SizeF
Dim tSize As Single
bmp = New Bitmap(32, 32)
For i As Integer = 0 To 31
For j As Integer = 0 To 31
'Draw a black border
If i = 0 Or j = 0 Or i = 31 Or j = 31 Then
bmp.SetPixel(i, j, Color.Black)
Else
'fill in the tile as sandybrown
bmp.SetPixel(i, j, Color.SandyBrown)
End If
Next
Next
gfx = Graphics.FromImage(bmp)
targetText = NextLetter()
'Set the target rectangle
rect = New Rectangle(0, 0, 24, 24)
brush = New SolidBrush(Color.Black)
format = New StringFormat
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
tSize = rect.Width
'Set up our font, compute the fontsize to be smaller or equal to the
target rectangle
Do
fnt = New Font(Me.Font.FontFamily.Name, tSize, FontStyle.Bold,
GraphicsUnit.Pixel)
fSize = gfx.MeasureString(targetText, fnt)
tSize -= 0.1
Loop Until fSize.Width < rect.Width
gfx.DrawString(targetText, fnt, brush, rect, format)
rect = New Rectangle(18, 18, 10, 10)
targetText = gettextvalue
Do
fnt = New Font(Me.Font.FontFamily.Name, tSize, FontStyle.Bold,
GraphicsUnit.Pixel)
fSize = gfx.MeasureString(targetText, fnt)
tSize -= 0.1
Loop Until fSize.Width <= rect.Width
gfx.DrawString(targetText, fnt, brush, rect, format)
Me.picTile.Image = bmp.Clone
bmp.Dispose()
End Sub
Private Function NextLetter() As String
pintLetter += 1
If pintLetter > 90 Then
pintLetter = 65
ElseIf pintLetter < 65 Then
pintLetter = 65
End If
Return Chr(pintLetter)
End Function
Private Function GetTextValue() As Integer
Select Case pintLetter
Case 68, 71
Return "2"
Case 66, 67, 77, 80
Return "3"
Case 70, 72, 86, 87, 89
Return "4"
Case 75
Return "5"
Case 75, 88
Return "8"
Case 81, 90
Return "10"
Case Else
Return "1"
End Select
End Function
Date:Mon, 20 Aug 2007 11:00:54 -0500
Author:
|
Re: Looking for a better way
You could use a fixed width font like Courier new.
"AMDRIT" wrote in message
news:OJT3MN04HHA.3940@TK2MSFTNGP05.phx.gbl...
> Hello Everyone,
>
> I have a need to generate images similar to what you would see in a
> periodic table of elements or on a Scrabble board. It seems that I can
> get the painted on the image, but laying the image out properly seems to
> be a challenge.
>
> Let's take the Scabble example, I think this would have less moving pieces
> and thus the code illustration will be smaller. I have mocked up some
> code so that we have something to point at.
>
> For me is seems that the "Letter" is centered on the tile while the
> "Letter Value" is in the bottome right hand corner. I do not know what
> the exact perportions are, but I venture that the "Letter" is 3/4 the size
> of the tile centered vertically and horizontally. Meanwhile the "Letter
> Value" consists of 1/4 of the tile size centered in the bottom right
> corner of the tile.
>
> My problems are: "A" and "W" or "B" and "T" look drastically different
> and the values "1" and "10" also have noticable differences. It seems to
> me that the text does not scale well. Perhaps I am to use another font, I
> have tried many fonts, it just seems that this should look better.
>
> Here is my code. The form contains a picturebox "picTile" who's size is
> 32x32 and a button "btnFillitIn". My apologies to Scrabbles' owner if I
> am violating a copywrite, I just want my code to work better.
>
> Thanks in advance
>
> Private pintLetter As Integer = 64
>
> Private Sub btnFillItIn_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnFillItIn.Click
>
>
> Dim bmp As Bitmap
> Dim gfx As Graphics
> Dim fnt As Font
> Dim rect As Rectangle
> Dim targetText As String
> Dim brush As SolidBrush
> Dim format As StringFormat
> Dim fSize As SizeF
> Dim tSize As Single
>
> bmp = New Bitmap(32, 32)
>
> For i As Integer = 0 To 31
> For j As Integer = 0 To 31
>
> 'Draw a black border
> If i = 0 Or j = 0 Or i = 31 Or j = 31 Then
> bmp.SetPixel(i, j, Color.Black)
> Else
> 'fill in the tile as sandybrown
> bmp.SetPixel(i, j, Color.SandyBrown)
> End If
>
> Next
> Next
>
> gfx = Graphics.FromImage(bmp)
> targetText = NextLetter()
> 'Set the target rectangle
> rect = New Rectangle(0, 0, 24, 24)
> brush = New SolidBrush(Color.Black)
> format = New StringFormat
> format.Alignment = StringAlignment.Center
> format.LineAlignment = StringAlignment.Center
> tSize = rect.Width
>
> 'Set up our font, compute the fontsize to be smaller or equal to the
> target rectangle
> Do
> fnt = New Font(Me.Font.FontFamily.Name, tSize, FontStyle.Bold,
> GraphicsUnit.Pixel)
> fSize = gfx.MeasureString(targetText, fnt)
> tSize -= 0.1
> Loop Until fSize.Width < rect.Width
>
> gfx.DrawString(targetText, fnt, brush, rect, format)
>
> rect = New Rectangle(18, 18, 10, 10)
> targetText = gettextvalue
>
> Do
> fnt = New Font(Me.Font.FontFamily.Name, tSize, FontStyle.Bold,
> GraphicsUnit.Pixel)
> fSize = gfx.MeasureString(targetText, fnt)
> tSize -= 0.1
> Loop Until fSize.Width <= rect.Width
> gfx.DrawString(targetText, fnt, brush, rect, format)
>
> Me.picTile.Image = bmp.Clone
> bmp.Dispose()
>
> End Sub
>
> Private Function NextLetter() As String
>
> pintLetter += 1
>
> If pintLetter > 90 Then
> pintLetter = 65
> ElseIf pintLetter < 65 Then
> pintLetter = 65
> End If
>
> Return Chr(pintLetter)
>
> End Function
>
> Private Function GetTextValue() As Integer
>
> Select Case pintLetter
> Case 68, 71
> Return "2"
> Case 66, 67, 77, 80
> Return "3"
> Case 70, 72, 86, 87, 89
> Return "4"
> Case 75
> Return "5"
> Case 75, 88
> Return "8"
> Case 81, 90
> Return "10"
> Case Else
> Return "1"
> End Select
>
> End Function
>
Date:Mon, 20 Aug 2007 11:44:47 -0500
Author:
|
|
|