Background: I am using OpenCV2 library in my MFC application and SetDIBitsToDevice to display each image. My problem is that following code works with 90% of image files but does not work with certain rotated images for the reason I don’t know. Whole description of the issue is here
So I need to find out why the function is returning 0 error code for the images and what error I am getting from the function.
According to MSDN
If zero scan lines are set (such as when dwHeight is 0) or the
function fails, the function returns zero.
So if the function returns zero, how can I find details of the error?
UPDATE: I tried to use GetLastError() as following. But actually message was “Operation completed successfully” that is not true, of course. I would appreciate any hint or advice to attack this issue.
BITMAPINFO* bmi = (BITMAPINFO* )buffer;
FillBitmapInfo(bmi,width,height,Bpp(pDoc->m_cvImage),0);
int result = SetDIBitsToDevice(pDC->GetSafeHdc(), 100, 100, width,
height, 0, 0, 0, height, pDoc->m_cvImage.data, bmi,
DIB_RGB_COLORS);
if(result==0) {
DWORD error = GetLastError();
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString text;
// Process any inserts in lpMsgBuf.
text= (LPCTSTR)lpMsgBuf;
// Free the buffer.
LocalFree( lpMsgBuf );
int nResult = AfxMessageBox(text, MB_OK);
}
Most GDI functions do not use
GetLastError()(some do, such asBitBlt()), and GDI does not define its own error codes other than0andGDI_ERROR. If a failure occurs, there is usually no way to determine what the actual error is. you just have to review your code with a fine tooth comb looking for logic mistakes.Since you are having issues with rotated images, I would start by looking at them first. They probably do not have the same dimensions that you are expecting, so they do not match the parameters you are passing to
SetDIBitsToDevice().