溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

c++ bitmap 截屏 MFC

發(fā)布時(shí)間:2020-07-15 04:13:14 來(lái)源:網(wǎng)絡(luò) 閱讀:924 作者:忘記江南 欄目:編程語(yǔ)言

****首先感謝巨人的肩膀,讓小白能夠走出bitmap圖片的苦海。****

   自己閑的沒(méi)事想通過(guò)c++ 的MFC來(lái)截屏并上傳服務(wù)器展示客戶端的當(dāng)前界面。中間一路降妖伏魔....(此處省略2048個(gè)字節(jié)),發(fā)揚(yáng)程序猿無(wú)私奉獻(xiàn)的優(yōu)良品質(zhì),分享源代碼來(lái)眾生同樂(lè)。


1.好戲在前頭,請(qǐng)?jiān)?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)頁(yè)休息會(huì)--------------------------------------

2.截屏接口,保存截屏(格式為.bmp)到指定路徑

in char* filenam:需要保存截屏的文件路徑

void getScreen(char* filename)

{

    CDC *pDC;//屏幕DC

    pDC = CDC::FromHandle(GetDC(NULL));//獲取當(dāng)前整個(gè)屏幕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;//每個(gè)像素字節(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);//寫(xiě)入位圖文件頭 

    fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//寫(xiě)入位圖信息頭

    byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申請(qǐng)內(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);//寫(xiě)入位圖數(shù)據(jù)

    delete [] p;   

    fclose(fp);

    memDC.SelectObject(oldmemBitmap);

}

--------------------------累了吧,后面沒(méi)有了--------------------------------------

-----------------------------------------------------------------------------------

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;//計(jì)算圖像每行像素所占的字節(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)提供一個(gè)封裝接口,將所有參數(shù)到一個(gè)結(jié)構(gòu)體中,可便于socket傳輸(我4不4很聰明,請(qǐng)叫我少先隊(duì)員)

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ù)寫(xiě)入到本地文件

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 待存盤(pán)的位圖數(shù)據(jù)

return 0;

int i,j;

//每行字節(jié)數(shù)

int lineByte=(bmpWidth*biBitCount/8+3)/4*4;

//循環(huán)變量,針對(duì)彩×××像,遍歷每像素的三個(gè)分量

int k;

//將圖像左下角1/4部分置成黑色

if(biBitCount==8)

{//對(duì)于灰度圖像

for(i=0;i<bmpHeight/2;i++)

{

for(j=0;j<bmpWidth/2;j++)

{

*(pBmpBuf+i*lineByte+j)=0;

}

}

}

else if(biBitCount==24)

{//彩×××像

for(i=0;i<bmpHeight/2;i++)

{

for(j=0;j<bmpWidth/2;j++)

{

for(k=0;k<3;k++)//每像素RGB三個(gè)分量分別置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)體寫(xiě)入本地文件,上層封裝了一層,主要與3.1配套使用

int recvSaveBmp(char* filename,p_bmpinfo bmpinfo)

{

saveBmp(filename, bmpinfo->pBmpBuf, bmpinfo->bmpWidth, bmpinfo->bmpHeight, bmpinfo->biBitCount, bmpinfo->pColorTable);

return TRUE;

}

---------------------------------測(cè)試代碼------------------------------------------------
發(fā)送端:

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)聽(tīng)

do {

len = socketRecv(socket,(char*)bmpinfo,sizeof(m_bmpinfo));//socket接受數(shù)據(jù)

recvSaveBmp(sFileName,bmpinfo);

} while(0 != len);


--------------------------------么有了,木騙你------------------------------------






--------------------------------么有了,俺真木騙你------------------------------------

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI