1)头文件
#include "stdafx.h"
#include
#include
#include
#pragma comment(lib,"jpeglib/libjpeg.lib")
#include "jpeglib/jpeglib.h"
2)window 截屏函数
int GetScreenBitMap(int x,int y,int w,int h,BYTE *dataBuf)
{
if(w<0||h<0)
{
return 0;//异常
}
HWND hWnd = GetDesktopWindow();//获得屏幕的HWND.
HDC hScreenDC = GetDC(hWnd); //获得屏幕的HDC.
HDC MemDC = CreateCompatibleDC(hScreenDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC,w,h);//改成创建的幕布的大小。
HGDIOBJ hOldBMP = SelectObject(MemDC,hBitmap);
BitBlt(MemDC,0,0,w,h,hScreenDC,x,y,SRCCOPY);//改变截屏大小,从全屏改成
SelectObject(MemDC,hOldBMP);
DeleteObject(MemDC);
ReleaseDC(hWnd,hScreenDC);
WriteBmpToBuf( hBitmap,dataBuf);
return 1;
}
3)将BitMap转换为Byte
void WriteBmpToBuf(HBITMAP hBitmap,BYTE *dataBuf)
{
const char name_type[8]="DISPLAY";
HDC hDC =CreateDC((LPCWSTR)name_type,NULL,NULL,NULL);
int iBits ;
BITMAP bm; //位图属性结构
GetObject(hBitmap, sizeof(bm), (LPSTR)&bm);
iBits=bm.bmBitsPixel;
WORD wBitCount; //位图中每个像素所占字节数
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
wBitCount = iBits;
DWORD dwPaletteSize=0; //调色板大小, 位图中像素字节大小
if (wBitCount <= 8)
dwPaletteSize = (1 <
BITMAPINFOHEADER bi,bi1; //位图信息头结构
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth