How to delete text from PictureBox?

my_lou

Well-known member
Joined
Nov 10, 2003
Messages
45
i have the following problem (excuse my ignorance if its a dumb question, i am new to GDI+): i have a PictureBox in a Windows Form and then on it i draw a graphic and some text like so:

Dim gStudyGuide As Graphics

Friend WithEvents SGBackCover_PictureBox As System.Windows.Forms.PictureBox
Me.SGBackCover_PictureBox = New System.Windows.Forms.PictureBox


SGBackCover_PictureBox

Me.SGBackCover_PictureBox.Dock = System.Windows.Forms.DockStyle.Fill
Me.SGBackCover_PictureBox.Image = CType(resources.GetObject("SGBackCover_PictureBox.Image"), System.Drawing.Image)
Me.SGBackCover_PictureBox.Location = New System.Drawing.Point(0, 0)
Me.SGBackCover_PictureBox.Name = "SGBackCover_PictureBox"
Me.SGBackCover_PictureBox.Size = New System.Drawing.Size(628, 734)
Me.SGBackCover_PictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.SGBackCover_PictureBox.TabIndex = 0
Me.SGBackCover_PictureBox.TabStop = False

Private Sub StudyGuideBack_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Variables initialization.
gStudyGuide = Graphics.FromImage(Me.SGBackCover_PictureBox.Image)
gStudyGuide.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

Call Me.DrawBarcode()
Call Me.DrawISBN()
End Sub End StudyGuideBack_Load

Private Sub DrawBarcode()
Variables declaration and initialization.
Dim orgBmp As New Bitmap(Me.BarcodeDir & "\" & Me.ISBN & ".bmp")
Dim newBmp As New Bitmap(orgBmp, orgBmp.Width, StudyGuide.DEFAULT_BARCODE_HEIGHT)

Draw barcode image over PictureBox Image.
gStudyGuide.DrawImage(newBmp, New Point(StudyGuide.BARCODE_X_COORDINATE, StudyGuide.BARCODE_Y_COORDINATE))
End Sub End DrawBarcode

Private Sub DrawISBN()
Variables declaration and initialization.
Font and brush variables.
Dim drawFont As New Font("Arial", 10)
Dim drawBrush As New SolidBrush(Color.Black)
Coordinates variables.
Dim xCoordinate As Single = 1730.0F
Dim yCoordinate As Single = 2760.0F
Initialize string format.
Dim drawFormat As New StringFormat
drawFormat.FormatFlags = StringFormatFlags.NoWrap

Draw ISBN string over PictureBox Image.
gStudyGuide.DrawString(Me.ISBN, drawFont, drawBrush, xCoordinate, yCoordinate, drawFormat)
End Sub End DrawISBN

so far so good. what i want to do now is be able to change the font of the text i just drew. so to accomplish this a have a FondDialog control in my form and as soon as the user selects new font id like to basically replace the text drawn above with the same text but in a different font.
the problem is i dont know how to delete text from a PictureBox. any ideas?
i looked into MSDN documentaion and i found the Graphics.Clear method, but it doesnt seem to be doing what i need.

of course i could always redraw everything in the PictureBox, but that would be...well lame. why redraw everything in it when i only need to change the text?

any advice would be greatly appreciated.
thanks.
 
Im new to GDI+ too but instead of deleting, is it possible to color the previous text white..make it invisible and just write over it?
 
Yes, you dont erase graphics... you merely draw over them.

To draw over it, just take the same drawing line that you have there, change the brushs color and then redraw the string. :)
Or, you can get a Brush from the Brushes collection:
Brushes.Green, Brushes.Blue, etc. :)
 
Back
Top