19,468
社区成员
发帖
与我相关
我的任务
分享
for (int x = 200; x < 260; x++)
{
for (int y = 200; y < 294; y++)
{
hdc=GetDC(hWnd);
COLORREF pixelColor = GetPixel(hdc,x,y);
if (pixelColor == RGB(190, 225, 245))
SetPixel(hdc, x, y, RGB(155,125,105));
}
}
ReleaseDC(NULL, hdc);
int ReadBmp(const char* szFileName, BITMAPINFOHEADER *bih, BYTE **Buffer)
{
FILE *file;
WORD bfh[7];
long dpixeladd;
if (NULL == (file = fopen(szFileName, "rb")))
{
return 0;
}
printf("%s\n", szFileName);
fread(&bfh, sizeof(WORD), 7, file);
if (bfh[0] != (WORD)(((WORD)'B') | ('M' << 8)))
{
fclose(file);
return 0;
}
BITMAPINFOHEADER mbin;
fread(&mbin, sizeof(BITMAPINFOHEADER), 1, file);
if (mbin.biBitCount < 24)
{
fclose(file);
return 0;
}
dpixeladd = mbin.biBitCount / 8;
long LineByteWidth = mbin.biWidth * (dpixeladd);
if ((LineByteWidth % 4) != 0)
LineByteWidth += 4 - (LineByteWidth % 4);
BYTE* mBuffer;
if ((mBuffer = (BYTE*)malloc(sizeof(BYTE)* LineByteWidth * mbin.biHeight)) != NULL)
{
*bih = mbin;
fread(mBuffer, LineByteWidth * mbin.biHeight, 1, file);
*Buffer = mBuffer;
fclose(file);
return 1;
}
fclose(file);
return 0;
}
int GetDIBColor(int X, int Y, BITMAPINFOHEADER bih, BYTE *Buffer, BYTE *r, BYTE *g, BYTE *b)
{
int dpixeladd;
BYTE *ptr;
if (X < 0 || X >= bih.biWidth || Y < 0 || Y >= bih.biHeight)
{
return 0;
}
dpixeladd = bih.biBitCount / 8;
long LineByteWidth = bih.biWidth * (dpixeladd);
ptr = Buffer + X * dpixeladd + (bih.biHeight - 1 - Y) * LineByteWidth;
*b = *ptr;
*g = *(ptr + 1);
*r = *(ptr + 2);
return 1;
}
int GetDIBColor(int X, int Y, BITMAPINFOHEADER bih, BYTE *Buffer, BYTE *rgb, int type)
{
int dpixeladd;
BYTE *ptr;
if (X < 0 || X >= bih.biWidth || Y < 0 || Y >= bih.biHeight)
{
return 0;
}
dpixeladd = bih.biBitCount / 8;
long LineByteWidth = bih.biWidth * (dpixeladd);
ptr = Buffer + X * dpixeladd + (bih.biHeight - 1 - Y) * LineByteWidth;
switch (type)
{
case 0: *rgb = *ptr;; break;
case 1: *rgb = *(ptr + 1);; break;
case 2: *rgb = *(ptr + 2);; break;
default:
break;
}
return 1;
}
int SetDIBColor(int X, int Y, BITMAPINFOHEADER bih, BYTE *Buffer, BYTE r, BYTE g, BYTE b)
{
int dpixeladd;
BYTE *ptr;
if (X < 0 || X >= bih.biWidth || Y < 0 || Y >= bih.biHeight)
{
return 0;
}
dpixeladd = bih.biBitCount / 8;
long LineByteWidth = bih.biWidth * (dpixeladd);
ptr = Buffer + X * dpixeladd + (bih.biHeight - 1 - Y) * LineByteWidth;
*ptr = r;
*(ptr + 1) = g;
*(ptr + 2) = b;
return 1;
}
int SetDIBColor_RGB(int X, int Y, BITMAPINFOHEADER bih, BYTE *Buffer, BYTE rgb, int type)
{
int dpixeladd;
BYTE *ptr;
if (X < 0 || X >= bih.biWidth || Y < 0 || Y >= bih.biHeight)
{
return 0;
}
dpixeladd = bih.biBitCount / 8;
long LineByteWidth = bih.biWidth * (dpixeladd);
ptr = Buffer + X * dpixeladd + (bih.biHeight - 1 - Y) * LineByteWidth;
switch (type)
{
case 0: *ptr = rgb; break;
case 1: *(ptr + 1) = rgb; break;
case 2: *(ptr + 2) = rgb; break;
default:
break;
}
return 1;
}
//创建位图文件
int SaveBitmap(const char* bmpPath, BYTE * pBuffer, int lWidth, int lHeight, int nByte)
{
// 生成bmp文件
HANDLE hf = CreateFileA(bmpPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL);
if (hf == INVALID_HANDLE_VALUE)
return 0;
// 写文件头
BITMAPFILEHEADER bfh;
memset(&bfh, 0, sizeof(bfh));
bfh.bfType = 'MB';
bfh.bfSize = sizeof(bfh) + lWidth * lHeight * nByte + sizeof(BITMAPINFOHEADER);
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
DWORD dwWritten = 0;
WriteFile(hf, &bfh, sizeof(bfh), &dwWritten, NULL);
// 写位图格式
BITMAPINFOHEADER bih;
memset(&bih, 0, sizeof(bih));
bih.biSize = sizeof(bih);
bih.biWidth = lWidth;
bih.biHeight = lHeight;// -lHeight;
bih.biPlanes = 1;
bih.biBitCount = nByte * 8;
WriteFile(hf, &bih, sizeof(bih), &dwWritten, NULL);
// 写位图数据
WriteFile(hf, pBuffer, lWidth*lHeight*nByte, &dwWritten, NULL);
CloseHandle(hf);
return 0;
}