Draw a string onto a bitmap object
Author |
Message |
fix #1 / 8
|
 Draw a string onto a bitmap object
How can I do that? I will take a string from a text box and output that string as a bitmap. Thanks in advance. --
|
Sun, 12 Dec 2004 17:03:09 GMT |
|
 |
Armin Zingle #2 / 8
|
 Draw a string onto a bitmap object
Quote: > How can I do that? > I will take a string from a text box and output that string as a > bitmap. Thanks in advance.
'bmp' is the bitmap variable: dim g as graphics g = graphics.fromimage(bmp) g.DrawText (mytextbox.text, myfont, mybrush, 0, 0) Armin
|
Sun, 12 Dec 2004 17:25:06 GMT |
|
 |
fix #3 / 8
|
 Draw a string onto a bitmap object
Ah... I see, thanks! --
Quote:
> > How can I do that? > > I will take a string from a text box and output that string as a > > bitmap. Thanks in advance. > 'bmp' is the bitmap variable: > dim g as graphics > g = graphics.fromimage(bmp) > g.DrawText (mytextbox.text, myfont, mybrush, 0, 0) > Armin
|
Sun, 12 Dec 2004 18:01:25 GMT |
|
 |
Armin Zingle #4 / 8
|
 Draw a string onto a bitmap object
Quote:
> > How can I do that? > > I will take a string from a text box and output that string as a > > bitmap. Thanks in advance. > 'bmp' is the bitmap variable: > dim g as graphics > g = graphics.fromimage(bmp) > g.DrawText (mytextbox.text, myfont, mybrush, 0, 0)
I didn't read your question thoroughly. I read "on a bitmap", not "as a bitmap". I wrote the following function to create a bitmap from your textbox. The bitmap is not a 1:1 copy of the visual display of the textbox but it contains the text of the textbox. Private Function CreateBitmapFromTextBox( _ ByVal Textbox As TextBox) _ As Bitmap Dim Font As Font Dim g As Graphics Dim SizeF As SizeF Dim bmp As Bitmap Font = Textbox.Font g = Textbox.CreateGraphics() SizeF = g.MeasureString(Textbox.Text, Font) bmp = New Bitmap(CInt(SizeF.Width), CInt(SizeF.Height)) g = Graphics.FromImage(bmp) g.Clear(Textbox.BackColor) g.DrawString( _ Textbox.Text, Font, _ New SolidBrush(Textbox.ForeColor), 0, 0 _ ) Return bmp End Function Armin
|
Sun, 12 Dec 2004 18:07:32 GMT |
|
 |
fix #5 / 8
|
 Draw a string onto a bitmap object
And also, can I get the dimension of the string? I don't want a which is too big or too small. --
Quote:
> > How can I do that? > > I will take a string from a text box and output that string as a > > bitmap. Thanks in advance. > 'bmp' is the bitmap variable: > dim g as graphics > g = graphics.fromimage(bmp) > g.DrawText (mytextbox.text, myfont, mybrush, 0, 0) > Armin
|
Sun, 12 Dec 2004 18:11:19 GMT |
|
 |
fix #6 / 8
|
 Draw a string onto a bitmap object
If I just pass the string but not the textbox to the function, how should this line be replaced? (g = Textbox.CreateGraphics()) Thanks for your kind help! --
Quote:
> > > How can I do that? > > > I will take a string from a text box and output that string as a > > > bitmap. Thanks in advance. > > 'bmp' is the bitmap variable: > > dim g as graphics > > g = graphics.fromimage(bmp) > > g.DrawText (mytextbox.text, myfont, mybrush, 0, 0) > I didn't read your question thoroughly. I read "on a bitmap", not "as a > bitmap". I wrote the following function to create a bitmap from your > textbox. The bitmap is not a 1:1 copy of the visual display of the textbox > but it contains the text of the textbox. > Private Function CreateBitmapFromTextBox( _ > ByVal Textbox As TextBox) _ > As Bitmap > Dim Font As Font > Dim g As Graphics > Dim SizeF As SizeF > Dim bmp As Bitmap > Font = Textbox.Font > g = Textbox.CreateGraphics() > SizeF = g.MeasureString(Textbox.Text, Font) > bmp = New Bitmap(CInt(SizeF.Width), CInt(SizeF.Height)) > g = Graphics.FromImage(bmp) > g.Clear(Textbox.BackColor) > g.DrawString( _ > Textbox.Text, Font, _ > New SolidBrush(Textbox.ForeColor), 0, 0 _ > ) > Return bmp > End Function > Armin
|
Sun, 12 Dec 2004 18:31:17 GMT |
|
 |
Armin Zingle #7 / 8
|
 Draw a string onto a bitmap object
Quote: > If I just pass the string but not the textbox to the function, > how should this line be replaced? (g = Textbox.CreateGraphics()) > Thanks for your kind help!
Maybe there's a better solution, but...: (there's no reference font and color, so pass them to the function) Private Function CreateBitmapFromString( _ ByVal Text As String, _ ByVal Font As Font, _ ByVal ForeColor As Color, _ ByVal BackColor As Color) _ As Bitmap Dim g As Graphics Dim SizeF As SizeF Dim bmp As Bitmap bmp = New Bitmap(1, 1) g = Graphics.FromImage(bmp) SizeF = g.MeasureString(Text, Font) bmp = New Bitmap(CInt(SizeF.Width), CInt(SizeF.Height)) g = Graphics.FromImage(bmp) g.Clear(BackColor) g.DrawString( _ Text, Font, _ New SolidBrush(ForeColor), 0, 0 _ ) Return bmp End Function function call: Dim bmp As Bitmap bmp = CreateBitmapFromString( _ "This is a text", New Font("Arial", 10), _ Color.Black, Color.White _ ) Armin
|
Sun, 12 Dec 2004 19:22:23 GMT |
|
 |
fix #8 / 8
|
 Draw a string onto a bitmap object
Well thanks very much! --
Quote:
> > If I just pass the string but not the textbox to the function, > > how should this line be replaced? (g = Textbox.CreateGraphics()) > > Thanks for your kind help! > Maybe there's a better solution, but...: > (there's no reference font and color, so pass them to the function) > Private Function CreateBitmapFromString( _ > ByVal Text As String, _ > ByVal Font As Font, _ > ByVal ForeColor As Color, _ > ByVal BackColor As Color) _ > As Bitmap > Dim g As Graphics > Dim SizeF As SizeF > Dim bmp As Bitmap > bmp = New Bitmap(1, 1) > g = Graphics.FromImage(bmp) > SizeF = g.MeasureString(Text, Font) > bmp = New Bitmap(CInt(SizeF.Width), CInt(SizeF.Height)) > g = Graphics.FromImage(bmp) > g.Clear(BackColor) > g.DrawString( _ > Text, Font, _ > New SolidBrush(ForeColor), 0, 0 _ > ) > Return bmp > End Function > function call: > Dim bmp As Bitmap > bmp = CreateBitmapFromString( _ > "This is a text", New Font("Arial", 10), _ > Color.Black, Color.White _ > ) > Armin
|
Sun, 12 Dec 2004 21:34:16 GMT |
|
|
|