首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

截图原理,和调色板的两个有关问题

2013-01-25 
截图原理,和调色板的两个问题问题1:截屏原理是什么?比如 一些截图软件问题2: 为什么讲位图保存为图片的时

截图原理,和调色板的两个问题
问题1:截屏原理是什么?比如 一些截图软件

问题2: 为什么讲位图保存为图片的时候,会用到调色板这个东西?
网上的代码里是有调色板的
[解决办法]
问题1:截屏原理是什么?比如 一些截图软件
答:GetDC(0)获取屏幕DC,然后使用BitBlt复制到你自己的DC里
问题2:为什么讲位图保存为图片的时候,会用到调色板这个东西?
答:把图片保存为8位(256色)位图时需要调色版,不过现在截屏一般是保存为24位真彩色位图,不需要调色板.
[解决办法]
1.GetWindowDesktop获得桌面DC然后截取其中的内容即可

2.调色板8位色以后这个结构体就消亡了。
[解决办法]
熟悉下位图格式..
1.得到屏幕DC//HDC hdc = CreateDC("DISPLAY",0,0,0);
//hdc = CreateDC("DISPLAY",NULL,NULL,NULL);  
//    int cx  = GetDeviceCaps(hdc,HORZRES);  
//    int cy  = GetDeviceCaps(hdc,VERTRES);  
2.创建屏幕兼容位图,兼容DC
//HBITMAP    hBitmap = CreateCompatibleBitmap(hdc,cx,cy);  
//HDC        hmemDC  = CreateCompatibleDC(hdc);
3.兼容位图选进兼容设备,复制内容到兼容DC里面.
//    hOldBitmp =(HBITMAP)SelectObject(hmemDC,hBitmap);  
//    BitBlt(hmemDC,0,0,cx,cy,hdc,0,0,SRCCOPY);  
//    hBitmap = (HBITMAP)SelectObject(hmemDC,hOldBitmp);

根据得到的HBITMAP来得到位图数据
//先得到基本长度,宽度   
    BITMAP bm;  
    GetObject(hBitmap,sizeof(bm),&bm);  
    int nWidth = bm.bmWidth;  
    int nHeight = bm.bmHeight;

//根据长度,宽度计算出位图数据大小
//像素数据大小(byte)   
    DWORD dwBmBitsSize = ((nWidth*24+31)/32)*4*nHeight;

//获取位图数据   
    char *pBits = new char[dwBmBitsSize];  
    memset(pBits , 0 , dwBmBitsSize);  
    GetDIBits(hmemDC,hBitmap,0,nHeight,pBits,(LPBITMAPINFO)&bih,DIB_RGB_COLORS);  

整个过程基本完成,之后写入文件(24位没有调色版)
    BITMAPFILEHEADER bfh={0};//文件头  
    BITMAPINFOHEADER bih={0};//信息头  
  
    //文件头信息   
    bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
    bfh.bfReserved1 = 0;  
    bfh.bfReserved2 = 0;  
    bfh.bfSize = bfh.bfOffBits + dwBmBitsSize;  
    bfh.bfType = 'MB';

    //信息头信息   
    bih.biBitCount     = 24;  
    bih.biCompression  = BI_RGB;  
    bih.biHeight       = nHeight;  
    bih.biWidth        = nWidth;  
    bih.biPlanes       = 1;  
    bih.biSize         = sizeof(BITMAPINFOHEADER);  
    bih.biSizeImage    = dwBmBitsSize;  
    bih.biClrImportant = 0;  


    bih.biClrUsed      = 0;  
    bih.biXPelsPerMeter= 0;  
    bih.biYPelsPerMeter= 0;

//写文件   
    CFile file;  
    file.Open("1.bmp",CFile::modeCreate 
[解决办法]
 CFile::modeWrite);  
    file.Write(&bfh,14);  
    file.Write(&bih,40);  
    file.Write(pBits,dwBmBitsSize);  
    file.Close();

void CBmpView::OnFullscreen() 
{
// TODO: Add your command handler code here
HDC hdc,hmemDC;
HBITMAP hBitmap,hOldBitmp;
//屏幕DC,长度,宽度
hdc = CreateDC("DISPLAY",NULL,NULL,NULL);
int cx  = GetDeviceCaps(hdc,HORZRES);
int cy  = GetDeviceCaps(hdc,VERTRES);

//兼容DC,兼容位图
hmemDC  = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc,cx,cy);
hOldBitmp =(HBITMAP)SelectObject(hmemDC,hBitmap);
BitBlt(hmemDC,0,0,cx,cy,hdc,0,0,SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hmemDC,hOldBitmp);
DeleteDC(hdc);
DeleteObject(hOldBitmp);

//基本长度,宽度
BITMAP bm;
GetObject(hBitmap,sizeof(bm),&bm);
int nWidth = bm.bmWidth;
int nHeight = bm.bmHeight;

//像素数据大小(byte)
DWORD dwBmBitsSize = ((nWidth*24+31)/32)*4*nHeight;

BITMAPFILEHEADER bfh={0};
BITMAPINFOHEADER bih={0};

//文件头信息
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfSize = bfh.bfOffBits + dwBmBitsSize;
bfh.bfType = 'MB';

//信息头信息
bih.biBitCount     = 24;
bih.biCompression  = BI_RGB;
bih.biHeight       = nHeight;
bih.biWidth        = nWidth;
bih.biPlanes       = 1;
bih.biSize         = sizeof(BITMAPINFOHEADER);
bih.biSizeImage    = dwBmBitsSize;
bih.biClrImportant = 0;
bih.biClrUsed      = 0;
bih.biXPelsPerMeter= 0;
bih.biYPelsPerMeter= 0;

//位图数据
char *pBits = new char[dwBmBitsSize];
memset(pBits , 0 , dwBmBitsSize);
GetDIBits(hmemDC,hBitmap,0,nHeight,pBits,(LPBITMAPINFO)&bih,DIB_RGB_COLORS);
DeleteDC(hmemDC);
DeleteObject(hBitmap);

//写文件
CFile file;
file.Open("1.bmp",CFile::modeCreate 
[解决办法]
 CFile::modeWrite);
file.Write(&bfh,14);
file.Write(&bih,40);
file.Write(pBits,dwBmBitsSize);
file.Close();
delete []pBits;
}

  


  

热点排行