****首先感謝巨人的肩膀,讓小白能夠走出bitmap圖片的苦海。****
10多年的交口網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整交口建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“交口網(wǎng)站設(shè)計”,“交口網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
自己閑的沒事想通過c++ 的MFC來截屏并上傳服務(wù)器展示客戶端的當前界面。中間一路降妖伏魔....(此處省略2048個字節(jié)),發(fā)揚程序猿無私奉獻的優(yōu)良品質(zhì),分享源代碼來眾生同樂。
1.好戲在前頭,請在.h添加下面這些東東....
typedef struct _BMPINFO
{
int bmpWidth;//圖像的寬
int bmpHeight;//圖像的高
int biBitCount;//圖像類型,每像素位數(shù)
RGBQUAD pColorTable[256];//顏色表指針
unsigned char pBmpBuf[10240000];
}m_bmpinfo,*p_bmpinfo;
//獲取截屏
void getScreen(char* filename);
int readBmp(const char* bmpName, unsigned char* pBmpBuf,int& bmpWidth,int& bmpHeight,int& biBitCount,RGBQUAD *pColorTable);
int sendReadBmp(char* filename,p_bmpinfo bmpinfo);
int recvSaveBmp(char* filename,p_bmpinfo bmpinfo);
int saveBmp(const char* bmpName,unsigned char *pBmpBuf,int& bmpWidth,int& bmpHeight,int& biBitCount,RGBQUAD *pColorTable);
--------------------------累了吧,關(guān)了網(wǎng)頁休息會--------------------------------------
2.截屏接口,保存截屏(格式為.bmp)到指定路徑
in char* filenam:需要保存截屏的文件路徑
void getScreen(char* filename)
{
CDC *pDC;//屏幕DC
pDC = CDC::FromHandle(GetDC(NULL));//獲取當前整個屏幕DC
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//獲得顏色模式
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);
CDC memDC;//內(nèi)存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&memBitmap);//將memBitmap選入內(nèi)存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//復(fù)制屏幕圖像到內(nèi)存DC
//以下代碼保存memDC中的位圖到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//獲得位圖信息
FILE *fp = fopen(filename, "w+b");
BITMAPINFOHEADER bih = {0};//位圖信息頭
bih.biBitCount = bmp.bmBitsPixel;//每個像素字節(jié)大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//圖像數(shù)據(jù)大小
bih.biWidth = bmp.bmWidth;//寬度
BITMAPFILEHEADER bfh = {0};//位圖文件頭
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位圖數(shù)據(jù)的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件總的大小
bfh.bfType = (WORD)0x4d42;
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);//寫入位圖文件頭
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//寫入位圖信息頭
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申請內(nèi)存保存位圖數(shù)據(jù)
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//獲取位圖數(shù)據(jù)
fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//寫入位圖數(shù)據(jù)
delete [] p;
fclose(fp);
memDC.SelectObject(oldmemBitmap);
}
--------------------------累了吧,后面沒有了--------------------------------------
-----------------------------------------------------------------------------------
3.讀取指定位置BMP相關(guān)信息到內(nèi)存中
in const char* bmpName, BMP路徑
out unsigned char* pBmpBuf,BMP數(shù)據(jù)信息
out int& bmpWidth,BMP寬度
out int& bmpHeight,BMP高度
out int& biBitCount,BMP的像素位數(shù)
out RGBQUAD *pColorTable色彩版
int readBmp(const char* bmpName, unsigned char* pBmpBuf,int& bmpWidth,int& bmpHeight,int& biBitCount,RGBQUAD *pColorTable)
{
FILE *fp=fopen(bmpName,"rb");
if(fp==0)
{
printf("cannot open file");
return FALSE;
}
fseek(fp,sizeof(BITMAPFILEHEADER),0);
BITMAPINFOHEADER head;
fread(&head,sizeof(BITMAPINFOHEADER),1,fp);
bmpWidth = head.biWidth;
bmpHeight = head.biHeight;
biBitCount = head.biBitCount;
int lineByte = (bmpWidth *biBitCount/8+3)/4*4;//計算圖像每行像素所占的字節(jié)數(shù)
if(biBitCount == 8)
{
pColorTable = new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
fread(pBmpBuf,1,lineByte *bmpHeight,fp);
fclose(fp);
return TRUE;
}
3.1 便于上層調(diào)用和重構(gòu)提供一個封裝接口,將所有參數(shù)到一個結(jié)構(gòu)體中,可便于socket傳輸(我4不4很聰明,請叫我少先隊員)
in char* filename,BMP路徑
out p_bmpinfo bmpinfo BMP數(shù)據(jù)結(jié)構(gòu)體
int sendReadBmp(char* filename,p_bmpinfo bmpinfo)
{
if(readBmp(filename, bmpinfo->pBmpBuf, bmpinfo->bmpWidth, bmpinfo->bmpHeight, bmpinfo->biBitCount, bmpinfo->pColorTable))
{
return TRUE;
}
return FALSE;
}
--------------------累了吧,么有了------------------------------------------------------
--------------------累了吧,真的么有了----------------------------------------------------
--------------------真的,真的么有了----------------------------------------------------
4.將內(nèi)存中的BMP數(shù)據(jù)寫入到本地文件
in const char* bmpName, 本地需保存的BMP文件路徑
in unsigned char *pBmpBuf,read得到的BMP數(shù)據(jù)
in int& bmpWidth,BMP寬度
in int& bmpHeight,BMP高度
in int& biBitCount,BMP像素位數(shù)
in RGBQUAD *pColorTable調(diào)色板
int saveBmp(const char* bmpName,unsigned char *pBmpBuf,int& bmpWidth,int& bmpHeight,int& biBitCount,RGBQUAD *pColorTable)
{
if(!pBmpBuf)//imgBuf 待存盤的位圖數(shù)據(jù)
return 0;
int i,j;
//每行字節(jié)數(shù)
int lineByte=(bmpWidth*biBitCount/8+3)/4*4;
//循環(huán)變量,針對彩×××像,遍歷每像素的三個分量
int k;
//將圖像左下角1/4部分置成黑色
if(biBitCount==8)
{//對于灰度圖像
for(i=0;i { for(j=0;j { *(pBmpBuf+i*lineByte+j)=0; } } } else if(biBitCount==24) {//彩×××像 for(i=0;i { for(j=0;j { for(k=0;k<3;k++)//每像素RGB三個分量分別置0才變成黑色 *(pBmpBuf+i*lineByte+j*3+k)=0; } } } int colorTablesize = 0; if(biBitCount == 8) colorTablesize = 1024; FILE *fp = fopen(bmpName,"wb"); if(fp == 0) return 0; BITMAPFILEHEADER fileHead; fileHead.bfType= 0x4d42; fileHead.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte *bmpHeight; fileHead.bfReserved1 = 0; fileHead.bfReserved2 = 0; fileHead.bfOffBits = 54 +colorTablesize; fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp); BITMAPINFOHEADER head; head.biBitCount = biBitCount; head.biClrImportant = 0; head.biClrUsed = 0; head.biCompression = 0; head.biHeight = bmpHeight; head.biPlanes =1; head.biSize = 40; head.biSizeImage = lineByte *bmpHeight; head.biWidth = bmpWidth; head.biXPelsPerMeter = 0; head.biYPelsPerMeter = 0; fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp); if(biBitCount == 8) fwrite(pColorTable,sizeof(RGBQUAD),256,fp); fwrite(pBmpBuf,bmpHeight * lineByte,1,fp); fclose(fp); return TRUE; } 4.1 便于socket將接收到的結(jié)構(gòu)體寫入本地文件,上層封裝了一層,主要與3.1配套使用 int recvSaveBmp(char* filename,p_bmpinfo bmpinfo) { saveBmp(filename, bmpinfo->pBmpBuf, bmpinfo->bmpWidth, bmpinfo->bmpHeight, bmpinfo->biBitCount, bmpinfo->pColorTable); return TRUE; } ---------------------------------測試代碼------------------------------------------------ p_bmpinfo bmpinfo = (p_bmpinfo)malloc(sizeof(m_bmpinfo)); sendReadBmp(sFileName,bmpinfo); socketSend(client,(char*)bmpinfo,sizeof(m_bmpinfo));//socket發(fā)送 接收端: p_bmpinfo bmpinfo = (p_bmpinfo)malloc(sizeof(m_bmpinfo)); int len; SOCKET socket = socketAccept(server); //socket監(jiān)聽 do { len = socketRecv(socket,(char*)bmpinfo,sizeof(m_bmpinfo));//socket接受數(shù)據(jù) recvSaveBmp(sFileName,bmpinfo); } while(0 != len); --------------------------------么有了,木騙你------------------------------------ --------------------------------么有了,俺真木騙你------------------------------------
發(fā)送端:
本文名稱:c++bitmap截屏MFC
標題URL:http://weahome.cn/article/jegcoh.html