Share via


How can I insert text into a PictureBox in VB 2008

Question

Thursday, August 27, 2009 12:05 AM

How can I insert text into a PictureBox in VB 2008.  I would like to add text to graphs which I have been able to generate, but I cannot find the equivalent method to use with text,

All replies (3)

Thursday, August 27, 2009 12:34 AM ✅Answered

your code is here :)

dim g as Graphics=pictureBox1.CreateGraphics()
g.DrawString("texto",New Font("Arial",12),Brushes.Black,x,y)

x and y is the location, Arial is the font and "texto" is the text that you want to write
Miguel Galarreta Ingeniero de Sistemas (Universidad Nacional de San Antonio Abad del Cusco) Cusco-Perú


Thursday, August 27, 2009 12:36 AM ✅Answered | 1 vote

How can I insert text into a PictureBox in VB 2008.  I would like to add text to graphs which I have been able to generate, but I cannot find the equivalent method to use with text,

Hi,

Use DrawString. Here is one of the pages on the documentation.>>

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawstring(VS.71).aspx

Public Class Form1

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        'Make PictureBox1 be black.>>
        e.Graphics.Clear(Color.Black)
        Dim StringToDraw As String = "Hi there!! :-)"
        Dim MyBrush As New SolidBrush(Color.LightBlue)
        Dim StringFont As New Font("Arial", 20)
        Dim PixelsAcross As Integer = 20
        Dim PixelsDown As Integer = 30
        e.Graphics.DrawString(StringToDraw, StringFont, MyBrush, PixelsAcross, PixelsDown)

    End Sub
End Class

Regards,

John


Thursday, August 27, 2009 12:52 AM ✅Answered | 1 vote

your code is here :)

dim g as Graphics=pictureBox1.CreateGraphics()

g.DrawString("texto",New Font("Arial",12),Brushes.Black,x,y)

x and y is the location, Arial is the font and "texto" is the text that you want to write


Miguel Galarreta Ingeniero de Sistemas (Universidad Nacional de San Antonio Abad del Cusco) Cusco-Perú

Hi Miguel,

If you do that the Graphics created can and often will disappear if you minimize
 and then maximize the FORM or if something moves in front of it temporarily.

Better to draw within the PAINT event as in my example above.

Try this with one button and one PictureBox then mimimize then maximize the FORM.>>

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim g As Graphics = PictureBox1.CreateGraphics
        Dim x As Integer = 20
        Dim y As Integer = 30
        g.DrawString("Hello.", New Font("Arial", 12), Brushes.Black, x, y)

    End Sub
End Class

Regards,

John