|
-
Apr 3rd, 2007, 11:21 PM
#1
Thread Starter
Junior Member
How to "move" text on a PictureBox?
VB2005 Express.
I am using this code to draw text on a PictureBox:
Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.DrawString("Text",myFont, Brushes.Black, X, Y)
I would like to be able to drag the text on the picturebox until the location "looks good" then drop it there with the MouseUp event.
Is there a way to move the text after it has been drawn on the pb?
Can it be removed by "refreshing" the pb?
-
Apr 3rd, 2007, 11:34 PM
#2
Re: How to "move" text on a PictureBox?
You aren't drawing on the PictureBox. You're drawing on the Image displayed in the PictureBox. They are not the same thing. The first is not permanent while the second is. Also, drawing on the PictureBox has no effect whatsoever on the Image. It's like having a photo in a picture frame and drawing on the glass cover with a white-board marker.
To draw temporarily on a PictureBox you simply handle the Paint event and use the Graphics object provided. The next time the control is repainted your drawing is cleared and redrawn, hence you change it as much as you like. You simply Refresh the control to redraw.
-
Apr 4th, 2007, 03:35 PM
#3
Thread Starter
Junior Member
Re: How to "move" text on a PictureBox?
Thanks, JM.
I like your analogy about writing on the glass of the picture frame.
Now, how can I wipe the glass clean and write at a different place on the glass?
-
Apr 4th, 2007, 05:42 PM
#4
Re: How to "move" text on a PictureBox?
Like I said, every time the control gets repaint, i.e. every time its Paint event is raised, any previous drawing is lost and new drawing must be performed. That's whay you must always do your GDI+ drawing in the Paint event handler for controls. That way if the form ever gets repainted due to being minimised and restored or covered by another form and uncovered, your drawing will be restored.
If you want to change your drawing you edit your control variables and the tell the control to repaint. In the Paint event handler you read those control variables and draw accordingly. The easiest way to force a control to repaint is to call its Refresh method. That will redraw the entire control. As a result that method is often inefficient if only a small portion of the control has changed. In that case it can be more efficient to spend the time calculating the smallest region of the control that has changed, then call its Invalidate and Update methods. That will cause only the region you specify to Invalidate to be redrawn. It is often quicker to calculate that smallest region than it is to redraw the entire control.
-
Apr 5th, 2007, 02:37 PM
#5
Thread Starter
Junior Member
Re: How to "move" text on a PictureBox?
OK, i'm making some progress. Below are the relevant subs.
Private Sub picA_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picA.MouseMove
txtButton.Text = MouseButtons.Left
If e.Button = MouseButtons.Left Then
Me.picA.Invalidate()
End If
End Sub
Private Sub picA_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picA.Paint
Dim pt As Point = picA.PointToClient(Control.MousePosition)
e.Graphics.DrawString(txtA.Text, myFont, Brushes.Black, pt.X, pt.Y)
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
dlgOpenFile.Filter = "JPEG (*.jpg)|*.jpg"
If dlgOpenFile.ShowDialog = DialogResult.OK Then
picA.Image = System.Drawing.Image.FromFile(dlgOpenFile.FileName)
End If
End Sub
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
picA.Image.Save("C:\VB2005\TextOnPic\Test.jpg")
End Sub
I am amazed how easy this is when one sees how to do it.
But I still have a problem: When the last Sub executes, the String placed on PicA is not on the saved Test.jpg.
How do I "fix" the string on the Pic so it is saved with the Pic?
-
Apr 5th, 2007, 03:36 PM
#6
Re: How to "move" text on a PictureBox?
As I said in my first post:
drawing on the PictureBox has no effect whatsoever on the Image. It's like having a photo in a picture frame and drawing on the glass cover with a white-board marker.
If you want your drawing to affect the Image then you have to draw on the Image, which is what you were doing in the first place.
I've posted code in the VB.NET CodeBank forum that is a very basic drawing program. It allows you to draw lines on the PictureBox and then remove them as they are not permanent. It then allows you to "save" the lines by performing the same drawing on the Image itself. Take a look at the code and apply the principle to your own situation. It's basically the same thing except that you're drawing text instead of lines.
http://www.vbforums.com/showthread.php?t=426684
-
Apr 6th, 2007, 03:24 PM
#7
Thread Starter
Junior Member
Re: How to "move" text on a PictureBox?
Thank you, JM. Here is my working test code.
Code:
Dim FontSize As Integer = 12
Dim myFont As New Font("Arial", FontSize, FontStyle.Bold Or FontStyle.Italic)
Dim Loc As Point
Private Sub picA_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picA.MouseMove
txtButton.Text = MouseButtons.Left
If e.Button = MouseButtons.Left Then
Loc = e.Location
Me.picA.Invalidate()
End If
End Sub
Private Sub picA_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picA.Paint
Dim pt As Point = picA.PointToClient(Control.MousePosition)
e.Graphics.DrawString(txtA.Text, myFont, Brushes.Black, pt.X, pt.Y)
End Sub
Private Sub btnLock_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLock.Click
Dim g As Graphics = Graphics.FromImage(Me.picA.Image)
g.DrawString(txtA.Text, myFont, Brushes.Black, Loc.X, Loc.Y)
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
dlgOpenFile.Filter = "JPEG (*.jpg)|*.jpg"
If dlgOpenFile.ShowDialog = DialogResult.OK Then
picA.Image = System.Drawing.Image.FromFile(dlgOpenFile.FileName)
End If
End Sub
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
picA.Image.Save("C:\VB2005\TextOnPic\Test.jpg")
End Sub
Now I can put a person's name by each person in a group photo. As I place each name, I hit btnLock to "fix" the name in place.
This is far easier that doing it in VB6.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|