Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

Inconsistent results with Marshal.ReadInt32.

$
0
0
The title may be confusing. Let me explain. I have the following method:

vb.net Code:
  1. Sub SpeedStitchTest()
  2.         Drawn = False
  3.  
  4.         bmpData = bmpMap.LockBits(New Rectangle(0, 0, bmpMap.Width, bmpMap.Height), Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppPArgb)
  5.  
  6.         Dim data As BitmapData = bmpImage.LockBits(New Rectangle(0, 0, bmpImage.Width, bmpImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb)
  7.         Dim AlphaInt As Int32 = Alpha.ToArgb
  8.         Dim ImageColorInt As Int32 = 0
  9.         Dim MapColorInt As Int32 = 0
  10.  
  11.         Dim ShortCircuit As Boolean = False
  12.  
  13.         Dim sw As Stopwatch = Stopwatch.StartNew
  14.  
  15.         For CanvasY = 0 To bmpMap.Height - bmpImage.Height
  16.             For CanvasX = 0 To bmpMap.Width - bmpImage.Width
  17.  
  18.                 If Drawn Then
  19.                     Exit For
  20.                 End If
  21.  
  22.                 MatchCount = 0
  23.                 PixelCount = bmpImage.Width * bmpImage.Height
  24.  
  25.                 For nY = 0 To bmpImage.Height - 1
  26.                     If ShortCircuit Then
  27.                         ShortCircuit = False
  28.                         Exit For
  29.                     End If
  30.  
  31.                     For nX = 0 To bmpImage.Width - 1
  32.  
  33.                         ' Read image pixel.
  34.                         ImageColorInt = Marshal.ReadInt32(data.Scan0, (data.Stride * nY) + (4 * nX))
  35.  
  36.                         ' Discount alpha pixels.
  37.                         If ImageColorInt = AlphaInt Then
  38.                             PixelCount -= 1
  39.                         Else
  40.                             ' Read map pixel.
  41.                             MapColorInt = Marshal.ReadInt32(bmpData.Scan0, (bmpData.Stride * (CanvasY + nY)) + ((CanvasX + nX) * 4))
  42.  
  43.                             ' Discount alpha pixels.
  44.                             If MapColorInt = AlphaInt Then
  45.                                 PixelCount -= 1
  46.                             Else
  47.                                 ' If the pixels match, increase the match count.
  48.                                 If ImageColorInt = MapColorInt Then
  49.                                     MatchCount += 1
  50.                                 Else
  51.                                     ShortCircuit = True
  52.                                 End If
  53.                             End If
  54.                         End If
  55.  
  56.                     Next
  57.                 Next
  58.  
  59.                 ' Check the match %.
  60.                 HighestMatch = Math.Max(HighestMatch, (MatchCount / PixelCount) * 100)
  61.             Next
  62.         Next
  63.  
  64.         MessageBox.Show(CStr(sw.ElapsedMilliseconds))
  65.         MessageBox.Show(CStr(HighestMatch))
  66.  
  67.         HighestMatch = 0
  68.         bmpImage.UnlockBits(data)
  69.         bmpMap.UnlockBits(bmpData)
  70.  
  71.     End Sub

Both images I use (bmpMap and bmpImage) are from the exact same file (PNG file, 960 x 976). When the method runs for the first time, the match percent is 100, because they are the same image. However, when it runs a second time, immediately after the first time, I get 9.43 (rounded) as a match percent. I am unsure why. To my knowledge, nothing is being changed and the method is running twice, back to back without anything running between. The pixel it gets to is (537, 91), if that matters.

Any ideas what may be happening?

Viewing all articles
Browse latest Browse all 27329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>