基于GDI图片压缩算法

基于GDI图片压缩算法

头文件

#include "stdafx.h"
#include <Windows.h>
#include <GdiPlus.h>
#pragma comment( lib, "GdiPlus.lib" )
using namespace Gdiplus;

获取设备编码格式

bool GetEncoderClsid(const WCHAR* pszFormat, CLSID* pClsid)
{
    UINT  unNum = 0;          // number of image encoders
    UINT  unSize = 0;         // size of the image encoder array in bytes

    ImageCodecInfo* pImageCodecInfo = NULL;

    // How many encoders are there?
    // How big (in bytes) is the array of all ImageCodecInfo objects?
    GetImageEncodersSize( &unNum, &unSize );
    if ( 0 == unSize ) {
        return false;  // Failure
    }

    // Create a buffer large enough to hold the array of ImageCodecInfo
    // objects that will be returned by GetImageEncoders.
    pImageCodecInfo = (ImageCodecInfo*)( malloc(unSize) );
    if ( !pImageCodecInfo ) {
        return false;  // Failure
    }

    // GetImageEncoders creates an array of ImageCodecInfo objects
    // and copies that array into a previously allocated buffer. 
    // The third argument, imageCodecInfos, is a pointer to that buffer. 
    GetImageEncoders( unNum, unSize, pImageCodecInfo );

    for ( UINT j = 0; j < unNum; ++j ) {
        if ( wcscmp( pImageCodecInfo[j].MimeType, pszFormat ) == 0 ) {
            *pClsid = pImageCodecInfo[j].Clsid;
            int size = pImageCodecInfo[j].SigSize;
            free(pImageCodecInfo);
            pImageCodecInfo = NULL;
            return true;  // Success
        }    
    }

    free( pImageCodecInfo );
    pImageCodecInfo = NULL;
    return false;  // Failure
}

尺寸压缩

bool CompressImagePixel( 
    const WCHAR* pszOriFilePath, 
    const WCHAR* pszDestFilePah, 
    UINT ulNewHeigth, 
    UINT ulNewWidth )
{
    // Initialize GDI+.
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    Status stat = GenericError;
    stat = GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL );
    if ( Ok != stat ) {
        return false;
    }
    // 重置状态
    stat = GenericError;

    // Get an image from the disk.
    Image* pImage = new Image(pszOriFilePath);

    do {
        if ( NULL == pImage ) {
            break;
        }

        // 获取长宽
        UINT unOriHeight = pImage->GetHeight();
        UINT unOriWidth = pImage->GetWidth();

        do {
            CLSID encoderClsid;
            if ( unOriWidth < 1 || unOriHeight < 1 ) {
                break;
            }

            // Get the CLSID of the JPEG encoder.
            if ( !GetEncoderClsid(L"image/jpeg", &encoderClsid) ) {
                break;
            }

            REAL fSrcX = 0.0f;
            REAL fSrcY = 0.0f;
            REAL fSrcWidth = (REAL) unOriWidth;
            REAL fSrcHeight = (REAL) unOriHeight ;
            RectF RectDest( 0.0f, 0.0f, (REAL)ulNewWidth, (REAL)ulNewHeigth);

            Bitmap* pTempBitmap = new Bitmap( ulNewWidth, ulNewHeigth );
            Graphics* graphics = NULL;

            do {
                if ( !pTempBitmap ) {
                    break;
                }

                graphics = Graphics::FromImage( pTempBitmap );
                if ( !graphics ) {
                    break;
                }

                stat = graphics->SetInterpolationMode(Gdiplus::InterpolationModeHighQuality);
                if ( Ok != stat ) {
                    break;
                }

                stat = graphics->SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
                if ( Ok != stat ) {
                    break;
                }

                stat = graphics->DrawImage( pImage, RectDest, fSrcX, fSrcY, fSrcWidth, fSrcHeight,
                    UnitPixel, NULL, NULL, NULL);
                if ( Ok != stat ) {
                    break;
                }

                stat = pTempBitmap->Save( pszDestFilePah, &encoderClsid, NULL );
                if ( Ok != stat ) {
                    break;
                }

            } while(0);

            if ( NULL != graphics ) {
                delete graphics;
                graphics = NULL;
            }

            if ( NULL != pTempBitmap ) {
                delete pTempBitmap;
                pTempBitmap = NULL;
            }
        } while(0);
    } while (0);

    if ( pImage ) {
        delete pImage;
        pImage = NULL;
    }

    GdiplusShutdown(gdiplusToken);

    return ( ( Ok == stat ) ? true : false );
}

质量压缩

bool CompressImageQuality( 
    const WCHAR* pszOriFilePath, 
    const WCHAR* pszDestFilePah,
    ULONG quality )
{
    // copy from http://msdn.microsoft.com/en-us/library/ms533844(v=VS.85).aspx
    // Initialize GDI+.
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    Status stat = GenericError;
    stat = GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL );
    if ( Ok != stat ) {
        return false;
    }

    // 重置状态
    stat = GenericError;

    // Get an image from the disk.
    Image* pImage = new Image(pszOriFilePath);
    do {
        if ( NULL == pImage ) {
            break;
        }

        // 获取长宽
        UINT ulHeight = pImage->GetHeight();
        UINT ulWidth = pImage->GetWidth();
        if ( ulWidth < 1 || ulHeight < 1 ) {
            break;
        }

        // Get the CLSID of the JPEG encoder.
        CLSID encoderClsid;
        if ( !GetEncoderClsid(L"image/jpeg", &encoderClsid) ) {
            break;
        }

        // The one EncoderParameter object has an array of values.
        // In this case, there is only one value (of type ULONG)
        // in the array. We will let this value vary from 0 to 100.
        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;
        stat = pImage->Save(pszDestFilePah, &encoderClsid, &encoderParameters);
    } while(0);

    if ( pImage ) {
        delete pImage;
        pImage = NULL;
    }

    GdiplusShutdown(gdiplusToken);

    return ( ( stat == Ok ) ? true : false );
}

主函数入口

int _tmain(int argc, _TCHAR* argv[])
{
    CompressImagePixel( L"1Src.jpg", L"1Des.jpg",  100, 100 );
    CompressImageQuality( L"src.png", L"Des.png", 80);
    return 0;
}

参考博客: 一种使用GDI+对图片尺寸和质量的压缩方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨田哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值