Is it possible to "cache" a Bimap for a PictureBox?

  • Thread starter Thread starter roger.breton
  • Start date Start date
R

roger.breton

Guest
I'm running this ugly and possibly very slow piece of code inside a PictureBox.Paint Event:

For j = Lower_Y To Higher_Y ' from -200 to +200
For i = Lower_X To Higher_X ' from -200 to + 200
x1 = i
y1 = j

a2 = Ref_a + x1 / GridSize
b2 = Ref_b + y1 / GridSize

Result = Function(Ref_L, Ref_a, Ref_b, Ref_L, a2, b2)

If Result < Tolerance Then
e.Graphics.FillEllipse(Color, x1 - 2, y1 - 2, 2, 2)
End If
Next
Next

I agree this is horrible code but it had the merit to allow me to test the method. Now that I know the method is valid, I need to make it work faster. That is why I was thinking, perhaps, instead of calling so many times the same FillEllipse routine, to draw minute circles on screen, couldn't I do the same thing on a Bitmap, instead, and, when I'm done "constructing" the Bitmap, to display the bitmap in the PictureBox using ONE instruction?

As it stands, the above loop executes a whopping 400 x 400 times or 160,000 times. Even on a fast Core i7 CPU, it still takes a second or so to execute. So, I'm thinking "poor user" who has to wail "all that time"! I tried to imagine different programming schemes to speed up the execution? Perhaps write this loop in C++? Or in unmanaged code?

But I sense I could avoid all those "FillEllipse" calls if I was to work of a Bitmap "offscreen" and at the end of the PictureBox.Paint Event, when I'm reading, just issue an instruction like PictureBox = Bitmap?

Is that possible?

Continue reading...
 
Back
Top