How to Draw Text on an image? RRS feed

  • Question

  • Hi everyone,

    I'm stuck on a little project where I need to draw Text on an image in a picturebox.  I have a TreeView, 2 pictureboxes and a Textbox.
    The TreeView has a list of pictures in a selected directory, when I click on an image name it displays in the first picturebox (PB1), and I want the text in the TextBox1 to be written on the image and displayed in the second picturebox (PB2) on the fly (to show the user the attempted output).  I have only managed to do this with the following code which I have problems with:

    pb_Orig.Image = Image.FromFile(TreeView1.SelectedNode.Text)
    Dim Bmp As New Bitmap(TreeView1.SelectedNode.Text)
    Dim G As Graphics = Graphics.FromImage(Bmp)
    Textbox1.Text, FontDialog1.Font, Brushes.Black, 0, 0)
    "temp" & TreeView1.SelectedNode.Text)
    pb2.Image = Image.FromFile(
    "temp" & TreeView1.SelectedNode.Text)

    The problem is that for every image I click on, a file with "temp" & filename is created, which cannot be deleted until I exit the application (which becomes a problem since I only want to save the image that I choose).  I was wondering if there was another way to accomplish this task without creating temporary files, or if I can delete the file when I click on a different picturename in the Treeview.  I have tried deleting the picture but the application seems to lock the file until I quit it. 

    Any suggestions or help will be greatly appreciated...

    Saturday, April 8, 2006 12:51 PM


  • Open the image with a filestream.  I have a sample on the VB-Tips website which shows how to open an image with out locking it.
    Saturday, April 8, 2006 1:56 PM

All replies

  • Open the image with a filestream.  I have a sample on the VB-Tips website which shows how to open an image with out locking it.
    Saturday, April 8, 2006 1:56 PM
  • Hi Ken,

    Thanks very much for the input, but I still have a small problem.

    When I use the code that you gave me the picturebox displays the "c:\bliss.jpg" picture without any problems, but when I modify the code like below, I get no picture in picturebox 2.  Any ideas?

    Private Sub TV1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TV1.AfterSelect
    Dim picname = FBD1.SelectedPath & "\" & TV1.SelectedNode.Text
    pb1.Image = Image.FromFile(FBD1.SelectedPath &
    "\" & TV1.SelectedNode.Text)
    Dim fs As New System.IO.FileStream(picname.ToString, IO.FileMode.Open)
    Dim bm As New Bitmap(fs)
    pb2.Image =
    DirectCast(bm.Clone, Image)
    Catch ex As Exception
    End Try
    End Sub

    Because I want the first picturebox to display my original picture and the second to display the altered picture, so when I click on another filename in the treeview the picture changes in the picturebox1 (pb1) but there is no picture displayed in the picturebox2 (pb2) in the form. 


    I think I figured my problem out, since I'm using the same picture for both picturebox1 and 2, when I load the picture into picturebox1 with the image.fromfile it locks the file and picturebox2 cannot access the file, but when I change the code so both pictureboxes display the file from system.io.filestream the problem was solved...
    Thanks for the help again...

    Saturday, April 8, 2006 5:56 PM