我需要使用c++ / cli将图像发送到字节数组中。该图像最初为Iplimage格式。

    int img_sz1 = img1->width * img1->height * img1->nChannels;
    array <Byte>^ hh1 = gcnew array<Byte> (img_sz1);
    Marshal::Copy( (IntPtr)img->imageData, hh1, 0, img_sz1 );

而且工作正常。

我添加了编码步骤以jpeg格式发送
    CvMat* buf1 = cvEncodeImage(".jpeg", img1, jpeg_params);
    img_sz1=buf1->width*buf1->height
    Marshal::Copy( (IntPtr)buf1, hh1, 0, img_sz1 );

现在它可以正常编译,但是在marshal:copy行给了我错误
 An unhandled exception of type 'System.AccessViolationException' occurred in
 mscorlib.dll. Additional information: Attempted to read or write protected memory.

任何帮助都非常感谢。

最佳答案

cvEncodeImage的返回是一个单行矩阵,其中包含编码的图像数据。您现在要复制的是结构本身,例如width字段,height字段等。我认为您需要从buf1->data复制。

10-08 05:06