System.AccessViolationException 使用 EmguCV 构建 Mat

System.AccessViolationException while construct Mat using EmguCV

我需要的是 Emgu.Cv.Mat。由于我有一个 Bitmap 我想将他的信息传递给这个 Mat 实例。

这是我的代码:

//the real file is not stored on the harddrive. this is for demonstration only.
Bitmap fileBmp = new Bitmap(@"C:\Users\my\Desktop\file1.PNG"); 

.

//generating an Image from it works pretty fine as well. Even displaying it works great.
Image<Bgr, Byte> image = new Image<Bgr, Byte>(fileBmp);

.

//But I'm not able to generate a Mat with it. Not using the Bitmap
Mat mat1 = new Mat(new int[] { fileBmp.Width, fileBmp.Height }, DepthType.Cv8S, fileBmp.GetHbitmap());

//... and not using the Image
Mat mat2 = new Mat(new int[] { fileBmp.Width, fileBmp.Height }, DepthType.Cv8S, image.Bitmap.GetHbitmap());

我一直收到

System.AccessViolationException

如果我阅读 mat1.Bitmap 属性 或 mat2.Bitmap 属性(所有其他属性看起来都很好)。 我的代码有什么问题?

关于如何使用 EmguCv 将 "graphic information" 传递给 Mat 有什么建议吗?

再说一次:由于位图未存储在硬盘上,因此无法使用 new Mat(@"C:\Users\my\Desktop\file1.PNG")

Image<Bgr, Byte> image = new Image<Bgr, Byte>(fileBmp);
Mat mat1 = image.Mat;

如果您使用的是 EmguCV 3.1,还要检查以确保您正在为 x64 编译