Results 1 to 18 of 18

Thread: [VB2010] Image to Text-Image using GDI

Threaded View

  1. #1

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

    Arrow [VB2010] Image to Text-Image using GDI

    Hi guys

    This is a simple project created to find out how cool this could be.

    I was browsing through certain websites and I got stuck on an image where the image is made up of characters. So, I thought about creating one like that, in VB.Net

    My idea is the common method for composting (I believe so ). That is, a mask image used to hide all the unwanted places in the original image.

    So, the first part is generating the mask image. Since I'm an "expert" in GDI , I have posted my question in here and ForumAccount came into action by providing a sample code. Thanks to him
    Also, boops boops had given some tips too.
    So that part is clear.

    So, I have
    • orgImage - original image
    • maskImage - mask image (black and white) : white areas are what we nedded and black are unwanted areas

    Now the next part is the main composting part. What I did actually was, first created a new bitmap with black background, called compImage.

    Then looped through the pixels of maskImage and checked whether the color of the current pixel is white and not transparent (in case of transparent PNG images). If the condition is matched, I would copy the color of the origImage's current pixel color to the compImage's current pixel.

    And finally this compImage is displayed to the user.

    The code is as follows:
    vb.net Code:
    1. Imports System.Drawing.Imaging
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.  
    7.         Dim orgImage As Bitmap = PictureBox1.Image  '~~~ original image
    8.  
    9.         Dim maskImage As Bitmap = TextImageFiller.Fill("akhileshbc", PictureBox1.Size, Me.Font, Color.White, Color.Black)   '~~~ generating the mask by passing the text, size of image, fontcolor (should be white as we are using this for comparison) and backcolor (any color other than white)
    10.  
    11.         Dim compImage As New Bitmap(orgImage.Width, orgImage.Height)    '~~~ final image
    12.  
    13.  
    14.         '~~~ prepare the final image's background (ie. create a black background)
    15.         Using g As Graphics = Graphics.FromImage(compImage)
    16.  
    17.             g.FillRectangle(Brushes.Black, 0, 0, orgImage.Width, orgImage.Height)
    18.  
    19.         End Using
    20.  
    21.  
    22.         '~~~ loop through the pixels, compare the colors of the mask image and put color based on the mask.
    23.         For i As Integer = 0 To maskImage.Width - 1
    24.  
    25.             For j As Integer = 0 To maskImage.Height - 1
    26.  
    27.                 '~~~ if the mask image's current pixel's color is white and not transparent (in case of PNG images), then put that color in the final image's pixel position
    28.                 If maskImage.GetPixel(i, j) = Color.FromArgb(255, Color.White) AndAlso orgImage.GetPixel(i, j) <> Color.FromArgb(0, 0, 0, 0) Then
    29.  
    30.                     compImage.SetPixel(i, j, orgImage.GetPixel(i, j))   '~~~ put the original color
    31.  
    32.                 ElseIf orgImage.GetPixel(i, j) = Color.FromArgb(0, 0, 0, 0) Then '~~~ check if it is transparent color
    33.  
    34.                     compImage.SetPixel(i, j, Color.FromArgb(0, 0, 0, 0))   '~~~ put the transparent color
    35.  
    36.                 End If
    37.  
    38.             Next
    39.  
    40.         Next
    41.  
    42.  
    43.         '~~~ display the output
    44.         Me.PictureBox2.Image = compImage
    45.  
    46.     End Sub
    47.  
    48. End Class
    49.  
    50. '~~~ the following code is provided by ForumAccount
    51. '~~~ Thanks to him :)
    52. Public Class TextImageFiller
    53.  
    54.     '//methods
    55.     Public Shared Function Fill(ByVal text As String, _
    56.                                 ByVal imageSize As Size, ByVal font As Font) As Bitmap
    57.  
    58.         Return TextImageFiller.Fill(text, imageSize, font, Color.Black)
    59.     End Function
    60.  
    61.     Public Shared Function Fill(ByVal text As String, ByVal imageSize As Size, _
    62.                                 ByVal font As Font, ByVal fontColor As Color) As Bitmap
    63.  
    64.         Return TextImageFiller.Fill(text, imageSize, font, fontColor, Color.White)
    65.     End Function
    66.  
    67.     Public Shared Function Fill(ByVal text As String, ByVal imageSize As Size, _
    68.                                 ByVal font As Font, ByVal fontColor As Color, _
    69.                                 ByVal backColor As Color) As Bitmap
    70.  
    71.         Dim bmp = New Bitmap(imageSize.Width, imageSize.Height)
    72.  
    73.         Using g = Graphics.FromImage(bmp)
    74.  
    75.             Dim flags = TextFormatFlags.NoPadding
    76.             Dim sz = TextRenderer.MeasureText(g, text, font, Size.empty, flags)
    77.  
    78.             For x = 0 To bmp.Width - 1 Step sz.Width
    79.                 For y = 0 To bmp.Height - 1 Step sz.Height
    80.                     TextRenderer.DrawText(g, text, font, New Point(x, y), _
    81.                                           fontColor, backColor, flags)
    82.                 Next
    83.             Next
    84.  
    85.         End Using
    86.  
    87.         Return bmp
    88.     End Function
    89.  
    90. End Class
    I have attached two images and the source code. Comments/suggestions/appreciation/gifts are always welcome

    I hope you would give your ideas in making it better.

    Thanks
    Attached Images Attached Images   
    Attached Files Attached Files

    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