下边是一个显示jpeg图的函数,buf是jpeg图的数据,nLength是数据长度.现RES=pStm->Write(buf,BUFFER_SIZE,&cbWritten);时res总为0,说明buf没有写进pStm中去.请高手帮忙看一下怎没回事.谢谢void CTestJJDlg::DisplayJpeg(char* buf,int nLength)
{
CString m_Path;
CFile m_file;
IStream *pStm;  
CFileStatus fstatus;  
CDC* pDC=this->GetDC();
HRESULT RES;
int BUFFER_SIZE=nLength;//sizeof(buf);
HGLOBAL hMem = GlobalAlloc(GMEM_FIXED, BUFFER_SIZE);
if (hMem == NULL) return;
CreateStreamOnHGlobal(hMem , FALSE, &pStm );
if (pStm == NULL) 
{
GlobalFree(hMem );
return;
}

// CString ---> IStream 
ULONG cbWritten;
RES=pStm->Write(buf,BUFFER_SIZE,&cbWritten);//str.GetLength()pBuffer
IPicture *pPic;
if(SUCCEEDED(OleLoadPicture(pStm,BUFFER_SIZE,TRUE,IID_IPicture,(LPVOID*)&pPic)))

OLE_XSIZE_HIMETRIC hmWidth;  
OLE_YSIZE_HIMETRIC hmHeight;  
pPic->get_Width(&hmWidth);  
pPic->get_Height(&hmHeight);  
double fX,fY;  
fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);  
fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);  
if(FAILED(pPic->Render(*pDC,65,40,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))  
AfxMessageBox("图像失败!");  
pPic->Release();  
}  
else  
AfxMessageBox("装载图像失败!");  
}

解决方案 »

  1.   

    用CreateStreamOnHGlobal比较简单
    HRESULT CommonDlgBase::get_picture(LPCTSTR restype,UINT resid,IPicture** pic)
    {
    HRSRC hResLoad = NULL; // handle to loaded resource
    HMODULE hExe = NULL; // handle to existing .EXE file
    HRSRC hRes = NULL; // handle/ptr. to res. info. in hExe
    LPVOID lpResLock = NULL; // pointer to resource datahRes = FindResource(hExe, MAKEINTRESOURCE(resid), restype);
    if (hRes == NULL)
    {
    return E_FAIL;
    }
    hResLoad = (HRSRC)LoadResource(hExe, hRes);
    if (hResLoad == NULL)
    {
    return E_FAIL;
    }
    lpResLock = LockResource(hResLoad);
    if (lpResLock == NULL)
    {
    return E_FAIL;
    }
    int fileSize = SizeofResource(hExe, hRes);
    //Verbose(_T("SizeofResource return %d"),fileSize);
    CComPtr<IStream> pStream = NULL;
    CreateStreamOnHGlobal(NULL,TRUE,&pStream);
    if(pStream)
    {
    ULONG uwrite = 0;
    pStream->Write(lpResLock, fileSize,&uwrite);
    if(uwrite==fileSize)
    {
    LARGE_INTEGER li;
    li.u.HighPart=0;
    li.u.LowPart =0;
    pStream->Seek( li,STREAM_SEEK_SET,0);
    HRESULT hr=OleLoadPicture(pStream,0,TRUE,IID_IPicture,(LPVOID*)pic);
    return hr;
    }
    }
    else
    return E_FAIL;
    return 0;
    }
    BOOL AboutMe::OnEraseBkgnd(CDC* pDC)
    {
    IPicture* pPicture = m_pPicture;
    if(pPicture)
    {
    long hmWidth,hmHeight; // HIMETRIC units
    pPicture->get_Width(&hmWidth);
    pPicture->get_Height(&hmHeight);
    CSize sz(hmWidth,hmHeight);
    pDC->HIMETRICtoDP(&sz);
    CDC dcmem;
    dcmem.CreateCompatibleDC(pDC);
    CBitmap membmp;
    CRect clientrc;
    GetClientRect(&clientrc);
    membmp.CreateCompatibleBitmap(pDC,clientrc.Width(),clientrc.Height());
    CBitmap* pold=dcmem.SelectObject(&membmp);
    pPicture->Render(dcmem.m_hDC,0,0,sz.cx,sz.cy,0,hmHeight,hmWidth,-hmHeight,NULL);
    pDC->BitBlt(0,0,clientrc.Width(),clientrc.Height(),&dcmem,0,0,SRCCOPY);
    dcmem.SelectObject(pold);
    membmp.DeleteObject();
    dcmem.DeleteDC();
    return TRUE;
    }
    return CommonDlgBase::OnEraseBkgnd(pDC);
    }
      

  2.   

    void writecstring(CString s,IStream* ps)
    {
    CComBSTR cbs((LPCSTR)s);
    cbs.WriteToStream(ps);
    }CString readcstring(IStream* ps)
    {
    CComBSTR cbs;
    cbs.ReadFromStream(ps);
    return (LPCSTR)(_bstr_t(cbs.Detach( ),false));
    }
      

  3.   

    你分配内存之后直接用GlobalLock和memcpy往里面写
    Write可能需要超出指定大小的内存空间,而你又禁止了重新分配内存。
      

  4.   

    嗷,这几天有点事,来迟了,对tianya320 () 说:RES=pStm->Write(buf,BUFFER_SIZE,&cbWritten);时res总为0,说明buf没有写进pStm中去?
    Write()方法返回类型是HRESULT,返回0就是S_OK,说明write()本身调用是成功的你之所以不成功,是因为 if(SUCCEEDED(OleLoadPicture(pStm,BUFFER_SIZE,TRUE,IID_IPicture,(LPVOID*)&pPic)))这个函数(OleLoadPicture)调用失败,
    你可能在上一篇帖子中忘记了一个关键的函数seek(),你可以再看一下我将// IStream -->CString 段代码(http://community.csdn.net/Expert/topic/3835/3835774.xml?temp=.2909357)中有一段
              ULARGE_INTEGER    pSeek;
     LARGE_INTEGER  dlibMove ={ 0 } ;
     pStream->Seek(dlibMove,STREAM_SEEK_SET ,&pSeek);所以这里OleLoadPicture()会失败,是因为在你RES=pStm->Write(buf,BUFFER_SIZE,&cbWritten);//str.GetLength()pBuffer之后,stream的指针也指到stream的末尾了,如果你要读stream的内容,需要把stream的指针
    重置到头部,所以你只要在Write()之后、OleLoadPicture()之前加上上面3行代码,就ok了
      

  5.   

    谢谢masterz,jiangsheng,jiangsheng三位,小弟献上薄利每人二十分.结贴
      

  6.   

    sorry.谢谢masterz,jiangsheng,:fuzb(fufu) 三位,小弟献上薄利每人二十分.结贴