#include <stdio.h>
#include <Windows.h>
int main(void)
{
BITMAP bmp;
FILE *fp;
HBITMAP hbmp;
HDC hdc, hdcMem;
int x, y, i;
unsigned char dat;
hbmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), TEXT("img.bmp"), IMAGE_BITMAP, (int)NULL, (int)NULL, LR_LOADFROMFILE);
if (hbmp == NULL)
{
puts("打开位图失败");
return 0;
}
GetObject(hbmp, sizeof(bmp), &bmp);
if (bmp.bmWidth == 84 && bmp.bmHeight == 48)
{
hdc = GetDC(NULL);
hdcMem = CreateCompatibleDC(hdc);
ReleaseDC(NULL, hdc);
SelectObject(hdcMem, hbmp);
fopen_s(&fp, "data.c", "w");
if (fp != NULL)
{
fputs("const unsigned char img[][84] = {\n", fp);
for (y = 0; y < 6; y++)
{
fputs("\t{", fp);
for (x = 0; x < 84; x++)
{
dat = 0;
for (i = 0; i < 8; i++)
{
dat >>= 1;
if (GetPixel(hdcMem, x, y * 8 + i) < 0x000010)
dat |= 0x80;
}
if (dat == 0)
fputs("0x00", fp);
else
fprintf(fp, "%#04x", dat);
if (x != 83)
fputs(", ", fp);
}
if (y == 5)
fputs("}\n", fp);
else
fputs("},\n", fp);
}
fputs("};", fp);
fclose(fp);
puts("取码成功!");
}
else
puts("打开c文件失败!");
DeleteDC(hdcMem);
}
else
puts("图像的尺寸必须为84x48!");
DeleteObject(hbmp);
return 0;
}