0

I have this code, which executes several times during runtime:

// Restore the bitmap from the copy
IWICBitmapLock *pLock = NULL;
UINT cbStride = 0;
hr = m_pWICBitmapCopy->GetSize(&uiWidth,&uiHeight);
WICRect rcLock = { 0, 0, uiWidth, uiHeight };
hr = m_pWICBitmapCopy->Lock(&rcLock, WICBitmapLockRead, &pLock);
hr = pLock->GetDataPointer(&cbBufferSize, &pv);
hr = pLock->GetStride(&cbStride);
m_pWICBitmap->Release();
hr = m_pWICImagingFactory->CreateBitmapFromMemory(uiWidth,uiHeight,GUID_WICPixelFormat32bppPBGRA,cbStride,cbBufferSize,pv,&m_pWICBitmap);
hr = pLock->Release();

uiWidth and uiHeight always different, the initial value is 3264 and 2448. Second time it could be e.g. 778 and 246. But method pLock->GetStride(&cbStride) always returns the same stride value (in my case 13056). It causes that CreateBitmapFromMemory() method creates a corrupted bitmap. Other variables are changing correctly depending on bitmap dimensions. It looks like pLock cached stride value somewhere.

In MSDN written that, the stride value is specific to the IWICBitmapLock, not the bitmap. However, I did release pLock every time. So what could be the cause of such behavior?

The second way I could use to do the same is like this, but I want to understand what’s wrong with the code above.

m_pWICBitmap->Release();
m_pWICImagingFactory->CreateBitmapFromSource(m_pWICBitmapCopy, WICBitmapCacheOnDemand, &m_pWICBitmap);
Craftsmann
  • 95
  • 1
  • 10
  • 1
    Releasing a bitmap *before* releasing the lock is odd, avoid that. – Hans Passant Feb 05 '15 at 15:15
  • I'm locking m_pWICBitmap**Copy** but releasing m_pWICBitmap – Craftsmann Feb 05 '15 at 15:25
  • Sure, but it is likely to avoid copying where possible. Just something I saw, easy for you to check. Nothing I can actually test, you surely need to post decent repro code to get an answer. – Hans Passant Feb 05 '15 at 15:46
  • MSDN doesn't say the stride will be different every time, just that it can, depending on the implementation. It could just be the stride that was passed to CreateBitmapFromMemory. So, I would guess the problem is elsewhere (I notice in this code you are not checking the value of hr, or the source bitmap's pixel format). – Esme Povirk Feb 06 '15 at 01:27
  • However, It could be different because dimensions of bitmap always different. e.g., it is impossible to have the stride over 13000 when picture width is 100 pixels. As regards checking values, I just omit this checking to make the code a bit easy to read, it always returns S_OK. – Craftsmann Feb 06 '15 at 08:29

0 Answers0