将HDC保存为BMP文件

HDC在MSDN中的全称为:The handle of device context。通常,我们都是用来做相应的显示操作。
    
    熟悉WIN32的朋友对于其应该不会陌生,经常采用GetDC,GetWindowDC等等来获取其句柄。而用得最多的,可能就是BeginPaint,如:

view plaincopy to clipboardprint?

  1. case WM_PAINT:  
  2.     HDC hdc = BeginPaint(hWnd,&ps);  
  3.     ...  
  4.     EndPaint(hdc,&ps);  
  5.     break;   

[cpp] view plaincopy

  1. case WM_PAINT:      HDC hdc = BeginPaint(hWnd,&ps);     ...     EndPaint(hdc,&ps);      break;   

 

   使用起来非常简单,但如果想将其内容保存为普通的图像文档,可就没那么容易。确切地说,在只知道HDC句柄的情况下,我们是无法保存其内容的;但我们可以剑走偏锋,将HDC的内容写到一个缓存中,然后我们再保存该缓存的内容即可。
   
   听起来很简单,却又像很复杂,不是么?没关系,我们现在一步一步来。
   
   
   首先,我们需要一个HDC的句柄。如同前面所说,你可以有多种方法,比如GetDC,GetWindowDC,甚至是CreateDC。反正呢,你用什么方法我不管,我只要有一个HDC的句柄就好了。
   
   有了HDC的句柄,接下来我们所需要做的是,知道这HDC的大小,也就是宽度和长度。这个不难,我们只要简单地调用GetDeviceCaps,然后参数给予HORZRES或VERTRES即可:

view plaincopy to clipboardprint?

  1. int iWidth = GetDeviceCaps(hdc,HORZRES);  
  2. int iHeight = GetDeviceCaps(hdc,VERTRES);  

[cpp] view plaincopy

  1. int iWidth = GetDeviceCaps(hdc,HORZRES);    int iHeight = GetDeviceCaps(hdc,VERTRES);  

 

    为什么要知道大小呢?因为我们要用它来创建缓存。而这缓存,说白了,其实就是一个BMP格式的数据结构而已。
    
    为了创建这个关键的缓存,我们必须调用CreateDIBSection函数,而该函数形参又用到BITMAPINFOHEADER,所以我们的一切,就先从填充该结构体开始。
    
    该结构体定义如下:

view plaincopy to clipboardprint?

  1. typedef struct tagBITMAPINFO  
  2. {   
  3.   BITMAPINFOHEADER bmiHeader;   
  4.   RGBQUAD bmiColors[1];   
  5. } BITMAPINFO;  

[cpp] view plaincopy

  1. typedef struct tagBITMAPINFO    {       BITMAPINFOHEADER bmiHeader;       RGBQUAD bmiColors[1];     } BITMAPINFO;  

 

    结构体里面还有一个BITMAPINFOHEADER,其定义如下:

view plaincopy to clipboardprint?

  1. typedef struct tagBITMAPINFOHEADER   
  2. {   
  3.   DWORD biSize;   
  4.   LONG biWidth;   
  5.   LONG biHeight;   
  6.   WORD biPlanes;   
  7.   WORD biBitCount   
  8.   DWORD biCompression;   
  9.   DWORD biSizeImage;   
  10.   LONG biXPelsPerMeter;   
  11.   LONG biYPelsPerMeter;   
  12.   DWORD biClrUsed;   
  13.   DWORD biClrImportant;   
  14. } BITMAPINFOHEADER;  

[cpp] view plaincopy

  1. typedef struct tagBITMAPINFOHEADER     {       DWORD biSize;       LONG biWidth;       LONG biHeight;       WORD biPlanes;       WORD biBitCount       DWORD biCompression;       DWORD biSizeImage;       LONG biXPelsPerMeter;       LONG biYPelsPerMeter;       DWORD biClrUsed;       DWORD biClrImportant;     } BITMAPINFOHEADER;  

 

    这么多变量,是不是有点头晕?大可不必紧张,其实我们只需要填充其中几个,其它统统置为0即可:

view plaincopy to clipboardprint?

  1. BITMAPINFO bmpInfo = {0};  
  2. bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
  3. bmpInfo.bmiHeader.biWidth = iWidth;  
  4. bmpInfo.bmiHeader.biHeight = iHeight;  
  5. bmpInfo.bmiHeader.biPlanes = 1;  
  6. bmpInfo.bmiHeader.biBitCount = 24;  

[cpp] view plaincopy

  1. BITMAPINFO bmpInfo = {0};    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);    bmpInfo.bmiHeader.biWidth = iWidth;    bmpInfo.bmiHeader.biHeight = iHeight;    bmpInfo.bmiHeader.biPlanes = 1;    bmpInfo.bmiHeader.biBitCount = 24;  

 

    一切从最简单做起,对于BMP而言,最简单的自然是24位位图,这就是为什么biPlanes和biBitCount分别设置为1和24的原因。
    
    填充完BITMAPINFO结构,我们还是不能马上调用CreateDIBSection,因为形参中还有一个HDC。虽然我们可以直接采用已知的HDC句柄,但接下来还要将创建的HBITMAP和HDC相连接,所以我们还是先创建一个缓存DC:

view plaincopy to clipboardprint?

  1. HDC hdcMem = CreateCompatibleDC(hdc);  

[cpp] view plaincopy

  1. HDC hdcMem = CreateCompatibleDC(hdc);  

 

    一切准备就绪之后,就调用CreateDIBSection吧:

view plaincopy to clipboardprint?

  1. BYTE *pData = NULL;  
  2. hBmp = CreateDIBSection(hdcMem,&bmpInfo,DIB_RGB_COLORS,reinterpret_cast<VOID **>(&pData),NULL,0);  

[cpp] view plaincopy

  1. BYTE *pData = NULL;    hBmp = CreateDIBSection(hdcMem,&bmpInfo,DIB_RGB_COLORS,reinterpret_cast<VOID **>(&pData),NULL,0);  

 

    pData是分配的一个内存空间,将来用来存储HDC的内容,只不过现在一切都是空的。如果你将这数据保存出来,你会发现一团漆黑。
    
    将HBITMAP和HDC结合:

view plaincopy to clipboardprint?

  1. hOldObj = SelectObject(hdcMem, hBmp);  

[cpp] view plaincopy

  1. hOldObj = SelectObject(hdcMem, hBmp);  

 

    至此为止,我们前期工作已经准备就绪,我们只需要将HDC的内容用BitBlt绘制到缓存中即可:

view plaincopy to clipboardprint?

  1. BitBlt(hdcMem,  
  2.        0,  
  3.        0,  
  4.        iWidth,  
  5.        iHeight,  
  6.        hdc,  
  7.        0,  
  8.        0,  
  9.        SRCCOPY);  

[cpp] view plaincopy

  1. BitBlt(hdcMem,           0,           0,           iWidth,           iHeight,           hdc,           0,           0,           SRCCOPY);  

 

  这里其实还有一个小技巧,如果你是想绘制HDC的某个区域,你只需要用StretchBlt替代即可:

view plaincopy to clipboardprint?

  1. StretchBlt(hdcMem,  
  2.             0,  
  3.             0,  
  4.             iWidth,  
  5.             iHeight,  
  6.             hdc,  
  7.             rcDC.left,  
  8.             rcDC.top,  
  9.             rcDC.right - rcDC.left + 1,  
  10.             rcDC.bottom - rcDC.top + 1,  
  11.             SRCCOPY);  

[cpp] view plaincopy

  1. StretchBlt(hdcMem,                0,                0,                iWidth,                iHeight,                hdc,                rcDC.left,                rcDC.top,                rcDC.right - rcDC.left + 1,                rcDC.bottom - rcDC.top + 1,                SRCCOPY);  

 

    喜欢追究问题的你,也许会发现,在调用该函数之后,pData所指向的内存缓冲区已经改变。是的,没错,这些改变的数据就是我们所需要的。接下来我们所需要做的仅仅是,将这数据按BMP文件的格式,保存下来即可。
    
    BMP文件格式其实很简单,最开始是文件头信息,然后是图片信息,接下来是数据。我们只需要按照这格式,顺序将数据写入即可。
    
    文件头信息和图片信息,微软已经为我们定义好了相应的结构体:
    
    BMP信息:

view plaincopy to clipboardprint?

  1. typedef struct tagBITMAPINFOHEADER   
  2. {   
  3.   DWORD biSize;   
  4.   LONG biWidth;   
  5.   LONG biHeight;   
  6.   WORD biPlanes;   
  7.   WORD biBitCount   
  8.   DWORD biCompression;   
  9.   DWORD biSizeImage;   
  10.   LONG biXPelsPerMeter;   
  11.   LONG biYPelsPerMeter;   
  12.   DWORD biClrUsed;   
  13.   DWORD biClrImportant;   
  14. } BITMAPINFOHEADER;  

[cpp] view plaincopy

  1. typedef struct tagBITMAPINFOHEADER     {       DWORD biSize;       LONG biWidth;       LONG biHeight;       WORD biPlanes;       WORD biBitCount       DWORD biCompression;       DWORD biSizeImage;       LONG biXPelsPerMeter;       LONG biYPelsPerMeter;       DWORD biClrUsed;       DWORD biClrImportant;     } BITMAPINFOHEADER;  

 

    文件头信息:

view plaincopy to clipboardprint?

  1. typedef struct tagBITMAPFILEHEADER   
  2. {  
  3.   WORD bfType;   
  4.   DWORD bfSize;   
  5.   WORD bfReserved1;   
  6.   WORD bfReserved2;   
  7.   DWORD bfOffBits;   
  8. } BITMAPFILEHEADER;   

[cpp] view plaincopy

  1. typedef struct tagBITMAPFILEHEADER     {      WORD bfType;       DWORD bfSize;       WORD bfReserved1;       WORD bfReserved2;       DWORD bfOffBits;     } BITMAPFILEHEADER;   

 

    我们首先填充这两个结构体数值:

view plaincopy to clipboardprint?

  1. BITMAPINFOHEADER bmInfoHeader = {0};  
  2. bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);  
  3. bmInfoHeader.biWidth = iWidth;  
  4. bmInfoHeader.biHeight = iHeight;  
  5. bmInfoHeader.biPlanes = 1;  
  6. bmInfoHeader.biBitCount = 24;  
  7.   
  8. //Bimap file header in order to write bmp file  
  9. BITMAPFILEHEADER bmFileHeader = {0};  
  10. bmFileHeader.bfType = 0x4d42;  //bmp    
  11. bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
  12. bmFileHeader.bfSize = bmFileHeader.bfOffBits + ((bmInfoHeader.biWidth * bmInfoHeader.biHeight) * 3); ///3=(24 / 8)  

[cpp] view plaincopy

  1. BITMAPINFOHEADER bmInfoHeader = {0};    bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);    bmInfoHeader.biWidth = iWidth;    bmInfoHeader.biHeight = iHeight;    bmInfoHeader.biPlanes = 1;    bmInfoHeader.biBitCount = 24;        //Bimap file header in order to write bmp file    BITMAPFILEHEADER bmFileHeader = {0};    bmFileHeader.bfType = 0x4d42;  //bmp      bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);    bmFileHeader.bfSize = bmFileHeader.bfOffBits + ((bmInfoHeader.biWidth * bmInfoHeader.biHeight) * 3); ///3=(24 / 8)  

 

    接下来的事情,估计大家都轻车熟路了。创建文件,然后写入数据,保存,完毕。

view plaincopy to clipboardprint?

  1. HANDLE hFile = CreateFile(strFile.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  
  2. DWORD dwWrite = 0;  
  3. WriteFile(hFile,&bmFileHeader,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);  
  4. WriteFile(hFile,&bmInfoHeader, sizeof(BITMAPINFOHEADER),&dwWrite,NULL);  
  5. WriteFile(hFile,&vtData[0], vtData.size(),&dwWrite,NULL);  
  6. CloseHandle(hFile);  

[cpp] view plaincopy

  1. HANDLE hFile = CreateFile(strFile.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);    DWORD dwWrite = 0;    WriteFile(hFile,&bmFileHeader,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);    WriteFile(hFile,&bmInfoHeader, sizeof(BITMAPINFOHEADER),&dwWrite,NULL);    WriteFile(hFile,&vtData[0], vtData.size(),&dwWrite,NULL);    CloseHandle(hFile);  

 

    文章的最后,是参考源代码:

view plaincopy to clipboardprint?

  1. #ifdef UNICODE  
  2.     #ifndef TSTRING  
  3.         #define TSTRING std::wstring  
  4.     #endif  
  5. #else  
  6.     #ifndef TSTRING  
  7.         #define TSTRING std::string  
  8.     #endif  
  9. #endif  
  10.   
  11. BOOL WriteBmp(const TSTRING &strFile,const std::vector<BYTE> &vtData,const SIZE &sizeImg);  
  12. BOOL WriteBmp(const TSTRING &strFile,HDC hdc);  
  13. BOOL WriteBmp(const TSTRING &strFile,HDC hdc,const RECT &rcDC);  
  14.   
  15. BOOL WriteBmp(const TSTRING &strFile,const std::vector<BYTE> &vtData,const SIZE &sizeImg)   
  16. {     
  17.   
  18.     BITMAPINFOHEADER bmInfoHeader = {0};  
  19.     bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);  
  20.     bmInfoHeader.biWidth = sizeImg.cx;  
  21.     bmInfoHeader.biHeight = sizeImg.cy;  
  22.     bmInfoHeader.biPlanes = 1;  
  23.     bmInfoHeader.biBitCount = 24;  
  24.   
  25.     //Bimap file header in order to write bmp file  
  26.     BITMAPFILEHEADER bmFileHeader = {0};  
  27.     bmFileHeader.bfType = 0x4d42;  //bmp    
  28.     bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
  29.     bmFileHeader.bfSize = bmFileHeader.bfOffBits + ((bmInfoHeader.biWidth * bmInfoHeader.biHeight) * 3); ///3=(24 / 8)  
  30.   
  31.     HANDLE hFile = CreateFile(strFile.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  
  32.     if(hFile == INVALID_HANDLE_VALUE)  
  33.     {  
  34.         return FALSE;  
  35.     }  
  36.   
  37.     DWORD dwWrite = 0;  
  38.     WriteFile(hFile,&bmFileHeader,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);  
  39.     WriteFile(hFile,&bmInfoHeader, sizeof(BITMAPINFOHEADER),&dwWrite,NULL);  
  40.     WriteFile(hFile,&vtData[0], vtData.size(),&dwWrite,NULL);  
  41.   
  42.   
  43.     CloseHandle(hFile);  
  44.   
  45.     return TRUE;  
  46. }   
  47.   
  48.   
  49. BOOL WriteBmp(const TSTRING &strFile,HDC hdc)  
  50. {  
  51.     int iWidth = GetDeviceCaps(hdc,HORZRES);  
  52.     int iHeight = GetDeviceCaps(hdc,VERTRES);  
  53.     RECT rcDC = {0,0,iWidth,iHeight};  
  54.   
  55.     return WriteBmp(strFile,hdc,rcDC);    
  56. }  
  57.   
  58. BOOL WriteBmp(const TSTRING &strFile,HDC hdc,const RECT &rcDC)  
  59. {  
  60.     BOOL bRes = FALSE;  
  61.     BITMAPINFO bmpInfo = {0};  
  62.     BYTE *pData = NULL;  
  63.     SIZE sizeImg = {0};  
  64.     HBITMAP hBmp = NULL;  
  65.     std::vector<BYTE> vtData;  
  66.     HGDIOBJ hOldObj = NULL;  
  67.     HDC hdcMem = NULL;  
  68.   
  69.     //Initilaize the bitmap information   
  70.     bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
  71.     bmpInfo.bmiHeader.biWidth = rcDC.right - rcDC.left;  
  72.     bmpInfo.bmiHeader.biHeight = rcDC.bottom - rcDC.top;  
  73.     bmpInfo.bmiHeader.biPlanes = 1;  
  74.     bmpInfo.bmiHeader.biBitCount = 24;  
  75.   
  76.     //Create the compatible DC to get the data  
  77.     hdcMem = CreateCompatibleDC(hdc);  
  78.     if(hdcMem == NULL)  
  79.     {  
  80.         goto EXIT;  
  81.     }  
  82.   
  83.     //Get the data from the memory DC     
  84.     hBmp = CreateDIBSection(hdcMem,&bmpInfo,DIB_RGB_COLORS,reinterpret_cast<VOID **>(&pData),NULL,0);  
  85.     if(hBmp == NULL)  
  86.     {  
  87.         goto EXIT;  
  88.     }  
  89.     hOldObj = SelectObject(hdcMem, hBmp);  
  90.       
  91.     //Draw to the memory DC  
  92.     sizeImg.cx = bmpInfo.bmiHeader.biWidth;  
  93.     sizeImg.cy = bmpInfo.bmiHeader.biHeight;  
  94.     StretchBlt(hdcMem,  
  95.                 0,  
  96.                 0,  
  97.                 sizeImg.cx,  
  98.                 sizeImg.cy,  
  99.                 hdc,  
  100.                 rcDC.left,  
  101.                 rcDC.top,  
  102.                 rcDC.right - rcDC.left + 1,  
  103.                 rcDC.bottom - rcDC.top + 1,  
  104.                 SRCCOPY);  
  105.       
  106.   
  107.     vtData.resize(sizeImg.cx * sizeImg.cy * 3);  
  108.     memcpy(&vtData[0],pData,vtData.size());  
  109.     bRes = WriteBmp(strFile,vtData,sizeImg);  
  110.   
  111.     SelectObject(hdcMem, hOldObj);  
  112.       
  113.   
  114. EXIT:  
  115.     if(hBmp != NULL)  
  116.     {  
  117.         DeleteObject(hBmp);  
  118.     }  
  119.   
  120.     if(hdcMem != NULL)  
  121.     {  
  122.         DeleteDC(hdcMem);  
  123.     }  
  124.   
  125.     return bRes;  
  126. }  

[cpp] view plaincopy

  1. #ifdef UNICODE#ifndef TSTRING#define TSTRING std::wstring#endif#else#ifndef TSTRING#define TSTRING std::string#endif#endifBOOL WriteBmp(const TSTRING &strFile,const std::vector<BYTE> &vtData,const SIZE &sizeImg);BOOL WriteBmp(const TSTRING &strFile,HDC hdc);BOOL WriteBmp(const TSTRING &strFile,HDC hdc,const RECT &rcDC);BOOL WriteBmp(const TSTRING &strFile,const std::vector<BYTE> &vtData,const SIZE &sizeImg) { BITMAPINFOHEADER bmInfoHeader = {0};bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);bmInfoHeader.biWidth = sizeImg.cx;bmInfoHeader.biHeight = sizeImg.cy;bmInfoHeader.biPlanes = 1;bmInfoHeader.biBitCount = 24;//Bimap file header in order to write bmp fileBITMAPFILEHEADER bmFileHeader = {0};bmFileHeader.bfType = 0x4d42;  //bmp  bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);bmFileHeader.bfSize = bmFileHeader.bfOffBits + ((bmInfoHeader.biWidth * bmInfoHeader.biHeight) * 3); ///3=(24 / 8)HANDLE hFile = CreateFile(strFile.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);if(hFile == INVALID_HANDLE_VALUE){return FALSE;}DWORD dwWrite = 0;WriteFile(hFile,&bmFileHeader,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);WriteFile(hFile,&bmInfoHeader, sizeof(BITMAPINFOHEADER),&dwWrite,NULL);WriteFile(hFile,&vtData[0], vtData.size(),&dwWrite,NULL);CloseHandle(hFile);return TRUE;} BOOL WriteBmp(const TSTRING &strFile,HDC hdc){int iWidth = GetDeviceCaps(hdc,HORZRES);int iHeight = GetDeviceCaps(hdc,VERTRES);RECT rcDC = {0,0,iWidth,iHeight};return WriteBmp(strFile,hdc,rcDC);}BOOL WriteBmp(const TSTRING &strFile,HDC hdc,const RECT &rcDC){BOOL bRes = FALSE;BITMAPINFO bmpInfo = {0};BYTE *pData = NULL;SIZE sizeImg = {0};HBITMAP hBmp = NULL;std::vector<BYTE> vtData;HGDIOBJ hOldObj = NULL;HDC hdcMem = NULL;//Initilaize the bitmap informationbmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);bmpInfo.bmiHeader.biWidth = rcDC.right - rcDC.left;bmpInfo.bmiHeader.biHeight = rcDC.bottom - rcDC.top;bmpInfo.bmiHeader.biPlanes = 1;bmpInfo.bmiHeader.biBitCount = 24;//Create the compatible DC to get the datahdcMem = CreateCompatibleDC(hdc);if(hdcMem == NULL){goto EXIT;}//Get the data from the memory DChBmp = CreateDIBSection(hdcMem,&bmpInfo,DIB_RGB_COLORS,reinterpret_cast<VOID **>(&pData),NULL,0);if(hBmp == NULL){goto EXIT;}hOldObj = SelectObject(hdcMem, hBmp);//Draw to the memory DCsizeImg.cx = bmpInfo.bmiHeader.biWidth;sizeImg.cy = bmpInfo.bmiHeader.biHeight;StretchBlt(hdcMem,0,0,sizeImg.cx,sizeImg.cy,hdc,rcDC.left,rcDC.top,rcDC.right - rcDC.left + 1,rcDC.bottom - rcDC.top + 1,SRCCOPY);vtData.resize(sizeImg.cx * sizeImg.cy * 3);memcpy(&vtData[0],pData,vtData.size());bRes = WriteBmp(strFile,vtData,sizeImg);SelectObject(hdcMem, hOldObj);EXIT:if(hBmp != NULL){DeleteObject(hBmp);}if(hdcMem != NULL){DeleteDC(hdcMem);}return bRes;}  

 

    一共有三个WriteBmp函数,使用上非常简单。
    
    比如,我想保存一个HDC,只需要简单地调用:

view plaincopy to clipboardprint?

  1. HDC hdc = GetDC(NULL);  
  2. WriteBmp(TEXT("//NAND//DCSave.bmp"));  
  3. ReleaseDC(NULL,hdc);  

[cpp] view plaincopy

  1. HDC hdc = GetDC(NULL);    WriteBmp(TEXT("//NAND//DCSave.bmp"));    ReleaseDC(NULL,hdc);  

 

    如果想保存HDC的某一个部分,同样也很简单:

view plaincopy to clipboardprint?

  1. HDC hdc = GetDC(NULL);  
  2. RECT rcDC = {0,0,100,100};  
  3. WriteBmp(TEXT("//NAND//DCSavePart.bmp"),rcDC);  
  4. ReleaseDC(NULL,hdc);  

[cpp] view plaincopy

  1. HDC hdc = GetDC(NULL);    RECT rcDC = {0,0,100,100};    WriteBmp(TEXT("//NAND//DCSavePart.bmp"),rcDC);    ReleaseDC(NULL,hdc);  

 

    这个函数还能做到一个很有意思的功能,就是截取屏幕。对于屏幕来说,也是一个HDC,我们只要获取屏幕的HDC句柄,剩下的就没有什么难度了:

view plaincopy to clipboardprint?

  1. HDC hdc = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);  
  2. WriteBmp(TEXT("//NAND//ScreenCapture.BMP"),hdc);  
  3. DeleteDC(hdc);  
时间: 2024-06-16 00:04:53

将HDC保存为BMP文件的相关文章

WinCE5.0下如何截屏并保存为BMP文件

前一篇介绍了如何在应用程序中操作FrameBuffer,实际上得到FrameBuffer后,对于我们来说截取屏幕就是很容易的事情了,完全可以绕开GDI,并且效率要比使用GDI高.  1     BITMAPINFO    bi; 2     BITMAPFILEHEADER bmfHdr; 3     HANDLE hFile; 4     DWORD dwWritten; 5  6     ZeroMemory(&bi, sizeof(BITMAPINFO)); 7     bi.bmiHea

c++-有谁有C++的代码实现连续从文件夹里读入BMP图像,在处理之后能够自动保存到指定文件夹的代码吗?急求

问题描述 有谁有C++的代码实现连续从文件夹里读入BMP图像,在处理之后能够自动保存到指定文件夹的代码吗?急求 有谁有C++的代码实现连续从文件夹里读入BMP图像,在处理之后能够自动保存到指定文件夹的代码吗?急求 解决方案 http://blog.csdn.net/flyfish1986/article/details/5372427 遍历文件 至于你说的读取bmp图像和处理,不知道你要如何处理 解决方案二: http://www.cnblogs.com/kex1n/archive/2011/1

1.1.0-学习Opencv与MFC混合编程之---全屏截图,保存为BMP图像(并增加快捷键)

源代码:http://download.csdn.net/detail/nuptboyzhb/3961677 Ø  添加全屏截图菜单项,菜单项的属性如下; Ø  为该菜单项建立类向导. 编辑消息处理函数如下 voidCCVMFCView::OnPrintscreen() {                   CRect rect;          //HBITMAP hMap;          rect.left = 0;          rect.top = 0;          r

python使用PyGame绘制图像并保存为图片文件的方法

  本文实例讲述了python使用PyGame绘制图像并保存为图片文件的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 ''' pg_draw_circle_save101.py draw a blue solid circle o

24-bit bmp格式图片颜色数据的获取和bmp文件的写入

问题描述 初学,我先简单说下我的思路吧!首先获得图片信息,然后根据位图信息头中相应的字节得到源图的宽和高,然后将宽和高转换成整型(int).随后是获得图片的颜色信息.后来的是第二个类,这个类的作用是将得到的图片写入另一个bmp文件.我先把颜色的信息放到一个叫pointArray的数组,就是这个地方我不知道有没有问题,然后用java.awt.color中的getRed(),getBlue()...等方法获得像素中的颜色分量,最后可能是颜色存储的地方有问题,思绪混乱了,希望能得到帮助如下是源代码:/

python使用PyGame绘制图像并保存为图片文件的方法_python

本文实例讲述了python使用PyGame绘制图像并保存为图片文件的方法.分享给大家供大家参考.具体实现方法如下: ''' pg_draw_circle_save101.py draw a blue solid circle on a white background save the drawing to an image file for result see http://prntscr.com/156wxi tested with Python 2.7 and PyGame 1.9.2

C语言解析BMP文件的结构

Windows   GDI提供了     typedef   struct   tagBITMAPFILEHEADER   {           WORD         bfType;           DWORD       bfSize;           WORD         bfReserved1;           WORD         bfReserved2;           DWORD       bfOffBits;       }   BITMAPFILE

php限制上传文件类型并保存上传文件的方法

 这篇文章主要介绍了php限制上传文件类型并保存上传文件的方法,涉及php针对上传文件的常用操作技巧,非常具有实用价值,需要的朋友可以参考下     本文实例讲述了php限制上传文件类型并保存上传文件的方法.分享给大家供大家参考.具体如下: 下面的代码演示了php中如何获取用户上传的文件,并限制文件类型的一般图片文件,最后保存到服务器 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 3

关于使用java从http接口取数据保存到本地文件的中文乱码处理

关于使用java从http接口取数据保存到本地文件的中文乱码处理 要做到如下几点: 1. 取接口数据,要注意加入编码设置与接口文件本身的编码设置一致,才能取得正确的数据   注意如下的: ins =new InputStreamReader(connection.getInputStream(),"GBK"):   必须加入第二个编码格式参数:参数的值与接口返回的数据的编码格式一致         /**   * 从某个接口取返回数据内容   * @param url   * @ret