Draw a string onto a bitmap object 
Author Message
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 8 post ] 

 Relevant Pages 

1. how to draw a simple bitmap onto a window

2. put drawing object inside another drawing object

3. Getting a bitmap onto a canvas in Word XP

4. drawing circles onto picture boxes

5. Drawing lines onto a frame

6. Drawing onto PictureBoxes

7. Copying a bitmap onto the clipboard

8. Copying a bitmap onto the clipboard

9. Plot bitmap onto a sphere

10. How can you draw onto Image.Picture ?

11. Drawing onto a PictureBox

12. how to draw text vertically onto picture box

 

 
Powered by phpBB® Forum Software