用GDI+抓屏并存成.jpg文件

#include     
#include     
#include     

using  namespace  Gdiplus;  

#pragma  comment  (lib,  "gdiplus.lib")  

GdiplusStartupInput  gdiplusStartupInput;  
ULONG_PTR                      gdiplusToken;  

int  GetEncoderClsid(LPCWSTR  format,  CLSID*  pClsid)  
{  
       UINT    num  =  0;  
       UINT    size  =  0;  

       ImageCodecInfo*  pImageCodecInfo  =   NULL;  

       GetImageEncodersSize(&num,  &size);  
       if (size  ==  0)  
               return  -1;  

       pImageCodecInfo  =  (ImageCodecInfo*)(malloc(size));  
       if (pImageCodecInfo  ==   NULL)  
               return  -1;  

       GetImageEncoders(num,  size,  pImageCodecInfo);  

       for (UINT  j  =  0;  j  <  num;  ++j)  
       {  
                if (  wcscmp(pImageCodecInfo[j].MimeType,  format)  ==  0  )  
               {  
                       *pClsid  =  pImageCodecInfo[j].Clsid;  
                       free(pImageCodecInfo);  
                        return  j;  
               }  
       }  

       free(pImageCodecInfo);  
       return  -1;  
}  
//    参数  
//            xs  =  图象x轴大小  
//            ys  =  图象y轴大小  
//            quality  =  jpeg图象质量
  

VOID        SaveCurScreenJpg(LPCWSTR  pszFileName,  int  xs,  int  ys,  int  quality)  
{  
       HWND        hwnd  =  ::GetDesktopWindow();  
       HDC          hdc  =  GetWindowDC(NULL);  
       int          x  =  GetDeviceCaps(hdc,  HORZRES);  
       int          y  =  GetDeviceCaps(hdc,  VERTRES);  
       HBITMAP  hbmp  =  ::CreateCompatibleBitmap(hdc,  x,  y),  hold;  
       HDC          hmemdc  =  ::CreateCompatibleDC(hdc);  
       hold        =  (HBITMAP)::SelectObject(hmemdc,  hbmp);  
       BitBlt(hmemdc,  0,  0,  x,  y,  hdc,  0,  0,  SRCCOPY);  
       SelectObject(hmemdc,  hold);  
       {  
               Bitmap    bit(xs,  ys),  bit2(hbmp,  NULL);  
               Graphics        g(&bit);  
               g.ScaleTransform((float)xs/x,  (float)ys/y);  
               g.DrawImage(&bit2,  0,  0);  

               CLSID                          encoderClsid;  
               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/jpeg",  &encoderClsid);  
               bit.Save(pszFileName,  &encoderClsid,  &encoderParameters);  
       }  
       ::DeleteObject(hbmp);  
       ::DeleteObject(hmemdc);  
       return;  
}  
int  APIENTRY  WinMain(HINSTANCE  hInstance,  
                                         HINSTANCE  hPrevInstance,  
                                         LPSTR          lpCmdLine,  
                                         int              nCmdShow)  
{  
       GdiplusStartup(&gdiplusToken,  &gdiplusStartupInput,   NULL);  
       ::SaveCurScreenJpg(L".//cap.400.300.30.jpg",  400,  300,  30);  
       ::SaveCurScreenJpg(L".//cap.800.600.100.jpg",  800,  600,  100);  
       ::SaveCurScreenJpg(L".//cap.800.600.100.jpg",  640,  480,  50);  
       GdiplusShutdown(gdiplusToken);  

       return  0;  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值