카메라의 라이브 이미지를 표시하는 C# 응용 프로그램을 만들고 있습니다. 다음 코드 단편에 직면 한 문제는 스레드에서 계속 실행되는이 함수를 실행할 때 Marshal.Copy에서 AccessViolationException이 발생한다는 것입니다. 그러나이 작업은 한 번 실행하면 성공적으로 실행됩니다 (단일 정적 이미지를 얻음). 나는 그것이 메모리 손상 문제와 관련이 있다고 생각한다. 이 문제를 해결하는 방법에 대한 아이디어 나 제안은 없습니까? 당신은 항상 비트 맵 버퍼에 바이트 myByteArray.Length의 수를 복사하려고처럼Marshal.Copy 메서드는 C# .NET에서 AccessViolationException을 throw합니다.
private Image ByteArrayToImage(byte[] myByteArray)
{
if (myByteArray != null)
{
MemoryStream ms = new MemoryStream(myByteArray);
int Height = 504;
int Width = 664;
Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(myByteArray, 0, bmpData.Scan0, myByteArray.Length);
bmp.UnlockBits(bmpData);
return bmp;
}
return null;
}
확실하지,하지만 당신은 'LockBits'주위에 잠금을 넣어보십시오 수 있으며, 'UnlockBits'. 이 문은 시스템 메모리의 비트 맵을 잠급니다. 그리고 예외는 보호 된 메모리에 대한 무효 액세스를 알려줍니다. – Savaratkar