• 使用IStream和GDI+在内存中实现图像格式转换


    首先,在StdAfx.h中静态调用diplus.lib,即由编译系统完成对DLL的加载,应用程序结束时卸载DLL的编码。如下

    #ifndef ULONG_PTR
    #define ULONG_PTR unsigned long*
    #include "GdiPlus.h"
    using namespace Gdiplus;
    #pragma comment(lib, "gdiplus.lib")
    #endif

    在类的头文件中定义,以下成员变量,用来初始化GDI+的使用和结束使用。

    GdiplusStartupInput m_gdiplusStartupInput; 
    ULONG_PTR m_gdiplusToken;

    然后在OnCreate()函数中加入初始化GDI+的函数:

    GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);

    在OnDestroy()函数中加入结束GDI+使用的函数:
    GdiplusShutdown(m_gdiplusToken);


    定义转换函数:BOOL MBmpToMImage(CMemFile& cbfBmp, CMemFile& cbfImage, CString strType)

    其中:
    CMemFile& cbfBmp表示原位图文件;
    CMemFile& cbfImage表示转换后的图形文件;
    CString strType表示转换的图片类型。

    该函数中主要的处理为以下几步:
            将原位图文件转换为IStream
             定义Image类实例,并使用第1步获得的IStream初始化
            获取转换的图片类型的CLSID
            将Image以转换的图片类型保存到IStream中
            将IStream转换为CMemFile内存文件(也可为CFile)
            详细代码如下:

    BOOL MBmpToMImage(CMemFile& cbfBmp, CMemFile& cbfImage, CString strType)
    {
    int iBmpSize = cbfBmp.GetLength();
    HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
    if (hMemBmp == NULL) return FALSE;
    IStream* pStmBmp = NULL;
    CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
    if (pStmBmp == NULL) 
    {
       GlobalFree(hMemBmp);
       return FALSE;
    }
    BYTE* pbyBmp = (BYTE *)GlobalLock(hMemBmp);
    cbfBmp.SeekToBegin();
    cbfBmp.Read(pbyBmp, iBmpSize);

    Image* imImage = NULL;
    imImage = Image::FromStream(pStmBmp, FALSE);
    if (imImage == NULL) 
    {
       GlobalUnlock(hMemBmp);
       GlobalFree(hMemBmp);
       return FALSE;
    }
    USES_CONVERSION;
    CLSID clImageClsid;
    GetImageCLSID(A2W("image/"+strType.GetBuffer(0)), &clImageClsid);

    HGLOBAL hMemImage = GlobalAlloc(GMEM_MOVEABLE, 0);
    if (hMemImage == NULL)
    {
       pStmBmp->Release();
       GlobalUnlock(hMemBmp);
       GlobalFree(hMemBmp);
       if (imImage != NULL) delete imImage;
       return FALSE;
    }
    IStream* pStmImage = NULL;
    CreateStreamOnHGlobal(hMemImage, TRUE, &pStmImage);
    if (pStmImage == NULL)
    {
       pStmBmp->Release();
       GlobalUnlock(hMemBmp);
       GlobalFree(hMemBmp);
       GlobalFree(hMemImage);
       if (imImage != NULL) delete imImage
       return FALSE;

    imImage->Save(pStmImage, &clJpgClsid);
    if (pStmImage == NULL) 
    {
       pStmBmp->Release();
       pStmImage>Release();
       GlobalUnlock(hMemBmp);
       GlobalFree(hMemBmp);
       GlobalFree(hMemImage;
       if (imImage != NULL) delete imImage;
       return FALSE;
    }
    LARGE_INTEGER liBegin = {0};
    pStmImage->Seek(liBegin, STREAM_SEEK_SET, NULL);
    BYTE* pbyImage = (BYTE *)GlobalLock(hMemImage);
    cbfImage.SeekToBegin();
    cbfImage.Write(pbyImage, GlobalSize(hMemImage));

    if (imImage != NULL) delete imImage;
    pStmBmp->Release();
    pStmImage->Release();
    GlobalUnlock(hMemBmp);
    GlobalUnlock(hMemImage);
    GlobalFree(hMemBmp);
    GlobalFree(hMemImage);
    return TRUE;
    }

    IStream是COM中的接口,真是搞不懂这样也可以~~~

  • 相关阅读:
    基础数据类型之字符串str
    python编码基础知识
    python逻辑运算之and、or
    Django中消息中间键和form组件的运用
    Django中 cookies and session的使用
    JavaScript 正则制表符,单词边界,去空格
    paramiko堡垒机、线程及锁
    0911 Socket网络编程
    os.system和os.popen
    类高级方法、反射、异常、动态导入模块
  • 原文地址:https://www.cnblogs.com/myitm/p/2145437.html
Copyright © 2020-2023  润新知