Image Flicker

R

Rick

I've created a simple control that loads an image, and allows a user
to "Pan" this image. If the image is too large for the screen, the
user is able to drag the image around the screen in order to see the
parts of the image otherwise off the screen. I'm getting an
incredible amount of flicker in the image when repainting it and I'd
love for some to look over the code below and see if there's something
I can do eliminate the flicker.

Rick

Public Class PanningImage
Private m_Image As Bitmap
Private m_LastPoint As Point
Private m_StartPoint As Point
Private m_Current As Point

Public Property Image() As Bitmap
Get
Return m_Image
End Get
Set(ByVal value As Bitmap)
m_Image = value
m_Current.X = 0
m_Current.Y = 0
Me.Invalidate()
End Set
End Property

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
m_Image = New Bitmap(20, 20)
m_LastPoint = New Point(0, 0)
m_Current = New Point(0, 0)
m_StartPoint = New Point(0, 0)
End Sub


Protected Overrides Sub OnPaint(ByVal pe As
System.Windows.Forms.PaintEventArgs)
Dim rect As New Rectangle
Dim left As Integer
Dim top As Integer

top = m_Current.Y
left = m_Current.X

pe.Graphics.Clear(Me.BackColor)
pe.Graphics.DrawImage(m_Image, left, top)


End Sub


Protected Overrides Sub OnPaintBackground(ByVal e As
System.Windows.Forms.PaintEventArgs)

End Sub


Private Sub PanningImage_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
m_StartPoint.X = e.X
m_StartPoint.Y = e.Y
m_LastPoint.X = m_Current.X
m_LastPoint.Y = m_Current.Y
End Sub


Private Sub PanningImage_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then

Dim xDistance As Integer = m_StartPoint.X - e.X
Dim yDistance As Integer = m_StartPoint.Y - e.Y
Me.m_Current.X = m_LastPoint.X - xDistance
Me.m_Current.Y = m_LastPoint.Y - yDistance

If Math.Abs(xDistance) > 3 And Math.Abs(yDistance) > 3 Then
Me.Invalidate()
End If
End If
End Sub
End Class
 
M

Minerva Jones

I've created a simple control that loads an image, and allows a user
to "Pan" this image. If the image is too large for the screen, the
user is able to drag the image around the screen in order to see the
parts of the image otherwise off the screen. I'm getting an
incredible amount of flicker in the image when repainting it and I'd
love for some to look over the code below and see if there's something
I can do eliminate the flicker.

Use offscreen buffering, or double buffering as it's often called.

I've amended your OnPaint, but I don't do VB, so syntactically it'll
be wrong, but you'll get the idea:


Protected Overrides Sub OnPaint(ByVal pe As
System.Windows.Forms.PaintEventArgs)

Dim rect As New Rectangle
Dim left As Integer
Dim top As Integer

top = m_Current.Y
left = m_Current.X

Dim bmpOffscreen As New Bitmap (ClientSize.Width,
ClientSize.Height)
Dim gfxOffscreen As Graphics.FromImage (bmpOffscreen)

// Do all drawing to the offscreen version
gfxOffscreen.Clear(Me.BackColor)
gfxOffscreen.DrawImage(m_Image, left, top)

// Then at the last step, copy the offscreen to the onscreen
// in one go. Therefore the intermediate steps aren't seen
pe.Graphics.DrawImage(bmpOffscreen, 0, 0)

// Tidy up these properly
gfxOffscreen.Dispose()
bmpOffscreen.Dispose()

End Sub
 
A

Andrew Simpson

hi,

i am interested in this code. could you post it for me please?

Thanks



Minerva Jones wrote:

Re: Image Flicker
18-Jul-07

wrote

Use offscreen buffering, or double buffering as it is often called

I have amended your OnPaint, but I do not do VB, so syntactically it wil
be wrong, but you will get the idea

Protected

Previous Posts In This Thread:

Image Flicker
I have created a simple control that loads an image, and allows a use
to "Pan" this image. If the image is too large for the screen, th
user is able to drag the image around the screen in order to

Re: Image Flicker
wrote

Use offscreen buffering, or double buffering as it is often called

I have amended your OnPaint, but I do not do VB, so syntactically it wil
be wrong, but you will get the idea

Protected

EggHeadCafe - Software Developer Portal of Choice
View Status and Control Services in ASP.NET
http://www.eggheadcafe.com/tutorial...f-ffb4e241ea36/view-status-and-control-s.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top