c - 函数 memcpy() 正在关闭程序

标签 c opencv memcpy

当编译器遇到函数 memcpy() 时,程序停止工作并显示消息框“name_program has stopworking”。

这是函数:

IplImage* hBitmap2Ipl(HBITMAP hBmp)
{
    BITMAP bmp;
    ::GetObject(hBmp,sizeof(BITMAP),&bmp);
    int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;

    // get depth color bitmap or grayscale
    int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;

    IplImage* img = cvCreateImageHeader( cvSize(bmp.bmWidth, bmp.bmHeight), depth,
        nChannels );

    // Copy bitmap data to IplImage
    img->imageData = (char*)malloc(bmp.bmHeight*bmp.bmWidth*nChannels*sizeof(char));

    memcpy(img->imageData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*nChannels);

    return img;
}

但这非常有效:

int a[10];
int b[10];
memcpy(a, b, 10);

最佳答案

GetObject 不会返回 bmBits 成员的有效指针 - 它仅填充结构的其他成员。

您需要使用类似 GetDIBits 的内容相反。

关于c - 函数 memcpy() 正在关闭程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26984609/

相关文章:

c++ - 结构的一部分的 memcpy

c - 如何创建一个新进程并使用共享内存与之通信

Raspbian 上的 Python - "TypeError: ' numpy.int3 2' object is not iterable'“

使用内存映射复制文件

opencv - 使用 OpenCV 检测正方形

php - 在 Windows 下安装 OpenCV 作为 php 扩展

c++ - 无法使用 memcpy 写入内存,但可以使用 WriteProcessMemory

c - 统计结构段错误

c - 具有特定地址的指针声明

收听 unix 套接字时检查发件人