|
RE: Stubborn font
|
Rod Stephens
|
Oct 28, 2008 09:06 PST
|
I'm not completely sure why you're getting this but it goes away if you clear the Graphics object before drawing.
And you don't really need to use CreateGraphics. That line just draws the same text again on a new Graphics object. All it does for this example is force a redraw.
Try this code in the KeyPress event handler:
If e.KeyChar = Chr(13) Then
e.Handled = True
ImageBuffer.Clear(Color.White)
ImageBuffer.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, PictureBox1.Width / 2, 100)
PictureBox1.Invalidate()
End If
It seems like what you really want is a persistent bitmap to display the image. It's a bit easier if you assign the bitmap to the PictureBox's Image property. Then you don't need the Paint event handler.
Dim Image As Bitmap
Dim ImageBuffer As Graphics
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
ImageBuffer = Graphics.FromImage(Image)
PictureBox1.Image = Image
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
e.Handled = True
ImageBuffer.Clear(Color.White)
ImageBuffer.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, PictureBox1.Width / 2, 100)
PictureBox1.Invalidate()
End If
End Sub
Or if you don't need to draw anything in other places, you can create the bitmap right in the KeyPress event handler. That way you don't have those module-level variables cluttering things up.
If e.KeyChar = Chr(13) Then
e.Handled = True
Dim bm As Image = New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height)
Dim gr As Graphics = Graphics.FromImage(bm)
gr.Clear(Color.White)
gr.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, PictureBox1.Width / 2, 100)
PictureBox1.Image = bm
End If
HTH,
Rod
Rod Stephens
mstep-@msn.com
To: VBHel-@topica.com
From: youce-@hotmail.com
Subject: Stubborn font
Date: Tue, 28 Oct 2008 14:31:42 +0000
Public Class Form1
Dim Image As Bitmap
Dim ImageBuffer As Graphics
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
ImageBuffer = Graphics.FromImage(Image)
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(Image, 0, 0)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
e.Handled = True
ImageBuffer.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, PictureBox1.Width / 2, 100)
PictureBox1.CreateGraphics.DrawImage(Image, 0, 0)
End If
End Sub
End Class
Please if someone can try the above code and tell me if the displayed text on the picture box has the same font as in the text box. At my end its kind of bold, I can't figure out the problem but I doubt my system.
|
|
 |
|