怎么按指定区域截图?
void CUserAction::DivBmp(char* caFileName, RECT* prect, int rcount)
{
//AfxMessageBox("--------------test--------------");
FILE* pf = fopen(caFileName, "rb");
if (pf == NULL)
{
AfxMessageBox("test----0");
return;
}
BITMAPFILEHEADER bm;
fread(&bm, 1, sizeof(BITMAPFILEHEADER), pf);
if (bm.bfType!=*(WORD*)"BM")
{
fclose(pf);
AfxMessageBox("test----1");
return;
}
BITMAPINFOHEADER bmif;
fread(&bmif, 1, sizeof(BITMAPINFOHEADER), pf);
int bmx = bmif.biWidth;
int bmy = bmif.biHeight>0?bmif.biHeight:(-bmif.biHeight);
if (bmif.biBitCount < 8 || bmif.biCompression != 0)
{
fclose(pf);
AfxMessageBox("test----2");
return;
}
RGBQUAD *pRgb = NULL;
if (bmif.biBitCount == 8) // 256色bmp
{
pRgb = (RGBQUAD*) new BYTE[bmif.biClrUsed * sizeof(RGBQUAD)];
fread(pRgb, bmif.biClrUsed * sizeof(RGBQUAD), 1, pf);
}
BOOL bAlpha = FALSE;
if (bmif.biBitCount == 32)
bAlpha = TRUE;// 带Alpha通道
DWORD dwRealWidth = (((bmx * bmif.biBitCount) + 31) >> 5) << 2;
int iBitSteps = bmif.biBitCount >> 3;
DWORD iSize = dwRealWidth * bmy;
BYTE* pBmpData = NULL;
int readlines = bmy;
int readszie = iSize;
int readtimes = bmy / readlines;
if (iSize > 128 * 1024 * 1024)
{
readlines = (128 * 1024 * 1024 / dwRealWidth);
readszie = readlines * dwRealWidth;
readtimes = bmy / readlines + 1;
}
DWORD* pDat = new DWORD[bmy * bmx];
pBmpData = new BYTE[readszie];
fseek(pf, bm.bfOffBits, SEEK_SET);
fread(pBmpData, 1, readszie, pf);
int iy,ix;
BYTE btPil;
WORD caTmp[3] = {255};
DWORD dwDataOff = 0;
int picy = 0;
for(int n = 0; n < readtimes; ++n)
{
for (iy = 0; iy < readlines; ++iy)
{
for (ix = 0; ix < bmx; ix++)
{
if (pRgb == NULL)
{
caTmp[0] = pBmpData[(iy * dwRealWidth + ix * iBitSteps) + 2];
caTmp[1] = pBmpData[(iy * dwRealWidth + ix * iBitSteps) + 1];
caTmp[2] = pBmpData[(iy * dwRealWidth + ix * iBitSteps)];
}
else
{
btPil = pBmpData[(iy * dwRealWidth + ix * iBitSteps)];
if (bmif.biClrUsed != 0)
{
caTmp[0] = pRgb[btPil].rgbRed;
caTmp[1] = pRgb[btPil].rgbGreen;
caTmp[2] = pRgb[btPil].rgbBlue;
}
else
{
caTmp[0] = caTmp[1] = caTmp[2] = btPil;
}
}
if (bmif.biHeight < 0) // 有时bmp图像的高会是负值,这时则不用颠倒
{
dwDataOff = (iy + picy) * bmx + ix;
}
else
{
dwDataOff = (bmy - (iy + picy) - 1) * bmx + ix;
}
if (bAlpha)
{
caTmp[3] = pBmpData[(iy * dwRealWidth + ix * iBitSteps) + 3];
} // ARGB
pDat[dwDataOff] = caTmp[3] << 24 | caTmp[0] << 16 | caTmp[1] << 8 | caTmp[2] ;
}
}
picy += readlines;
int realbits = fread(pBmpData, 1, readszie, pf);
if (realbits < readszie) readlines = realbits / dwRealWidth;
}
fclose(pf);
CString str_test_debug;
CString str_test_debug_y;
for (int i = 0; i < rcount; ++i)
{
//str_test_debug.Format("%d",i);
//AfxMessageBox(str_test_debug);
char fbuf[256];
sprintf(fbuf, "%d.bmp", i);
if( (pf = fopen(fbuf, "wb")) != NULL )
{
int nWidth = prect[i].right - prect[i].left;
int nHeight = prect[i].bottom - prect[i].top;
int nPitch = nWidth; // 保存文件头
BITMAPFILEHEADER FileHeader;
FileHeader.bfType = *(WORD*)"BM";
FileHeader.bfSize = nWidth * nHeight * 4 + 0x36;
FileHeader.bfReserved1 = 0;
FileHeader.bfReserved2 = 0;
FileHeader.bfOffBits = 0x36;
fwrite(&FileHeader, sizeof(BITMAPFILEHEADER), 1, pf); // 保存文件信息
BITMAPINFOHEADER Header;
Header.biSize = sizeof(BITMAPINFOHEADER); // 结构的大小
Header.biWidth = nWidth; // 宽
Header.biHeight = nHeight; // 高
Header.biPlanes = 1; // 固定
Header.biBitCount = 32; // 颜色数
Header.biCompression = BI_RGB; // 是否压缩
Header.biSizeImage = nWidth * nHeight * 4; // 图片的大小
Header.biXPelsPerMeter = 0;
Header.biYPelsPerMeter = 0;
Header.biClrUsed = 0;
Header.biClrImportant = 0;
fwrite(&Header, Header.biSize, 1, pf); // 写入具体内容(从下向上存放)
fseek(pf, 0x36, SEEK_SET);
for (int y = prect[i].top; y < prect[i].bottom; ++y)
{
// fwrite(pDat + (prect[i].bottom - 1 - y) * bmx, 1, (prect[i].right - prect[i].left) * 4, pf);
fwrite(pDat + (prect[i].bottom - 1 - y) * bmx, (prect[i].right - prect[i].left) * 4,1 , pf);
str_test_debug_y.Format("%d",prect[i].bottom - 1 - y);
//AfxMessageBox(str_test_debug_y);
}
fclose(pf);
}
//AfxMessageBox("test123456");
}
//AfxMessageBox("test");
delete pBmpData;
delete pDat;
}
----------------------------------
for (int y = prect[i].top; y < prect[i].bottom; ++y)
{
// fwrite(pDat + (prect[i].bottom - 1 - y) * bmx, 1, (prect[i].right - prect[i].left) * 4, pf);
fwrite(pDat + (prect[i].bottom - 1 - y) * bmx, (prect[i].right - prect[i].left) * 4,1 , pf);
str_test_debug_y.Format("%d",prect[i].bottom - 1 - y);
//AfxMessageBox(str_test_debug_y);
}
其中这里有点问题,请大师们指导一下。截出来的图不是我想要的。