从程序外部直接加载(png, jif, bmp,jpg 等)多种文件格式的文件并显示
HBITMAP LoadAllPic(CString strFileName)
{
IPicture* p=NULL;
IStream* s=NULL;
HGLOBAL hG;
void* pp;
FILE* fp;
fp = fopen(strFileName,"rb");//打开文件
if (!fp)
return NULL;
fseek(fp,0,SEEK_END);
int fs = ftell(fp);
fseek(fp,0,SEEK_SET);
hG = GlobalAlloc(GPTR,fs);
if (!hG)
{
fclose(fp);
return NULL;
}
pp = (void*)hG;
fread(pp,1,fs,fp);
fclose(fp);
CreateStreamOnHGlobal(hG,false,&s);
if (!s)
{
GlobalFree(hG);
return NULL;
}
OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);
if (!p)
{
s->Release();
GlobalFree(hG);
return NULL;
}
s->Release();
GlobalFree(hG);
HBITMAP hB = 0;
p->get_Handle((unsigned int*)&hB);
// Copy the image.Necessary, because upon p's release,
// the handle is destroyed.
HBITMAP hBB = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,LR_COPYRETURNORG );
p->Release();
return hBB;
}
// 在CStatic上显示
CStatic *pStatic = (CStatic *)GetDlgItem(IDC_STATIC);
pStatic->ModifyStyle(0xF,SS_BITMAP);
pStatic->SetBitmap(hBitmap);