I implemented the simplest screen capture functionality in C#, but I encountered a System.AccessViolationException
error.
System: Windows 11
zed sdk:5.0
cuda 12.8
.net: .net 8.0
Can you help me?
static void Main()
{
RuntimeParameters ZedRuntimeParams = new RuntimeParameters();
Camera zed = new Camera(0);
InitParameters initParams = new InitParameters();
initParams.resolution = RESOLUTION.HD720;
initParams.cameraFPS = 30;
ERROR_CODE err = zed.Open(ref initParams);
if (err != ERROR_CODE.SUCCESS)
{
Console.WriteLine("open fail: " + err);
return;
}
sl.Mat image = new sl.Mat();
image.Create((uint)zed.ImageWidth, (uint)zed.ImageHeight, MAT_TYPE.MAT_8U_C4, MEM.CPU);
while (true)
{
if (zed.Grab(ref ZedRuntimeParams) == ERROR_CODE.SUCCESS)
{
zed.RetrieveImage(image, VIEW.LEFT); // here: System.AccessViolationException
Console.WriteLine($"RetrieveImage success");
}
else
{
Console.WriteLine("RetrieveImage fail");
}
}
// 关闭相机
zed.Close();
}
error:
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
at sl.Camera.dllz_retrieve_image(Int32, IntPtr, Int32, Int32, Int32, Int32, Int32)
at sl.Camera.RetrieveImage(sl.Mat, sl.VIEW, sl.MEM, sl.Resolution)
at test6.Program.Main()