I've made a screen capture program, and I have a program to play back the captured clips (right now there's no sound). All of the data is stored like so:

Frames per second, as a Byte
Width of each bitmap, as an Integer
Height of each bitmap, as an Integer
All of the bitmaps' data (24 bits per pixel)

In the playback form, I have a function to read the next bitmap from the file, using the stored width and height:
vb.net Code:
  1. Private Sub LoadNextBitmap()
  2.         Try
  3.             If m_Deserializer.PeekChar() = -1 Then Exit Sub
  4.         Catch ex As Exception
  5.  
  6.         End Try
  7.         m_CurBitmap = New Bitmap(w, h, PixelFormat.Format24bppRgb)
  8.         Dim bpbmp As Integer = w * h * 3
  9.         Dim bdata As BitmapData = m_CurBitmap.LockBits(New Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  10.         ' ^ I've also tried ImageLockMode.ReadOnly, no success.
  11.         Dim b(bpbmp - 1) As Integer
  12.         For i As Integer = 0 To bpbmp - 1
  13.             b(i) = m_Deserializer.ReadByte()
  14.         Next
  15.         Marshal.Copy(b, 0, bdata.Scan0, bpbmp) '<-- error here
  16.         m_CurBitmap.UnlockBits(bdata)
  17.     End Sub
I get an error on the Marshal.Copy line: "AccessViolation exception was unhandled: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I suppose that this means that I'm writing too much data to the area in memory specified by the pointer, but I don't see how. The b() array seems to be in working order. Can anyone help? The file saves just fine.