Click here to Skip to main content
16,011,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Once again, I need HELP!!! In the code below I am displaying a word "WATER" in a picture box (PicBox1) on Form1. Not sure if the code will work but my initial problem is this,,,when I run the code I get an error message telling me that I need to use the New Keyword to create a new instance of the object (Dim myBitMap etc).

Tried this: PicBox1.Image = New Bitmap(PicBox1.Image) to no avail.

Any thoughts???

Secondly: my goal in this code is to display random words in a picture box that will appear "invisible" to the user...will eventually use Color.Fromargb(255,10,10,10) so that the word blends into the black background. Then the code will replace the black background (color.fromargb(255,0,0,0) pixel by pixel with color.red. The word will slowly standard out from the background and will become indentifiable to the user....so any additional suggestions for this novice programmer will be really appreciated.

Thanks
Gary V

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myBitMap As New Bitmap(PicBox1.Image)
Dim g As Graphics = PicBox1.CreateGraphics()
Dim MyColor As Color
Dim verdanaFamily As New FontFamily("Verdana")
Dim verdanaFont As New Font("Verdana", 90)
Dim pointF As New PointF(30, 60)
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))

Dim Xcount As Integer
Dim Ycount As Integer
 PicBox1.BackColor = Color.FromArgb(255, 0, 0, 0)

PicBox1.Image = New Bitmap(PicBox1.Image)
g.DrawString("WATER", verdanaFont, New SolidBrush(Color.FromArgb(255, 0, 0, 255)), New PointF(1, 1))
     
solidBrush.Dispose()
g.Dispose()


      
      For Xcount = 0 To myBitMap.Width - 1
          
          For Ycount = 0 To myBitMap.Height - 1
              MyColor = myBitMap.GetPixel(Xcount, Ycount)
              If MyColor = Color.FromArgb(255, 0, 0, 0) Then
                  myBitMap.SetPixel(Xcount, Ycount, Color.Red)
              End If
              
          Next Ycount
      Next Xcount
  End Sub
Posted
Updated 4-Jan-11 12:19pm
v2

Okay, if PicBox1.Image is Nothing, then this will fail:

VB
PicBox1.Image = New Bitmap(PicBox1.Image)


as well this:

VB
Dim myBitMap As New Bitmap(PicBox1.Image)


because you are trying to copy an image that doesn't exist. The immediate fix to this is do something like this

VB
Dim myBitMap As New Bitmap(10,20)


Which will create a new image that is 10x20.

I will point out that your code won't do what you want. The UI won't redraw until after the event handler is finished, as both run in the UI thread. All the user is likely to see is a long wait (GetPixel is slow), and then an immediate jump to the final image. My suggestion is that you look at the Timer control, doing part of the revealing at a time in the Tick event. Provided you don't take too long to change the image, the user should see a suitable effect.

Alternativly, a simpler solution would be to have a label where the foreground and background colours are the same. You can then progressivly change the background colour (again using a Timer), revealing the word without all the messing around with GDI.

However, with respect, I think you should be (re)visiting some Visual Basic basics from a book or similar. The first problem should be pretty trivial to solve with suitable knowledge about how variables work in Visual Basic.
 
Share this answer
 
Comments
GaryV100 4-Jan-11 19:32pm    
Thanks so much for your help!!! I was pretty sure the second part of the code would not work but I was not sure why...
You are right...I was a very amateur, weekend VB6 programmer and I am now trying to figure out how to convert some of my code into VB10!!! Google and this site have been a blessing! Could recommend a VB.net book that might be helpful in working with basic graphic functions in VB.net?? Basics would be good!!!

Thanks again!!!!!
Gary V
Also, using GetPixel will be PAINFULLY slow. VB.NET has no method of manipulating an image like this with any speed. This kind of code is better handled in C# because of it's support of pointers and "unsafe" code.

Search the articles for "Image processing for dummies" (yeah, ytou read that right) for some great examples on how to do this in C#.
 
Share this answer
 
Dave, thanks for your input!!
Interesting, after searching Google, I tried a combination of g.CopyFromScreen(New Point(X,Y, New Point(0,0),New Size(1,1)) then used GetPixel to determine the pixel color. Then I used the DrawEllipse method with a (1,1) circle size to replace the black color with red(for example). This worked absolutely perfect on my desk top running XP. Ran quite fast!! When I moved the code to a lap top running Vista....it ran PAINFULLY slow!! So, thanks for the confirmation that it is probably not my limited programming ability.

Gary V
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900