Redraw a Single Shape or Changed Shape Only Instead of All Shapes

raklali
236
Reputation points
Please see the following Code:-
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Parallel.ForEach(Segments.OfType(Of Segment),
Sub(segment)
Try
segment.DrawLine(e.Graphics)
Catch ex As Exception
End Try
End Sub
)
Parallel.ForEach(Ellipses.OfType(Of Ellipse),
Sub(ellipse)
Try
ellipse.DrawEllipse(e.Graphics)
Catch ex As Exception
End Try
End Sub
)
End Sub
Where the Segment
and Ellipse
are Classes to draw Lines and Oval Shapes, Segments
and Ellipses
are the ListOf
of the object instances each of which is identified by a Unique Name such as Segment1, Segment2, ..., Ellipse1, Ellipse2...
My question - Where PictureBox1
has several shapes such as Segment1, Segment2, ..., Ellipse1, Ellipse2 , Is it possible to Redraw only the Changed Shape identified by its unique Name, instead of Redrawing all Shapes? For example, Can only Segment1 or Ellipse2 be Redrawn?
Request a solution with Sample Code...
{count} votes
@Castorix31 , in the code of my question, all shapes are being Redrawn every time we call
PictureBox1.Refresh()
, Correct?Can we create and Call a
Sub
to redraw only one of the shapes?If you don't do a loop on all shapes and just call something like Segment1.DrawLine(e.Graphics), only this shape will be (re)drawn,
biut of course all others will be ignored and not drawn...
But I still need to Call
PictureBox1.Refresh()
. So the issue remains, How do we Draw a Specific Shape underPictureBox1_Paint()
?We will need to device a method to send some parameter to
PictureBox1_Paint()
to Redraw a specific Shape only.Everything which is inside Paint event (WM_PAINT) is drawn on the control/window
When there are many things to draw, that's why methods like Double Buffering or, better, Direct2D are used to draw everything only once : in this case 100, 1000, 10000 or more shapes are drawn as fast as just 1 shape (nearly as fast, just a few ms more to calculate new coordinates)
(like in the other thread where I posted a sample with 1000 animated shapes (circles) with no slow-down (VB/Direct2D_1000 animated circles)
because the 1000 circles are drawn at same time at monitor frequency (usually 60hz)
Oh ok, Then I need to find what is slowing my program. Thank you sir.
Sign in to comment