C++ GDIPlus Bitmap lockbits 结果 WrongState(8)

问题描述

我正在尝试正确。两个 BitmaP* 对象之间的不同区域。当我传递 2 个位图*时,它可以为第一帧运行锁定位,但不能为第二个位图运行。

 Rect GetRecDifference(BitmaP* currentFrame,BitmaP* prevIoUsFrame) {
if (prevIoUsFrame == NULL) {
    prevIoUsFrame = currentFrame;

}
else {
    return Rect(0,0);
}

BitmapData* bd1 = new BitmapData;
Rect rect1(0,currentFrame->GetWidth(),currentFrame->GetHeight());
currentFrame->LockBits(&rect1,ImageLockModeRead,PixelFormat32bppARGB,bd1);

BitmapData* bd2 = new BitmapData;
Rect rect2(0,prevIoUsFrame->GetWidth(),prevIoUsFrame->GetHeight());
prevIoUsFrame->LockBits(&rect2,bd2);

它可以运行第一个 (bd1*) 加载状态正常,最后一个结果显示正常。但是当涉及到 bd2 时,它的状态显示为 WrongState(8)。

这是因为我将当前指针复制到前一个指针吗?错误状态错误的原因是什么?我需要清除内存中的某些部分吗?

解决方法

问题是你试图锁定同一个图像两次,这个

previousFrame = currentFrame;

意味着你的两个指针都指向同一个图像。

相反,您需要一个方案,将两个图像同时保存在内存中。类似下面的内容

Bitmap* current = NULL;
Bitmap* previous = NULL;
while (something)
{
    current = getNextImage(); // get the new image
    if (current && previous)
    {
        // process current and previous images
        ...
    }
    delete previous; // delete the previous image,not needed anymore
    previous = current; // save the current image as the previous
}
delete previous; // one image left over,delete it as well

这不是唯一的方法,但希望你能明白。