<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    將HDC保存為BMP文件

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    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("http://NAND//DCSave.bmp"));  
    3. ReleaseDC(NULL,hdc);  

    [cpp] view plaincopy
    1. HDC hdc = GetDC(NULL);    WriteBmp(TEXT("http://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("http://NAND//DCSavePart.bmp"),rcDC);  
    4. ReleaseDC(NULL,hdc);  

    [cpp] view plaincopy
    1. HDC hdc = GetDC(NULL);    RECT rcDC = {0,0,100,100};    WriteBmp(TEXT("http://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("http://NAND//ScreenCapture.BMP"),hdc);  
    3. DeleteDC(hdc);  
    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全