cxImageUser

#pragma once
#include <string>
#include "ximage.h"
#include "ximagif.h"
#include "xiofile.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

#ifdef _DEBUG
#pragma comment(lib, "cximage//db//cximage.lib")
#pragma comment(lib, "cximage//db//jasper.lib")
#pragma comment(lib, "cximage//db//jbig.lib")
#pragma comment(lib, "cximage//db//jpeg.lib")
#pragma comment(lib, "cximage//db//libdcr.lib")
#pragma comment(lib, "cximage//db//libpsd.lib")
#pragma comment(lib, "cximage//db//mng.lib")
#pragma comment(lib, "cximage//db//png.lib")
#pragma comment(lib, "cximage//db//tiff.lib")
#pragma comment(lib, "cximage//db//zlib.lib")
#else
#pragma comment(lib, "cximage//cximage.lib") 
#pragma comment(lib, "cximage//jasper.lib")
#pragma comment(lib, "cximage//jbig.lib")
#pragma comment(lib, "cximage//jpeg.lib")
#pragma comment(lib, "cximage//libdcr.lib")
#pragma comment(lib, "cximage//libpsd.lib")
#pragma comment(lib, "cximage//mng.lib")
#pragma comment(lib, "cximage//png.lib")
#pragma comment(lib, "cximage//tiff.lib")
#pragma comment(lib, "cximage//zlib.lib")
#endif

class CxImageUser
{
public:
    CxImageUser();
    ~CxImageUser();

    static bool gif2jpg(const std::wstring& strSrcFile, const std::wstring& strDstFile);
    static bool png2jpg(const std::wstring& strSrcFile, const std::wstring& strDstFile);
    static bool ResizeGif(const std::wstring& strSrcFile, const std::wstring& strDstFile, const int& nWidth, const int& nHeight);
};
#include "cxImageUser.h"
#include "StdStrFile.h"

bool CxImageUser::gif2jpg(const std::wstring& strSrcFile, const std::wstring& strDstFile)
{
    CxImage  image; 
    //gif -> jpg 
    image.Load(strSrcFile.c_str(), CXIMAGE_SUPPORT_GIF); 
    if (image.IsValid())
    { 
        if(!image.IsGrayScale()) image.IncreaseBpp(24); 
        image.SetJpegQuality(80); 
        image.Save(strDstFile.c_str(),CXIMAGE_FORMAT_JPG); 
    }

    return true;
}

bool CxImageUser::png2jpg( const std::wstring& strSrcFile, const std::wstring& strDstFile )
{
    CxImage  image; 
    //png -> jpg 
    image.Load(strSrcFile.c_str(), CXIMAGE_FORMAT_PNG); 
    if (image.IsValid()){ 
        if(!image.IsGrayScale()) image.IncreaseBpp(24); 
        image.SetJpegQuality(80); 
        image.Save(strDstFile.c_str(),CXIMAGE_FORMAT_JPG); 
    }
    image.GetExifInfo();

    return true;
}

bool CxImageUser::ResizeGif(const std::wstring& strSrcFile, const std::wstring& strDstFile, const int& nWidth, const int& nHeight)
{
#if CXIMAGE_SUPPORT_DECODE && CXIMAGE_SUPPORT_ENCODE && CXIMAGE_SUPPORT_GIF
    CxImage img;
    img.Load(strSrcFile.c_str(), CXIMAGE_FORMAT_GIF);

    if (!img.IsValid())
    {
        return false;
    }

    //这样做只能保留第一帧
//     img.Resample(nWidth, nHeight);
//     img.Save(strDstFile.c_str(), CXIMAGE_FORMAT_GIF);

    int iNumFrames = img.GetNumFrames();
    CxImage** imgSave = new CxImage*[iNumFrames];

    for (int i = 0; i < iNumFrames; i++)
    {
        CxImage* newImage = new CxImage();
        newImage->SetFrame(i);
        newImage->Load(strSrcFile.c_str(), CXIMAGE_FORMAT_GIF);
        newImage->Resample(nWidth, nHeight);
        if (0 == newImage->GetNumColors())
        {
            imgSave[i]->DecreaseBpp(8, true);
        }
        imgSave[i] = newImage;
    }

    CxIOFile hFile;
    std::string Method = "wb";
    std::wstring  stempmd = CStdStr::s2ws(Method);
    LPCWSTR wMethod = stempmd.c_str();
    bool BFlag = hFile.Open(strDstFile.c_str(), wMethod);

    CxImageGIF multiimage;

    multiimage.SetLoops(-1);
    multiimage.SetFrameDelay(img.GetFrameDelay());
    multiimage.SetDisposalMethod(img.GetDisposalMethod());
    multiimage.Encode(&hFile, imgSave, iNumFrames, false, false);

    hFile.Close();

    //释放资源
    for (int i = 0; i < iNumFrames; ++i)
    {
        if (imgSave[i])
        {
            delete imgSave[i];
            imgSave[i] = nullptr;
        }
    }
    if (imgSave)
    {
        delete[] imgSave;
        imgSave = nullptr;
    }
    
#endif

    return true;
}
原文地址:https://www.cnblogs.com/autumoonchina/p/7065290.html