Get an image and paste text on it...

decrypt

Well-known member
Joined
Oct 6, 2003
Messages
216
How could you get an image from a file, and paste text on it, and then save it? (the program would do all of this automatically)
 
First create a Bitmap object from the file, create graphics from the image and use the DrawString method of the graphics to draw the text. Then simply use the Save method of the Bitmap object.
Code:
 First load the image
 Dim b As New Bitmap("path")
 get the graphics object from the image so you can draw onto the image
 Dim gr As Graphics = Graphics.FromImage(b)
 draw text
 gr.DrawString("text", Me.Font, New SolidBrush(Color.Red), 10, 10)
 save the image
 b.Save("path")
 dispose
 gr.Dispose()
 b.Dispose()
 
Simply create a new Font object and pass in the information you want into the constructor. Is that were you are encountering errors?
 
I tried this:

Code:
gr.DrawString("text", "Times New Roman", New SolidBrush(Color.Red), 10, 10)

How would i make it so that that will work?
 
edit: ah i c, i was supposed to put it in there, sry...

gr.DrawString("text", New Font(arguments), New SolidBrush(Color.Red), 10, 10)
 
Last edited by a moderator:
If you look closer at the arguments the constructor accepts you will see that it takes much more of them if you want to build a new font without a base one....
[edit]You edited your post so Im not sure where you are with this problem now :)[/edit]
 
I get an error:
Code:
 gr.DrawString(TextBox1.Text, New Font("Times New Roman", FontStyle.Regular), New SolidBrush(Color.FromArgb(0, 40, 40)), 10, 10)
 
The error is there becuase if you read the required argument type for the combination you showed you will see that it accepts a Font object that will be used as a base for the one you are creating. It doesnt accept a string. The try this and see if its what you want:
Code:
  New Font("Times New Roman", 12) 12 is the fonts size
 
darn beaten to the post , was just gonna put this ...
you must specify the Font size
Code:
        gr.DrawString(TextBox1.Text, New Font("Times New Roman", 12, FontStyle.Regular), New SolidBrush(Color.FromArgb(0, 40, 40)), 10, 10)
 
Thanks that worked perfectly, my next question was going to be how do you set the font size, but you just answered it. Thanks alot...
 
Back
Top