vc++的屏幕截圖

截圖功能想起來真的不難,可是,我實際操作的時候,卻發現真的不容易,網上講了很多方法,但是,要麼實現起來很複雜,要麼就是庫類在vc++不支持(其實網上資料很多,可能是我沒學會)。花了好長時間,終於用gdi+搞定了。

vc本身不帶有gdi+庫,需要自己下載,我上傳了一份。地址:
http://download.csdn.net/download/lsjweiyi/9727096

vc中還需要進行配置,具體配置方法百度吧。

#define ULONG_PTR ULONG
#include  <iostream>   
#include  <windows.h>
#include  <Gdiplus.h>
using   namespace   Gdiplus;
using   namespace   std   ;

//獲取Encoder的CLSID的方法
int GetEncoderClsid(const WCHAR* format, CLSID* pCLSID)
{
 UINT num = 0;
 UINT size = 0;
 ImageCodecInfo* pImageCodecInfo = NULL;//把指針變爲0

 GetImageEncodersSize(&num, &size);//獲取圖片編碼器大小

 if(size == 0){
    return -1;
    }
 pImageCodecInfo = (ImageCodecInfo *)(malloc(size));
 if(pImageCodecInfo == NULL){
    return -1;
    }
 GetImageEncoders(num, size, pImageCodecInfo);

 // Find for the support of format for image in the windows
 for(UINT i = 0; i < num; ++i)
 {
  //MimeType: Depiction for the program image 
  if( wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
  { 
   *pCLSID = pImageCodecInfo[i].Clsid;
   free(pImageCodecInfo);
   return i; 
  } 
 } 
 free(pImageCodecInfo); 
 return -1;

}

HRESULT GetScreenSnap()
{
  ///////////////////////////////////////////
  LPCWSTR     pszFileName = L"test.bmp";
  /*
  int     xs = 200;
  int     ys = 200;
  int     quality = 80;
  */
  int     quality = 80;

  //得到屏幕設備HDC以及長寬      
  HDC hdc = GetWindowDC(NULL);
  int x  = GetDeviceCaps(hdc,     HORZRES);      
  int y  = GetDeviceCaps(hdc,     VERTRES);

  //創建與屏幕設備兼容的內存區域HDC
  HDC hmemdc = ::CreateCompatibleDC(hdc);

  //創建相關位圖,把位圖與內存區域HDC綁定
  HBITMAP hbmp = ::CreateCompatibleBitmap(hdc,x,y),hold;      
  hold  = (HBITMAP)::SelectObject(hmemdc,hbmp);

  //把屏幕設備傳到內存區域(位圖)中
  BitBlt(hmemdc, 0, 0, x, y,hdc,0,0,SRCCOPY); 


   //添加鼠標到圖片裏
   //POINT l_pt;
   //::GetCursorPos(&l_pt);
   //HCURSOR l_hCursor = ::GetCursor();
   //::DrawIcon(hmemdc,l_pt.x,l_pt.y,l_hCursor); 

  //還原內存區域
  SelectObject(hmemdc,hold);
 /*
  //::ShowWindow(NULL,SW_SHOWMAXIMIZED);//SW_SHOWMAXIMIZED
  //得到顯示對話框的HDC
  HDC hdc1 = GetDC(NULL);
  //創建與對話框兼容的內存區域的DC
  HDC hmemdc1 = ::CreateCompatibleDC(hdc1); 
  //把位圖與對話框兼容內存綁定
  hold  = (HBITMAP)::SelectObject(hmemdc1,hbmp);
  //把對話框兼容內存的內容(位圖)傳至對話框
  BitBlt(hdc1, 0, 0, x, y,hmemdc1,0,0,SRCCOPY);

  //還原內存區域
  SelectObject(hmemdc1,hold);
  */

  ::UpdateWindow(NULL);
  {      

  //使用GDI+保存位圖
    Bitmap bit(hbmp,NULL);
    printf ("%d",sizeof(bit));
    printf ("\n");
    CLSID encoderClsid;
    //encoderClsid = (void*)(new char[10]);
    EncoderParameters encoderParameters;      

    encoderParameters.Count = 1;      
    encoderParameters.Parameter[0].Guid = EncoderQuality;      
    encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;      
    encoderParameters.Parameter[0].NumberOfValues = 1;      
    encoderParameters.Parameter[0].Value = &quality;      

    GetEncoderClsid(L"image/bmp",&encoderClsid);
    bit.Save(pszFileName,&encoderClsid,&encoderParameters);
   }      
   ::DeleteObject(hbmp);      
   ::DeleteObject(hmemdc);
   //::DeleteObject(hmemdc1);
   //::DeleteObject(hdc1);
   ::DeleteObject(hdc);
   return 0;   
} 

int main(){
    //初始化gdi+
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    GetScreenSnap();

    GdiplusShutdown(gdiplusToken); //關閉gdi+
    return 0;
}

放張效果圖:
這裏寫圖片描述

其實我還沒完全弄懂整個代碼,只知道是獲取屏幕句柄,然後用位圖保存。比如如何不保存直接把位圖顯示出來,不知道可不可以做到。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章