我的IPicture已经取到了图,现在我想用它的SaveAsFile方法把图像保存到内存的二进制流中,再存入数据库,可是到了调用pIPicture->SaveAsFile(pStream,TRUE,&lLength);总是报内存错,请问如何正确使用能达到目的。

解决方案 »

  1.   

    void SaveBmp(HBITMAP hbmp, LPCTSTR sIconFileName)
    {
    if(hbmp==NULL || sIconFileName==NULL)
    return;
    //warning: this code snippet is not bullet proof.
    //do error check by yourself [masterz]
    PICTDESC picdesc;
    picdesc.cbSizeofstruct = sizeof(PICTDESC);
    picdesc.picType = PICTYPE_BITMAP ;    
    picdesc.bmp.hpal = NULL;
    picdesc.bmp.hbitmap = hbmp;
    IPicture* pPicture=NULL;
    OleCreatePictureIndirect(&picdesc, IID_IPicture, TRUE,(VOID**)&pPicture);
    LPSTREAM pStream;
    CreateStreamOnHGlobal(NULL,TRUE,&pStream);
    LONG size;
    HRESULT hr=pPicture->SaveAsFile(pStream,TRUE,&size);
    char pathbuf[1024];
    strcpy(pathbuf,sIconFileName);
    CFile iconfile;
    iconfile.Open(pathbuf, CFile::modeCreate|CFile::modeWrite);
    LARGE_INTEGER li;
    li.HighPart =0;
    li.LowPart =0;
    ULARGE_INTEGER ulnewpos;
    pStream->Seek( li,STREAM_SEEK_SET,&ulnewpos);
    ULONG uReadCount = 1;
    while(uReadCount>0)
    { pStream->Read(pathbuf,sizeof(pathbuf),&uReadCount);
    if(uReadCount>0)
    iconfile.Write(pathbuf,uReadCount);
    }
    pStream->Release();
    iconfile.Close();
    }
      

  2.   

    class CPicture {
    public:
    ........................
    operator IPicture*() {
    return m_spIPicture;
    }
    ........................
    protected:
    CComQIPtr<IPicture>m_spIPicture;  // ATL smart pointer to IPicture
    }问一下我有一个CPicture *pPicture,怎么才能得到他的m_spIPicture变量
      

  3.   

    为什么不直接保存到内存流
    去看COleStreamFile
      

  4.   

    为什么不直接保存到内存流
    去看COleStreamFile
      

  5.   

    用了OleLoadPicture(pstm, 0, FALSE,IID_IPicture, (void**)&m_spIPicture);
    其实还是先读到IStream* pstm里面了,不过是临时的
      

  6.   

    有谁知道得到一个IStream *pstrm内容的长度,并把它保存到一个BYTE*或者char*里吗
      

  7.   

    ……
    CreateStreamOnHGlobal
    Read&Write
      

  8.   

    首先谢谢各位的帮助
    BOOL CPicture::Load(IStream* pstm)
    {
    Free();
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, 0);
    CreateStreamOnHGlobal(hGlobal, TRUE, &m_pStream);  
    m_pStream = pstm;
    HRESULT hr = OleLoadPicture(m_pStream, 0, FALSE,
    IID_IPicture, (void**)&m_spIPicture);
    m_lSize = ::GlobalSize(hGlobal);
    ASSERT(SUCCEEDED(hr) && m_spIPicture); return TRUE;
    }
    我的代码,图片可以正常显示,但是m_lSize取到的是0,为何,哪有问题吗?我read也是"",失败呀,不知道哪有问题