المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : SetDIBitsToDevice problem



C++ Programming
12-10-2009, 06:17 AM
I have this code that loads a DIB bitmap from a file and passes it to SetDIBitsToDevice.

This code works:

FILE* f = fopen(".\\bitmap1.bmp", "rb");

vector buffer;
buffer.resize(_filelength(f->_file));
fread(&buffer[0], buffer.size(), 1, f);
BITMAPINFO* bminfo = (BITMAPINFO*) &buffer[14];
BITMAPFILEHEADER* bmh = (BITMAPFILEHEADER*) &buffer[0];
LPVOID bits = &buffer[bmh->bfOffBits];

SetDIBitsToDevice(
pDC->GetSafeHdc(), // handle to device context
0, // x-coordinate of upper-left corner of
// dest. rect.
0, // y-coordinate of upper-left corner of
// dest. rect.
bminfo->bmiHeader.biWidth, // source rectangle width
bminfo->bmiHeader.biHeight, // source rectangle height
0, // x-coordinate of lower-left corner of
// source rect.
0, // y-coordinate of lower-left corner of
// source rect.
0, // first scan line in array
bminfo->bmiHeader.biHeight, // number of scan lines
bits, // address of array with DIB bits
bminfo, // address of structure with bitmap info.
DIB_RGB_COLORS // RGB or palette indexes
);



But this doesn't. The only differences are in the source origins and width/height/number of scanlines. I'm trying to pass a smaller rectangle that starts at (100,100) and has the same lower-right corner (hence smaller width/height).

FILE* f = fopen(".\\bitmap1.bmp", "rb");

vector buffer;
buffer.resize(_filelength(f->_file));
fread(&buffer[0], buffer.size(), 1, f);
BITMAPINFO* bminfo = (BITMAPINFO*) &buffer[14];
BITMAPFILEHEADER* bmh = (BITMAPFILEHEADER*) &buffer[0];
LPVOID bits = &buffer[bmh->bfOffBits];

SetDIBitsToDevice(
pDC->GetSafeHdc(), // handle to device context
0, // x-coordinate of upper-left corner of
// dest. rect.
0, // y-coordinate of upper-left corner of
// dest. rect.
bminfo->bmiHeader.biWidth-100, // source rectangle width
bminfo->bmiHeader.biHeight-100, // source rectangle height
100, // x-coordinate of lower-left corner of
// source rect.
100, // y-coordinate of lower-left corner of
// source rect.
0, // first scan line in array
bminfo->bmiHeader.biHeight-100, // number of scan lines
bits, // address of array with DIB bits
bminfo, // address of structure with bitmap info.
DIB_RGB_COLORS // RGB or palette indexes
);


What can be the problem with the second code?

There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal