C#でAForge.NetからWebカメラフレームをキャプチャしようとしています。残念ながら、_CurrentFrame.LockBitsでArgumentExceptionが発生します。ロックされたビットマップへのイベントの書き込みに問題があると思います!?UnlockBitsで「GDI+で一般的なエラーが発生しました」というメッセージが表示されることもあります。
public bool GetFrame(ref Draw.STexture Frame)
{
BitmapData bd = _CurrentFrame.LockBits(new Rectangle(0, 0, _CurrentFrame.Width, _CurrentFrame.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
byte[] data = new byte[4 * _CurrentFrame.Width * _CurrentFrame.Height];
Marshal.Copy(bd.Scan0, data, 0, data.Length);
//Do something with data here
_CurrentFrame.UnlockBits(bd);
_CurrentFrame.Dispose();
}
private void OnFrame(object sender, NewFrameEventArgs e)
{
if (_CurrentFrame != null)
_CurrentFrame.Dispose();
_CurrentFrame = (Bitmap)e.Frame.Clone();
}