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:
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim orgImage As Bitmap = PictureBox1.Image '~~~ original image
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)
Dim compImage As New Bitmap(orgImage.Width, orgImage.Height) '~~~ final image
'~~~ prepare the final image's background (ie. create a black background)
Using g As Graphics = Graphics.FromImage(compImage)
'~~~ loop through the pixels, compare the colors of the mask image and put color based on the mask.
For i As Integer = 0 To maskImage.Width - 1
For j As Integer = 0 To maskImage.Height - 1
'~~~ 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
If maskImage.GetPixel(i, j) = Color.FromArgb(255, Color.White) AndAlso orgImage.GetPixel(i, j) <> Color.FromArgb(0, 0, 0, 0) Then
compImage.SetPixel(i, j, orgImage.GetPixel(i, j)) '~~~ put the original color
ElseIf orgImage.GetPixel(i, j) = Color.FromArgb(0, 0, 0, 0) Then '~~~ check if it is transparent color
compImage.SetPixel(i, j, Color.FromArgb(0, 0, 0, 0)) '~~~ put the transparent color
End If
Next
Next
'~~~ display the output
Me.PictureBox2.Image = compImage
End Sub
End Class
'~~~ the following code is provided by ForumAccount
'~~~ Thanks to him :)
Public Class TextImageFiller
'//methods
Public Shared Function Fill(ByVal text As String, _
ByVal imageSize As Size, ByVal font As Font) As Bitmap
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
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
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