[RESOLVED] GDI - How to draw characters or strings in a bitmap (fill it)
Hi guys
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):
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.
Thanks
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
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
I'll play with it
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
BTW, I got a fancy number for the thread id: 655555
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
Re: GDI - How to draw characters or strings in a bitmap (fill it)
Originally Posted by ForumAccount
Cool . Resolved?
Yes
And I have rated you too..
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India