我利用下列代码实现将一个内存文件CMemFile(bmp)转为jpeg格式,并将其写入一个文件(CFile),编译倒是通过了,但结果未能将信息写入CFile文件,CFile文件为空。下面是我的代码,请高手不吝赐教!
BOOL CGoClientDlg::MBmpToMImage(CMemFile& cbfBmp, CFile& cbfImage)
{
//MessageBox("Hello!");
int iBmpSize = cbfBmp.GetLength();
  if (iBmpSize!=0)   MessageBox("Right");
else   MessageBox("False");  
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);
GetCodecClsid(L"image/jpeg", &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, &clImageClsid);
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;
}

解决方案 »

  1.   

    参考
    http://www.vckbase.com/document/viewdoc/?id=1177  
      使用GDI+在内存中转换图片类型肯定能帮到你的
      

  2.   

    使用GDI+在内存中转换图片类型作者:卢伟  微软新推出的GDI+功能强大,本文仅对图片转换加以讨论,不足之处请大家指出,本人QQ:394777271。图片类型的转换支持:bmp、dib、png、gif、jpeg/jpg、tiff、emf等。以下是详细步骤。  首先,在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;
    }
      

  3.   

    我正是参照他的这段程序的,但结果就是有问题啊,AntonlioX(做人要厚道):你试过这段程序?成功了?
      

  4.   

    //Bitmap * newBitmap = new Bitmap(filename);
    //保存图像
    CLSID jpgClsid;
    GetEncoderClsid(L"image/jpeg", &jpgClsid);
    newBitmap->Save(L"c:\\01.jpg",&jpgClsid,NULL);/******************************************************/
    /*CLSID 获取函数定义                                  */
    /******************************************************/
    BOOL GetEncoderClsid(const WCHAR* format, CLSID* pCLSID)
    {
    UINT num = 0;
    UINT size = 0;ImageCodecInfo* pImageCodecInfo = NULL;
    GetImageEncodersSize(&num, &size);
    if(size == 0){
    return FALSE;
    }
    pImageCodecInfo = (ImageCodecInfo *)(malloc(size));
    if(pImageCodecInfo == NULL)
    return FALSE;
    GetImageEncoders(num, size, pImageCodecInfo); // Find for the support of format for image in the windows
    for(UINT i = 0; i < num; ++i)

    //MimeType: Depiction for the program image  
    if( wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
    {  
    *pCLSID = pImageCodecInfo[i].Clsid; 
    free(pImageCodecInfo); 
    return TRUE;  
    }  
    }  
    free(pImageCodecInfo);  
    return FALSE; 
    }
      

  5.   

    park2305():你的代码中newBitmap是什么类型的,是Bitmap?可是Bitmap类型没有Save这个函数啊!
      

  6.   

    Image类中有这个方法,Bitmap是从Image中继承而来,所以是可以这么用的!