Results 1 to 10 of 10

Thread: [RESOLVED] Image Flickering.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2008
    Posts
    204

    Resolved [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

  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    28

    Re: Image Flickering.

    Try setting DoubleBuffered to True on the form itself, rather than the controls.

  3. #3
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Image Flickering.

    Try this code:
    VB.NET Code:
    1. Protected Overrides ReadOnly Property CreateParams() As CreateParams    
    2.     Get    
    3.         Dim cp As CreateParams = MyBase.CreateParams    
    4.         cp.ExStyle = cp.ExStyle Or &H2000000    
    5.         Return cp    
    6.     End Get    
    7. 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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2008
    Posts
    204

    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

  7. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Image Flickering.

    Quote Originally Posted by Susheelss View Post
    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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2008
    Posts
    204

    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)

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2008
    Posts
    204

    Resolved 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
  •  



Click Here to Expand Forum to Full Width