VC实现:bmp转jpg、jpg转bmp、截屏保存jpg

    技术2022-06-27  121

     

     

     

     

    1.下载(GDI+ for VC6.0 SDK)GDIPlus文件将其中的Includes和Lib中的文件拷到vc目录下的Includes和Lib文件夹中.   下载地址:http://www.codeguru.com/code/legacy/gdi/GDIPlus.zip(引用他人的)

    2.在你将要使用GDI+的工程中,完成初始化工作:在StdAfx.h中加入

    #define ULONG_PTR unsigned long#include <Gdiplus.h>using namespace Gdiplus;

     

    3.在C×App头文件中加入ULONG_PTR        m_gdiplusToken;

    4.在 BOOL C×App::InitInstance() 中添加

    GdiplusStartupInput     m_gdiplusStartupInput;GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);

    5.在int CXApp::ExitInstance()加入(这个方法需要自己加入 在ClassWizard 中,注意在ClassName中选择CXApp,Messages列表框中选择ExitInstance)

    Gdiplus::GdiplusShutdown(m_gdiplusToken);

    6.在Project->stting->Link->Object/libary中加入gdiplus.lib此时GDI+设置成功。

    需要GDI+,如果没有请看:vc6.0 下设置GDI+

    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)   {       UINT num = 0;                     // number of image encoders       UINT size = 0;                   // size of the image encoder array in bytes       ImageCodecInfo* pImageCodecInfo = NULL;       GetImageEncodersSize(&num, &size);       if(size == 0)           return -1;     //   Failure           pImageCodecInfo = (ImageCodecInfo*)(malloc(size));       if(pImageCodecInfo == NULL)           return -1;     //   Failure           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;     //   Success           }               }       free(pImageCodecInfo);       return -1;     //   Failure   }

     

    /****************BMP转JPG*********用法示例**************************

    Bitmap newbitmap(L"d://d.bmp");//加载BMPconst unsigned short *pFileName=L"d://new.jpg";//保存路径SaveFile(&newbitmap,pFileName );

    ************************************************************/

    void SaveFile(Bitmap* pImage, const wchar_t* pFileName)//{    EncoderParameters encoderParameters;    CLSID jpgClsid; GetEncoderClsid(L"image/jpeg", &jpgClsid);    encoderParameters.Count = 1;    encoderParameters.Parameter[0].Guid = EncoderQuality;    encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;    encoderParameters.Parameter[0].NumberOfValues = 1;

        // Save the image as a JPEG with quality level 100.    ULONG             quality;    quality = 100;    encoderParameters.Parameter[0].Value = &quality;    Status status = pImage->Save(pFileName, &jpgClsid, &encoderParameters);    if (status != Ok)     {        wprintf(L"%d Attempt to save %s failed./n", status, pFileName);    }}

    // 将当前屏幕保存成为jpg图片       // 参数   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;   }  

    HBITMAP   ReturnHBITMAP(CString   FileName)//FileName可能是bmp、dib、png、gif、jpeg/jpg、tiff、emf等文件的文件名 {           Bitmap   tempBmp(FileName.AllocSysString())   ;         Color       backColor;               HBITMAP       HBitmap;           tempBmp.GetHBITMAP(backColor,&HBitmap);         return   HBitmap;

    }


    最新回复(0)