如何将HBITMAP指定的图片保存成文件?

解决方案 »

  1.   

    CImage.Attach(HBITMAP);
    CImage.save(filepathname);
      

  2.   

    #include  <windows.h> 
    #ifndef ULONG_PTR
    #define ULONG_PTR unsigned long*
    #endif
    #include <gdiplus.h>
    #include  <stdio.h>  using  namespace  Gdiplus;  #pragma comment(lib, "GdiPlus.lib")  
    #pragma comment(lib, "gdi32.lib")
    #pragma comment(lib, "user32.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;  
    }
      

  3.   

    注意必须添加gdi+库,可以到microsoft网站下载