一般调用API函数的问题我都是查看msdn的参数对照程序有没有调用错误。然后根据msdn上的各类参数都试一下,再运行程序看看结果,或者用调试器调试一下参数有没有传输正确等方法来测试。比如你这个问题,原因我已经知道了,是因为你调用的GetDIBits函数的最后一个参数用错了。你的最后一个参数是DIB_RGB_COLORS,而我查找过MSDN,从MSDN上看到这么一句话。
BITMAPINFO
The BITMAPINFO structure defines the dimensions and color information for a DIB.
typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO, *PBITMAPINFO;
Members
bmiHeader
Specifies a BITMAPINFOHEADER structure that contains information about the dimensions of color format.
.
bmiColors
The bmiColors member contains one of the following:
An array of RGBQUAD. The elements of the array that make up the color table.
An array of 16-bit unsigned integers that specifies indexes into the currently realized logical palette. This use of bmiColors is allowed for functions that use DIBs. When bmiColors elements contain indexes to a realized logical palette, they must also call the following bitmap functions:
CreateDIBitmap
CreateDIBPatternBrush
CreateDIBSection
The iUsage parameter of CreateDIBSection must be set to DIB_PAL_COLORS.
The number of entries in the array depends on the values of the biBitCount and biClrUsed members of the BITMAPINFOHEADER structure.
The colors in the bmiColors table appear in order of importance. For more information, see the Remarks section.
感谢楼上的大侠,不过我刚才也查了很久MSDN,下面是16和24的说明:
16 The bitmap has a maximum of 2^16 colors. If the biCompression member of the BITMAPINFOHEADER is BI_RGB, the bmiColors member of BITMAPINFO is NULL. Each WORD in the bitmap array represents a single pixel. The relative intensities of red, green, and blue are represented with five bits for each color component. The value for blue is in the least significant five bits, followed by five bits each for green and red. The most significant bit is not used. The bmiColors color table is used for optimizing colors used on palette-based devices, and must contain the number of entries specified by the biClrUsed member of the BITMAPINFOHEADER.
If the biCompression member of the BITMAPINFOHEADER is BI_BITFIELDS, the bmiColors member contains three DWORD color masks that specify the red, green, and blue components, respectively, of each pixel. Each WORD in the bitmap array represents a single pixel.
Windows NT/Windows 2000/XP: When the biCompression member is BI_BITFIELDS, bits set in each DWORD mask must be contiguous and should not overlap the bits of another mask. All the bits in the pixel do not have to be used.
Windows 95/98/Me: When the biCompression member is BI_BITFIELDS, the system supports only the following 16bpp color masks: A 5-5-5 16-bit image, where the blue mask is 0x001F, the green mask is 0x03E0, and the red mask is 0x7C00; and a 5-6-5 16-bit image, where the blue mask is 0x001F, the green mask is 0x07E0, and the red mask is 0xF800.
24 The bitmap has a maximum of 2^24 colors, and the bmiColors member of BITMAPINFO is NULL. Each 3-byte triplet in the bitmap array represents the relative intensities of blue, green, and red, respectively, for a pixel. The bmiColors color table is used for optimizing colors used on palette-based devices, and must contain the number of entries specified by the biClrUsed member of the BITMAPINFOHEADER.
我发现我代码错是因为如果设置为24,the bmiColors member of BITMAPINFO is NULL,可能是应该没有bmiColors部分,而我的代码WriteFile(hFile,&bi,sizeof(bi),&num,NULL);
恰恰写入了bmiColors部分,所以可能造成了后面数据的错位,而改为16后,就需要有bmiColors部分,所以颜色数据就正常了,而把WriteFile(hFile,&bi,sizeof(bi),&num,NULL)中的写入数据大小改为sizeof(BITMAPINFOHEADER),用24为参数,则图片颜色同样正常。但是我之前几天迷茫的时候就试过使用DIB_PAL_COLORS参数,颜色也不对,可能我改的有些着急吧。不过很感谢小虾大侠的帮忙,很感动,能在快过年的情况下帮我看代码,辛苦了,祝您新年快乐。由于我一直都是一个人弄代码,所以出错时候感觉就像在迷宫里乱走,也许是很简单的问题都会浪费很长时间,希望高手别见笑,请多多指教。