.net &&;:无法在PixelFormat类型上使用此间接寻址
我正在开发一个使用OpenCV 4.2的C++/CLI应用程序。在下面的代码中,我试图将.net &&;:无法在PixelFormat类型上使用此间接寻址,.net,opencv,c++-cli,.net,Opencv,C++ Cli,我正在开发一个使用OpenCV 4.2的C++/CLI应用程序。在下面的代码中,我试图将位图转换为自定义类型(struct),但它引发了以下错误: 错误C3699:“&&”:无法在类型“System::Drawing::Imaging::PixelFormat”上使用此间接寻址 这是我的C++代码: typedef struct { cv::Mat mat; System::Drawing::Imaging::PixelFormat pixelFormat; }CustomIm
位图
转换为自定义类型(struct),但它引发了以下错误:
错误C3699:“&&”:无法在类型“System::Drawing::Imaging::PixelFormat”上使用此间接寻址
这是我的C++代码:
typedef struct
{
cv::Mat mat;
System::Drawing::Imaging::PixelFormat pixelFormat;
}CustomImage;
CustomImage BitmapToCustomImage(System::Drawing::Bitmap^ bitmap)
{
CustomImage ob;
ob.pixelFormat = bitmap->PixelFormat;
System::Drawing::Rectangle blank = System::Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height);
System::Drawing::Imaging::BitmapData^ bmpdata = bitmap->LockBits(blank, System::Drawing::Imaging::ImageLockMode::ReadWrite, bitmap->PixelFormat);
int wb = ((bitmap->Width * 24 + 31) / 32) * 4;
cv::Mat cv_img(cv::Size(bitmap->Width, bitmap->Height), CV_8UC3, bmpdata->Scan0.ToPointer(), wb);
bitmap->UnlockBits(bmpdata);
ob.mat = cv_img;
return ob;
} // <------------- the error shows on this line when I compile
System::Drawing::Bitmap^ PerformSomeOperation(System::Drawing::Bitmap^ inputBitmap)
{
System::Drawing::Bitmap^ bitmap;
CustomImage ob = BitmapToCustomImage(inputBitmap);
// some stuff
// try to convert back to bitmap
return bitmap;
}
我做错了什么
除了发送位图句柄之外,还有没有其他方法可以将位图传递给C++/CLI应用程序,因为这经常会导致问题?您正在尝试将托管类型保存在本机结构中。C++/CLI不允许混合类型(它被提出但从未实现) 尝试将跟踪句柄存储为装箱像素格式
struct
{
cv::Mat mat;
gcroot<System::Drawing::Imaging::PixelFormat^> pixelFormat;
} CustomImage;
struct
{
cv::垫;
gcroot像素格式;
}顾客形象;
这有点奇怪,因为PixelFormat是一种值类型,通常不会与^
组合,但在这个特定场景中,强装箱类型正是您想要的
(您还需要解决图像缓冲区的生存期问题),
C++ +CLI不是C++。只是一种不幸命名的不同语言。就像JavaScript和Java一样,我没有投反对票。匿名向下投票是一些成员行使的特权(在会议结束后,至少有一个代表栏)。元意图是推动提高问题的质量和答案的质量。用c++
标记的问题(就像这一个)在短时间内吸引了很多评论者。不相关,但一旦BitmapToCustomImage
返回,当你试图访问图像时,这会爆炸。按照构建Mat
的方式,它共享像素缓冲区(即,只要使用Mat
实例,缓冲区就需要存在)。制作一个深度拷贝来避免这个问题。@Eljay:它真的不像“Javascript和Java”。C++ + CLI是C++的一组(不可移植)扩展,它不带任何东西。每一个有效的C++程序在C++/CLI中都是有效的。当我第二次运行函数时,它给出了AccessViolationException
。当我说问题中的经常导致问题时,我的意思是仅此而已。您能否提供一个如何创建深度副本的示例?
struct
{
cv::Mat mat;
gcroot<System::Drawing::Imaging::PixelFormat^> pixelFormat;
} CustomImage;