[RESOLVED] GDI - How to draw characters or strings in a bitmap (fill it)
Hi guys :wave:
I was wondering how to draw characters/string on a bitmap.
For example: I have created a new Bitmap with resolution of 200x200. Now using Graphics.DrawString(), I have to draw text. I can draw text. But my question is, I want to fill it the image with that string.
Say, the text be "abc".
So, the output would look like this for 200x200 bitmap (assumption):
Code:
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
abcabcabcabcabcabcabcabcabc
How can this be done ?
I have googled and found that there is a method called MeasureString(). But how is it used in this context. Just give me a start(or clue) and I'll do the rest. :thumb:
Thanks :wave:
1 Attachment(s)
Re: GDI - How to draw characters or strings in a bitmap (fill it)
I got a little carried away, but this should do it:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Me.PictureBox1.Image = TextImageFiller.Fill("akhileshbc", New Size(200, 200), Me.Font)
End Sub
End Class
Public Class TextImageFiller
'//methods
Public Shared Function Fill(ByVal text As String, _
ByVal imageSize As Size, ByVal font As Font) As Bitmap
Return TextImageFiller.Fill(text, imageSize, font, Color.Black)
End Function
Public Shared Function Fill(ByVal text As String, ByVal imageSize As Size, _
ByVal font As Font, ByVal fontColor As Color) As Bitmap
Return TextImageFiller.Fill(text, imageSize, font, fontColor, Color.White)
End Function
Public Shared Function Fill(ByVal text As String, ByVal imageSize As Size, _
ByVal font As Font, ByVal fontColor As Color, _
ByVal backColor As Color) As Bitmap
Dim bmp = New Bitmap(imageSize.Width, imageSize.Height)
Using g = Graphics.FromImage(bmp)
Dim flags = TextFormatFlags.NoPadding
Dim sz = TextRenderer.MeasureText(g, text, font, Size.Empty, flags)
For x = 0 To bmp.Width - 1 Step sz.Width
For y = 0 To bmp.Height - 1 Step sz.Height
TextRenderer.DrawText(g, text, font, New Point(x, y), _
fontColor, backColor, flags)
Next
Next
End Using
Return bmp
End Function
End Class
Re: GDI - How to draw characters or strings in a bitmap (fill it)
That's great :thumb:
I'll play with it :wave:
Re: GDI - How to draw characters or strings in a bitmap (fill it)
I have made a CodeBank submission here : http://www.vbforums.com/showthread.php?t=655555 :)
BTW, I got a fancy number for the thread id: 655555 :lol:
Re: GDI - How to draw characters or strings in a bitmap (fill it)
Re: GDI - How to draw characters or strings in a bitmap (fill it)
Quote:
Originally Posted by
ForumAccount
Cool :). Resolved?
Yes :)
And I have rated you too.. ;)