|
RE: Stubborn font
|
Jamil Youcef
|
Oct 29, 2008 05:07 PST
|
Actually I tryed the Invalidate event before, but I did not like the fact that the paper is flickering especially that I have to invalidate the picture box ( actually it’s a user control ) in the TextChanged event.
I was trying to fill the rectangle where I want to print on the usercontrol with the usercontrol back color, but you gave me the idea to fill the rectangle in the imagebuffer instead .
ImageBuffer.FillRectangle(New SolidBrush(PictureBox1.BackColor), Rectangle)
ImageBuffer.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, Rectangle.X, Rectangle.Y)
PictureBox1.CreateGraphics.DrawImage(Image, 0, 0)
Seems I am liking the results.
From: mstep-@msn.comTo: youce-@hotmail.com; vbhel-@topica.comSubject: RE: Stubborn fontDate: Tue, 28 Oct 2008 10:06:27 -0600
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 IfIt 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 SubOr 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 IfHTH,RodRod Stephensm-@msn.com
To: VBHel-@topica.comFrom: youce-@hotmail.comSubject: Stubborn fontDate: Tue, 28 Oct 2008 14:31:42 +0000
Public Class Form1Dim Image As BitmapDim ImageBuffer As GraphicsPrivate Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadImage = New Bitmap(PictureBox1.Width, PictureBox1.Height)ImageBuffer = Graphics.FromImage(Image)End SubPrivate Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Painte.Graphics.DrawImage(Image, 0, 0)End SubPrivate Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPressIf e.KeyChar = Chr(13) Thene.Handled = TrueImageBuffer.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, PictureBox1.Width / 2, 100)PictureBox1.CreateGraphics.DrawImage(Image, 0, 0)End IfEnd SubEnd 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.
_________________________________________________________________
Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy!
http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us
|
|
 |
|