请教各位,我可以以二进制流的方式打开一张jpg图片显示到picture 控件上,还可以遍历指定文件夹X下的10张图片,得到保存了这些图片路径的列表(CStringlist)。现在我想要在按下BUTTON 按钮之后,循环显示这个文件夹下的10张图片,当然还是以二进制流的方式显示。是不是可以通过读一个列表里面的路径再显示一张图片这样的方式来循环显示这10张图片呢?或者有什么别的方法来连续显示图片吗? 可以提供思路,当然有代码参考就更好了。
(1) 以二进制流显示jpg图片代码:
CFile file;
file.Open(_T("G:\\img.jpgt"), CFile::modeRead);
DWORD dwDataLen = file.GetLength(); //获取文件的长度
for (int i = 0; i < dwDataLen; i++)
{
BYTE *buf = new BYTE[dwDataLen]; //创建数组,用来保存图像的数据
memset(buf, 0x00, dwDataLen); //初始化数组
file.Read(buf, dwDataLen); //图像文件的数据保存在数组中
file.Close(); //关闭文件 CImage image;
COleStreamFile osf;
osf.CreateMemoryStream(NULL);
osf.Write((void*)buf, dwDataLen);
osf.SeekToBegin();
image.Load(osf.GetStream()); CRect rect; //定义矩形类
CWnd *pWnd = GetDlgItem(IDC_PICTURE); //获得pictrue控件窗口的指针
pWnd->GetClientRect(&rect); //获得pictrue控件所在的矩形区域
CDC *pDC = pWnd->GetDC(); //获得pictrue控件的DC
pDC->SetStretchBltMode(COLORONCOLOR); //进行图像缩放处理避免图像显示时失真
image.Draw(pDC->m_hDC, rect); //将图片画到Picture控件表示的矩形区域
ReleaseDC(pDC); //释放picture控件的DC
(2) 遍历图片中的文件
void SRAECH(CString strPathName)
{
if (strPathName.IsEmpty())
{
AfxMessageBox(_T("请选择图片所在的文件夹!"));
}
strPathName += "//*.*";
CFileFind finder;
CStringList filelist;//文件列表
long filenum = 0;//长度
BOOL bResult = finder.FindFile(strPathName); if (!bResult)
{
AfxMessageBox(_T("未找到图像文件!"));
return ;
} while (bResult)
{
bResult = finder.FindNextFile();
//判断是否是目录或者是隐藏文件,又或者是".."和".",如果是,进入下一轮循环
if (finder.IsDirectory() || finder.IsDots() || finder.IsHidden())
continue;
else//如果是文件
{
CString str;
//得到文件名
str = finder.GetFileName();
//得到文件后缀名
int nLen = str.GetLength() - finder.GetFileTitle().GetLength();
str = str.Right(nLen);
//判断文件格式,如果是图像文件,将其完整的路径保存在列表中
//if (str == ".jpg" || str == ".png" || str == ".bmp" || str == ".gif")
if (str == ".jpg") //只找JPG图片
filelist.AddTail(finder.GetFilePath());
}
}
finder.Close();
filenum = filelist.GetCount();
}
(1) 以二进制流显示jpg图片代码:
CFile file;
file.Open(_T("G:\\img.jpgt"), CFile::modeRead);
DWORD dwDataLen = file.GetLength(); //获取文件的长度
for (int i = 0; i < dwDataLen; i++)
{
BYTE *buf = new BYTE[dwDataLen]; //创建数组,用来保存图像的数据
memset(buf, 0x00, dwDataLen); //初始化数组
file.Read(buf, dwDataLen); //图像文件的数据保存在数组中
file.Close(); //关闭文件 CImage image;
COleStreamFile osf;
osf.CreateMemoryStream(NULL);
osf.Write((void*)buf, dwDataLen);
osf.SeekToBegin();
image.Load(osf.GetStream()); CRect rect; //定义矩形类
CWnd *pWnd = GetDlgItem(IDC_PICTURE); //获得pictrue控件窗口的指针
pWnd->GetClientRect(&rect); //获得pictrue控件所在的矩形区域
CDC *pDC = pWnd->GetDC(); //获得pictrue控件的DC
pDC->SetStretchBltMode(COLORONCOLOR); //进行图像缩放处理避免图像显示时失真
image.Draw(pDC->m_hDC, rect); //将图片画到Picture控件表示的矩形区域
ReleaseDC(pDC); //释放picture控件的DC
(2) 遍历图片中的文件
void SRAECH(CString strPathName)
{
if (strPathName.IsEmpty())
{
AfxMessageBox(_T("请选择图片所在的文件夹!"));
}
strPathName += "//*.*";
CFileFind finder;
CStringList filelist;//文件列表
long filenum = 0;//长度
BOOL bResult = finder.FindFile(strPathName); if (!bResult)
{
AfxMessageBox(_T("未找到图像文件!"));
return ;
} while (bResult)
{
bResult = finder.FindNextFile();
//判断是否是目录或者是隐藏文件,又或者是".."和".",如果是,进入下一轮循环
if (finder.IsDirectory() || finder.IsDots() || finder.IsHidden())
continue;
else//如果是文件
{
CString str;
//得到文件名
str = finder.GetFileName();
//得到文件后缀名
int nLen = str.GetLength() - finder.GetFileTitle().GetLength();
str = str.Right(nLen);
//判断文件格式,如果是图像文件,将其完整的路径保存在列表中
//if (str == ".jpg" || str == ".png" || str == ".bmp" || str == ".gif")
if (str == ".jpg") //只找JPG图片
filelist.AddTail(finder.GetFilePath());
}
}
finder.Close();
filenum = filelist.GetCount();
}
当然用容器,vector<string>把图片全路径都push进去,召唤定时器settimer int i=0;当前显示图片
逻辑就是取得容器下标i的路径,显示,然后i++ i+1>=容器.size() 则i=0 不断循环了。