15,979
社区成员
发帖
与我相关
我的任务
分享
///////图片浏览器的初始化////////////////
//////////////////////////
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup( &m_gdiplusToken,&gdiplusStartupInput,NULL );
m_pImageList=new CImageList;
m_pImageList->Create(140,140,ILC_COLOR32|ILC_MASK,200,200);
m_picList.SetImageList(m_pImageList,TVSIL_NORMAL );
void Ctrain13_Dlg::OnBnClickedButton1()//按下按钮,进入文件夹窗口导入图片
{
OnShowPicture();
}
void Ctrain13_Dlg::OnShowPicture() //获得文件路径名并显示图片
{
char buf[MAX_PATH] = {0};
if (select_folder(buf))
{
m_strFileName=buf;
LoadImageToList(buf);
}
}
int select_folder(char path[MAX_PATH],char *title="请选择目录...")//选择文件夹
{
BROWSEINFO bi;
ITEMIDLIST *pidl;
bi.hwndOwner=NULL;
bi.pidlRoot=NULL;
bi.pszDisplayName=path;
bi.lpszTitle=title;
bi.ulFlags=BIF_EDITBOX;
bi.lpfn=NULL;
bi.lParam=0;
bi.iImage=0;
pidl=SHBrowseForFolder(&bi);
return (pidl && SHGetPathFromIDList(pidl,path));
}
BOOL Ctrain13_Dlg::LoadImageToList(CString strFilePathName)//将jpg文件加载到控件m_picList上
{
int m;
for(m=m_picList.GetItemCount();m>=0;m--)
{
m_picList.DeleteItem(m);
m_pImageList->Remove(m);
}
m_picList.SetRedraw(FALSE);
CFileFind ff;
CString szDir = strFilePathName;
CString strSize = _T("");
CString getextention;
if(szDir.Right(1) != "\\")
szDir += "\\";
szDir += "*.*";
BOOL res = ff.FindFile(szDir);
int i=0;
while(res)
{
res = ff.FindNextFile();
if(!ff.IsDirectory() && !ff.IsDots()) // 文件
{
// getextention=ff.Right(foundFileName.GetLength()-foundFileName.ReverseFind('.')-1);//获得文件的后缀名
CString strFileName = ff.GetFileName();
getextention=strFileName.Right(strFileName.GetLength()-strFileName.ReverseFind('.')-1);//获得文件的后缀名
//AfxMessageBox(strFileName);
if(getextention=="jpg"||getextention=="JPG")
{
m_strImageName = strFilePathName + _T("\\") +strFileName;
m_picList.InsertItem(i,strFileName,i);//插入名字
LoadBitmapToList(m_strImageName,i++);//读取图片
}
}
}
m_picList.SetRedraw(TRUE);
m_picList.Invalidate();
m_picList.UpdateWindow();
return true;
}
BOOL Ctrain13_Dlg::LoadBitmapToList(CString strFileName,int i)//使用GDI+加载jpg文件
{
Bitmap bmp(strFileName.AllocSysString());
int sourceWidth = 140;
int sourceHeight = 140; //获得图片宽度
Bitmap* pThumbnail =(Bitmap*)bmp.GetThumbnailImage(sourceWidth , sourceHeight , NULL, NULL); //设定缩略图的大小
HBITMAP hBmp;
pThumbnail->GetHBITMAP(Color(255,255,255),&hBmp );
CBitmap *pImage = CBitmap::FromHandle(hBmp); //转换成CBitmap格式位图
int a=m_pImageList->Add(pImage,RGB(255,255,255));
pImage->DeleteObject();
return true;
}
void Ctrain13_Dlg::OnShowPicture() //获得文件路径名
{
char buf[MAX_PATH] = {0};
if (select_folder(buf))
{
//AfxMessageBox(buf);
m_strFileName=buf;
LoadImageToList(buf);
//UpdateData(FALSE);
}
// GetDlgItem(IDC_EDIT_PATH)->SetWindowText(m_Path);
}
HRESULT ShowPic(CDC *pDC,CString lpstrFile, int nScrWidth, int nScrHeight)
{
IPicture *pPic;
IStream *pStm;
BOOL bResult;
HANDLE hFile=NULL;
DWORD dwFileSize,dwByteRead;
//打开硬盘中的图形文件
hFile=CreateFile(lpstrFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile!=INVALID_HANDLE_VALUE)
{
dwFileSize=GetFileSize(hFile,NULL);//获取文件字节数
if(dwFileSize==0xFFFFFFFF)
{
CloseHandle(hFile);
return E_FAIL;
}
}
else
{
return E_FAIL;
}
//分配全局存储空间
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
if(hGlobal==NULL)
return E_FAIL;
LPVOID pvData =GlobalLock(hGlobal);
if(pvData==NULL)//锁定分配内存块
{
GlobalFree(hGlobal);
return E_FAIL;
}
ReadFile(hFile,pvData,dwFileSize,&dwByteRead,NULL);//把文件读入内存缓冲区
CloseHandle(hFile);//关闭打开的文件
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal,TRUE,&pStm);
//装入图形文件
bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic);
GlobalFree(hGlobal);
if(FAILED(bResult))
return E_FAIL;
long hmWidth;//图片的真实宽度
long hmHeight;//图片的真实高度
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//将图形输出到屏幕上(有点像BitBlt)
bResult=pPic->Render(pDC->m_hDC,0,0,nScrWidth,nScrHeight,0,hmHeight,hmWidth,-hmHeight,NULL);
pPic->Release();
if(SUCCEEDED(bResult))
{
return S_OK;
}
else
{
return E_FAIL;
}
}
void CXXXX::OnPaint()
{
CPaintDC dc(this);
ShowPic(&dc,"C:\\a.jpg",1024, 768);
}