How to draw text on an image in a picturebox in vb.net?

366 views
Skip to first unread message

foody

unread,
May 16, 2008, 8:37:49 PM5/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hello,

In my program you can load an image of your liking then I want text to
appear with the image loaded without having the image disappear for
the new text. How do you do it? I am using VB.Net. I am not sure if
there is drawtext for vb.net or something like that? Thanks in
advance.

P.S "To know exactly what I am looking for please download this file
and check it out:
http://www.mcshaffry.com/GameCode/attachment.php?attachmentid=151&sid="

Thanks in advance.

Cerebrus

unread,
May 18, 2008, 3:16:42 AM5/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Unless you are clearing the graphics object, I don't see why your text
is not being drawn over the book.

I suggest you read up some tutorials on GDI/GDI+, TextRenderer class,
DrawText method, etc.

Andrew Badera

unread,
May 18, 2008, 5:15:21 AM5/18/08
to DotNetDe...@googlegroups.com
Please don't post links to binary or mystery meat downloads. Source only, and make sure the entire resource is obvious from the URL.
--
--
--Andy Badera
http://higherefficiency.net
http://flipbitsnotburgers.blogspot.com/
http://andrew.badera.us/
http://changeroundup.com/
and...@badera.us
(518) 641-1280
Google me: http://www.google.com/search?q=andrew+badera

Cerebrus

unread,
May 18, 2008, 9:18:42 AM5/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I kinda agree. It leaves the user guessing as to how many viruses he's
going to be assaulted with within a few seconds.

fahed al daye

unread,
May 18, 2008, 9:23:23 AM5/18/08
to DotNetDe...@googlegroups.com
It is funny how I did not get my answer after all of this :( That is
fine...I did solve it thankfully. But however I am sure this question
would be easier and hope someone can answer it here goes:


I want to ask a question concerning the string trimming here is my
situation:

I have a certain border where the string are allowed to be entered and
here I have the measurement:

[--------------------------------------------]

Between the [ and ] are where the strings are allowed to be entered,
now here is my test sentence:

"Hello world! I have very limited situation to arise but as long as it
works."

How do I truncate certain amount of the text above and take the
remaining truncated text on the next line and what is left of the text
if it too goes beyond the border it truncates what's left and again go
to the next line. For example this is what I want:

[--------------------------------------------]
Hello world! I have very limited
situation to arise but as long a
s long as it works.
[--------------------------------------------]

So my question is how do I take certain length of the string, split it
to the next line and repeat until there is no more string left. I
think it will be in some sort of loop where it takes the complete
string length, truncate certain amount and put the new one in the next
line and repeat until it is end of string.


--
Yours truly,
Fahed Al Daye

Cerebrus

unread,
May 19, 2008, 2:59:06 AM5/19/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You haven't posted any of your code, so it's difficult to tell what
method you are using. My guess is that you should use :

~ Graphics.MeasureString()

~ Graphics.DrawString() with the StringFormat parameter that allows
automatic word wrapping.

Andrew Badera

unread,
May 19, 2008, 5:37:06 AM5/19/08
to DotNetDe...@googlegroups.com
"Funny" ? not so much.

"Inevitable" ? sure!

1. You have to help us help you.
2. We're not paid to do this.
3. Sometimes we can't or simply don't WANT to help. So sue us.

Glenn

unread,
May 19, 2008, 8:30:12 AM5/19/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Sounds like you need a basic "text wrapping" function. Think it
through and you'll get it. Yes, we've done them, but we don't hand
out code here. Give it a shot and let us know what specific problems
you have with the code you've written.

...Glenn

foody

unread,
May 19, 2008, 9:04:21 AM5/19/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Alright, all your argument is fair enough this is the code here:

I am putting it in the textbox textchanged event where it takes the
first char of the string, check the length of the width. I f the width
is over the border of the restricted boundary set, go to the next
line...repeat process until it reaches the max height. Naturally the
delete function doesn't work because every time I press the delete
button on the keyboard I get index out of bound, I have to code the
delete function where in the drawstring I move one step to the left,
decrement the index of the strtest3 by one until I reach the top. What
is my objective in this whole affair it is simple let me show by this
link:

http://www.tlucretius.net/7thSea/bookgen.gif (this link is obtained
from google image there is no fear of virus or anything like that). If
you click on the link you will see the book open in two pages, this is
pretty much what is loaded in the picturebox of my project, but the
two pages are empty. Now at the begging of the code when I type
anything in the keyboard the txtContent textChange checks to see if I
reached the end of the first line if not keep drawing what I typed.
But if it did reach the end of the first line by using this statement
if fSize.Width > (dblMaxX - dblX) Then, then go one line down and
repeat the process. In the picture as you see, ones I reach the last
line of the first page then I use this code to see if I reached the
end of the height. If I did then I use this here:

dblX = imgLoadPicture.Width / 1.9
dblY = imgLoadPicture.Height / 10

so that I can reach the second page and put the cursor at first line
of the second page. My complete desire at the point when I reach the
end of the line of the second page I erase everything in the book and
start again from the beginning.

But it is not working correctly, the first part is half working, yes
it does what I want it keeps writing in the book until it reaches at
the end of the page, but if I made a mistake in my typing and I press
delete button, instead of deleting the text in the book it draws the
code of the delete and keeps going until I get an index out of
bound..which is wrong. That means I have to physically code the delete
button in such away that if I press delete I decrement the index
bound, I delete one character in the string I, decrement the X
coordinate so that it goes one back and if I reach at the beginning of
the line, I decrement the Y coordinate so I start at the end of the
previous line and keep until I reach at the beginning of the page.
That is if I am at the beginning of the page of the page, if I am
not, I have to continue decrementing the indexing bound until I am at
the beginning of the second page and then do mathematical calculation
until I am at the end of the line of the previous page and repeat
process when I press delete until I am at the beginning of the page of
the book. But my code is not working correctly, if you can see it can
you help them then? I tried my best to explain what I am looking for
what the code is doing, I hope I was descriptive on the matter ^_^* I
thank you guys for helping me on this issue, looking forward (now) for
any help in this regard.

Private Sub txtContent_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtContent.TextChanged
'ReDim Preserve strtest3(Len(strText))
If blNewLine = False Then
strtest3 = txtContent.Text
strText = strtest3
strtest = strText
fSize = g.MeasureString(strtest3, fntFonts)
' Check to see if I reached at the end of the page then go
to the next page
If fSize.Height > (dblMaxY - dblY) Then

dblX = imgLoadPicture.Width / 1.9
dblY = imgLoadPicture.Height / 10

strtest3 = txtContent.Text
strtest3(iCharCounter) = vbCr

newLen = Len(txtContent.Text)
strText.CopyTo(iCharCounter, strtest2, 0, (newLen -
iCharCounter))
'
While (iCharCounter <= UBound(strtest3))
' This code here displays the text on the
picturebox with the image loaded.
' bmpDrawText = New Bitmap(picBookPaperDoll.Image)
g = Graphics.FromImage(bmpDrawText)
If Asc(strtest3.GetValue(iCharCounter)) = 13 Then
strText2 = String.Empty ' We are in a new
line
blNewLine = True
dblY = dblY + 5
txtContent.Text = String.Empty
End If
iCharCounter = iCharCounter + 1
End While
iCharCounter = 0
' Check to see if I reached at the end of the line
then go down one line.
ElseIf fSize.Width > (dblMaxX - dblX) Then
strtest3 = txtContent.Text
strtest3(iCharCounter) = vbCr

newLen = Len(txtContent.Text)
strText.CopyTo(iCharCounter, strtest2, 0, (newLen -
iCharCounter))
'
While (iCharCounter <= UBound(strtest3))
' This code here displays the text on the
picturebox with the image loaded.
' bmpDrawText = New Bitmap(picBookPaperDoll.Image)
g = Graphics.FromImage(bmpDrawText)
If Asc(strtest3.GetValue(iCharCounter)) = 13 Then
strText2 = String.Empty ' We are in a new
line
blNewLine = True
dblY = dblY + 5
txtContent.Text = String.Empty
End If
iCharCounter = iCharCounter + 1
End While

End If

Try
If Asc(strtest3.GetValue(newBeginningCounter)) <> 13
Then
strText2 = strText2 &
strtest3.GetValue(newBeginningCounter).ToString
g.DrawString(strText2.ToString, fntFonts,
Brushes.Red, dblX, dblY)
End If
Catch ex As Exception
newBeginningCounter = 0
strText2 = strText2 &
strtest3.GetValue(newBeginningCounter).ToString
g.DrawString(strText2.ToString, fntFonts, Brushes.Red,
dblX, dblY)
End Try
newBeginningCounter = newBeginningCounter + 1 :
iCharCounter = iCharCounter + 1

picBookPaperDoll.Image = bmpDrawText
ElseIf blNewLine = True Then
blNewLine = False : iCharCounter = 0
End If

End Sub
Reply all
Reply to author
Forward
0 new messages