【发布时间】:2018-02-19 15:25:13
【问题描述】:
我有一个基于 opencv 的 dll,用 c++ 编写。我需要在 dll 中加载一个 cv::Mat 图像,然后从 C# UI 调用它。我在 WinForms 中工作过,但现在正在转向 WPF。
我在dll中暴露了以下函数:
__declspec(dllexport)uchar* GetBlankFrame(void)
{
cv::Mat img = cv::imread("D://data/TestPattern.jpg", 1);
static cv::Mat tmp;
cv::cvtColor(img, tmp, CV_BGRA2BGR);
return tmp.data;
}
在一个 winForms 应用程序中,我曾经使用它来创建位图:
[DllImport("tester.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetBlankFrame();
IntPtr frame = GetBlankFrame();
Bitmap blank = new Bitmap(600, 800, 3 * 600, System.Drawing.Imaging.PixelFormat.Format24bppRgb, GetBlankFrame());
这很好用。
现在,我正在尝试将此功能移至 WPF 应用程序。要显示图像,我似乎需要使用BitmapSource 而不是Bitmap。
我正在尝试:
IntPtr frame = GetBlankFrame();
BitmapSource srcFrame = Imaging.CreateBitmapSourceFromHBitmap(frame, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
但它给了我:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll
我哪里错了?
谢谢。
【问题讨论】: