mickn66
Well-known member
- Joined
- Nov 2, 2002
- Messages
- 53
I have a program that is supposed to take a 300 by 200 pixel image and examine it one 5 by 5 block at a time. However, it is extremely slow. The following is the code that does the looping (spotsize is 5):
Dim x, y As Integer
For y = 0 To pct1.Height - spotsize Step spotsize
For x = 0 To pct1.Width - spotsize Step spotsize
spot.X = x
spot.Y = y
DrawSpot()
Next x
Next y
MsgBox("done")
"DrawSpot" is the function that analyzes each spot and thats where the trouble is, because without it, the above code takes about 1 second to run. Drawspot does 3 things. It draws a red rectangle around the spot in pct1, it draws the spot onto pct2, which is 50x50 and set to strectchimage mode, thereby showing a close up of spot, and it analyzes the brightness of spot (not in this order).
Here is the code for DrawSpot():
Private Sub DrawSpot()
Dim biggy As New Rectangle(0, 0, 50, 50) rectangle for pct2
Dim g2 As Graphics = pct2.CreateGraphics to draw on pct2
Dim bitty As New Bitmap(pct1.Image) source image of pct1
Dim g As Graphics = Graphics.FromHwnd(pct1.Handle) to draw on pct1
g2.DrawImage(bitty, biggy, spot, GraphicsUnit.Pixel)
calculate brightness of spot before drawing rectangle
Dim x, y As Integer
Dim aColor As Color
Dim total As Double = 0
Dim counter As Integer = 0
For x = spot.Left To spot.Right
For y = spot.Top To spot.Bottom
aColor = bitty.GetPixel(x, y)
total = total + aColor.GetBrightness
counter = counter + 1
Next y
Next x
lblBrightness.Text = CStr(total / counter)
draw the rectangle on Spot on pct1
Dim myPen As New Pen(Color:=Color.Red, Width:=2)
g.DrawRectangle(myPen, spot)
End Sub
I dont understand why it slows it down as much as it does. Have I done something horribly stupid? Is there a faster way? Thanks!
Mick
Dim x, y As Integer
For y = 0 To pct1.Height - spotsize Step spotsize
For x = 0 To pct1.Width - spotsize Step spotsize
spot.X = x
spot.Y = y
DrawSpot()
Next x
Next y
MsgBox("done")
"DrawSpot" is the function that analyzes each spot and thats where the trouble is, because without it, the above code takes about 1 second to run. Drawspot does 3 things. It draws a red rectangle around the spot in pct1, it draws the spot onto pct2, which is 50x50 and set to strectchimage mode, thereby showing a close up of spot, and it analyzes the brightness of spot (not in this order).
Here is the code for DrawSpot():
Private Sub DrawSpot()
Dim biggy As New Rectangle(0, 0, 50, 50) rectangle for pct2
Dim g2 As Graphics = pct2.CreateGraphics to draw on pct2
Dim bitty As New Bitmap(pct1.Image) source image of pct1
Dim g As Graphics = Graphics.FromHwnd(pct1.Handle) to draw on pct1
g2.DrawImage(bitty, biggy, spot, GraphicsUnit.Pixel)
calculate brightness of spot before drawing rectangle
Dim x, y As Integer
Dim aColor As Color
Dim total As Double = 0
Dim counter As Integer = 0
For x = spot.Left To spot.Right
For y = spot.Top To spot.Bottom
aColor = bitty.GetPixel(x, y)
total = total + aColor.GetBrightness
counter = counter + 1
Next y
Next x
lblBrightness.Text = CStr(total / counter)
draw the rectangle on Spot on pct1
Dim myPen As New Pen(Color:=Color.Red, Width:=2)
g.DrawRectangle(myPen, spot)
End Sub
I dont understand why it slows it down as much as it does. Have I done something horribly stupid? Is there a faster way? Thanks!
Mick