|
-
Aug 5th, 2011, 07:46 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Image Flickering.
Dear All,
In my project I am using Graphics FillRectangle with solid brush to fill the rectangle.
But every time I re-size the rectangle in the picturebox paint method, the image starts flickering.
Can anybody suggest how can I get rid of it?
I also tried placing this below code in my form load event
Code:
Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)
Me.UpdateStyles()
But even this did not help.
Regards,
Susheelss
-
Aug 5th, 2011, 08:35 AM
#2
Junior Member
Re: Image Flickering.
Try setting DoubleBuffered to True on the form itself, rather than the controls.
-
Aug 6th, 2011, 08:29 AM
#3
Hyperactive Member
Re: Image Flickering.
Try this code:
VB.NET Code:
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H2000000
Return cp
End Get
End Property
Every time the form redraws it will ignore the areas occupied by child controls. It tends to fix most flicker issues. You will need to use Control.Refresh() if any of your controls have to be redrawn for any reason.
Hope this helps
-
Aug 6th, 2011, 10:20 AM
#4
Re: Image Flickering.
DoubleBuffering the picturebox should take care of it. The flicker is most likely caused by the fact that the control will normally be cleared before it is redrawn. This means that the control is drawn empty, then the control is drawn with the new specifications. Normally, that 'drawn empty' part isn't visible. It either shows for a fraction of a second too short for the eye to register, or it isn't shown at all. However, in certain circumstances, the erasure is on the screen long enough for it to register on the eye, and it registers as flickering.
In earlier versions, there were settings you could set to remove the erasure step in the drawing. However, that has all been rolled into the DoubleBuffer property for the control. If your drawing is too slow, even that won't handle it perfectly, but the problem would manifest as a jerky response rather than a flicker.
My usual boring signature: Nothing
 
-
Aug 6th, 2011, 09:04 PM
#5
Re: Image Flickering.
A PictureBox is double buffered by default, so you don't have to do anything about that. But there must be some other explanation for the flickering. Maybe you could post your code to show what's happening.
BB
-
Aug 7th, 2011, 12:16 AM
#6
Thread Starter
Addicted Member
Re: Image Flickering.
Here is my code bit:
Code:
Dim HardShutter_IMG As System.Drawing.Graphics = PBImage.CreateGraphics()
Dim Shutter_Pen As New Pen(Color.Yellow, 2)
Dim mySolidBrush As New SolidBrush(Color.Gray)
HardShutter_IMG.FillRectangle(mySolidBrush, 0, 0, 1024, Current_Shutter_Width)
HardShutter_IMG.FillRectangle(mySolidBrush, 0, (1024 - (Current_Shutter_Width)), 1024, (Current_Shutter_Width))
Every time the user drags the shutter the width of the Top and Bottom Shutters increase and the above code gets executed inside Picturebox(PBImage) Paint event.
Regards,
Susheelss
-
Aug 7th, 2011, 04:24 AM
#7
Re: Image Flickering.
 Originally Posted by Susheelss
Here is my code bit:
Code:
Dim HardShutter_IMG As System.Drawing.Graphics = PBImage.CreateGraphics()
Dim Shutter_Pen As New Pen(Color.Yellow, 2)
Dim mySolidBrush As New SolidBrush(Color.Gray)
HardShutter_IMG.FillRectangle(mySolidBrush, 0, 0, 1024, Current_Shutter_Width)
HardShutter_IMG.FillRectangle(mySolidBrush, 0, (1024 - (Current_Shutter_Width)), 1024, (Current_Shutter_Width))
Every time the user drags the shutter the width of the Top and Bottom Shutters increase and the above code gets executed inside Picturebox(PBImage) Paint event.
CreateGraphics invariably causes trouble, and it could be the cause in this case. Instead of using CreateGraphics, use the Graphics object (e.Graphics) provided in the Paint event. For example:
Code:
Private Sub PBImage_Paint(Sender As Object, e As PaintEventArgs) Handles PBImage.Paint
Dim mySolidBrush As New SolidBrush(Color.Gray)
e.Graphics.FillRectangle(mySolidBrush, 0, 0, 1024, Current_Shutter_Width)
e.Graphics.FillRectangle(mySolidBrush, 0, (1024 - (Current_Shutter_Width)), 1024, (Current_Shutter_Width))
'It's good practice to do dispose of pens, brushes etc. after use.
mySolidBrush.Dispose
End Sub
Then, to make sure the Paint event fires when the user moves the shutter, add
Code:
PB_Image.Invalidate()
You would put that as soon as you have calculated the new shutter width, for example at the end of the MouseMove event sub.
BB
-
Aug 9th, 2011, 11:07 PM
#8
Thread Starter
Addicted Member
Re: Image Flickering.
Thanks Mr.Boops Boops. Instead of creating Graphics object, I used e.Graphics and I think my flickering problem is solved. But now I am facing an another problem because of it.
Earlier I was rotating the whole graphics i.e. the four rectangles using GraphicsPath RotateTransform.RotateAt method to specify the rotation point.
But now with e.graphics I am not able to specify the point to rotate at. The code I used earlier is pasted below. Please go through it and let me know how to rotate e.graphics by specifying the rotateAt point
Code:
Dim RotationTransform As New Matrix(1, 0, 0, 1, 0, 0) ' rotation matrix
Dim TranslationTransform As New Matrix(1, 0, 0, 1, 0, 0) ' translation matrix
Dim RotationTransform As New Matrix(1, 0, 0, 1, 0, 0) ' rotation matrix
Dim TranslationTransform As New Matrix(1, 0, 0, 1, 0, 0) ' translation matrix
RotationTransform.RotateAt(NumericUpDown1.Value, New PointF(512, 512)) 'Convert Direction to degrees
gp.Transform(RotationTransform)
-
Aug 10th, 2011, 02:48 AM
#9
Re: Image Flickering.
You can use Matrix.RotateAt to rotate the Graphics in the Paint event like this:
Code:
Dim mtx As New Matrix
mtx.RotateAt(NumericUpDown1.Value, New Point(x, y))
e.Graphics.Transform = mtx
'paint rotated stuff here
That way you don't need RotateTransform or TranslateTransform to do the rotation (let alone twice ). If you want to rotate the graphics dynamically, don't forget to Invalidate the PictureBox in the NumericUpDown.ValueChanged event. By the way, you don't need to specify (1, 0, 0, 1, 0, 0) in the New Matrix code because those are the default values anyway.
BB
-
Aug 10th, 2011, 04:27 AM
#10
Thread Starter
Addicted Member
Re: Image Flickering.
Thanks Mr.Boops Boops. That worked exactly the way I wanted. I think the problem is now solved. Thanks for the help
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
|