IStream* IImage *终于搞定了!
感觉还是不够细心,没有看IImage 接口的源码,非要到走投无路时才去瞄一下,图形图象确实要细心,总算搞定了,这个周末可以休息下了,看了多普达S1精英版好长时间了,周末去把它买了!谗了我好久了。
HBITMAP CImageLoader::loadImageFile( CString &strFileName)
{
int n_start=0,n_end=0;
GetPos(n_start,n_end,strFileName);
CString m_filename(L"1.data");//文件名
CFile m_file(m_filename,CFile::modeRead );
m_file.Seek(n_start,CFile::begin);
//获取文件长度
DWORD m_filelen =n_end-n_start;//m_file.GetLength()
CString str=L"^-^";
str.Format(L"%d",m_filelen);
AfxMessageBox(str);
//在堆上分配空间
HGLOBAL m_hglobal = GlobalAlloc(GMEM_MOVEABLE,m_filelen);
LPVOID pvdata = NULL;
//锁定堆空间,获取指向堆空间的指针
pvdata = GlobalLock(m_hglobal);
//将文件数据读区到堆中
m_file.Read(pvdata,m_filelen);
IStream* m_stream;
GlobalUnlock(m_hglobal);
//在堆中创建流对象
CreateStreamOnHGlobal(m_hglobal,TRUE,&m_stream);
IImagingFactory *pImgFactory =NULL;
IImage *pImage =NULL;
CoInitializeEx(NULL,COINIT_MULTITHREADED);
HBITMAP hResult =0;
if (SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IImagingFactory,
(void **)&pImgFactory)))
{
ImageInfo imageInfo;
//直接将流数据传进去
if(SUCCEEDED(pImgFactory->CreateImageFromStream(m_stream,&pImage))
// if(SUCCEEDED(pImgFactory->CreateImageFromFile(strFileName,&pImage))
&& SUCCEEDED(pImage->GetImageInfo(&imageInfo)))
{
CWindowDC dc(0);
CDC dcBitmap;
dcBitmap.CreateCompatibleDC(&dc);
hResult =CreateCompatibleBitmap(dc.GetSafeHdc(),imageInfo.Width,imageInfo.Height);
if(hResult){
HGDIOBJ hOldBitmap = dcBitmap.SelectObject(hResult);
pImage->Draw(dcBitmap.GetSafeHdc(),CRect(0,0,imageInfo.Width,imageInfo.Height),NULL);
dcBitmap.SelectObject(hOldBitmap);
}
pImage->Release();
}
pImgFactory->Release();
}
CoUninitialize();
return hResult;
}