Results 1 to 6 of 6

Thread: [RESOLVED] GDI - How to draw characters or strings in a bitmap (fill it)

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Resolved [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):
    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.

    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


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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
    Attached Images Attached Images  

  3. #3

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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

    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


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: GDI - How to draw characters or strings in a bitmap (fill it)

    Cool . Resolved?

  6. #6

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: GDI - How to draw characters or strings in a bitmap (fill it)

    Quote Originally Posted by ForumAccount View Post
    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


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width