基于C++的图片文件路径设置及扫描规定格式文件

ImageClass.h文件

typedef struct FileMessage
{
 CString filename[100];    //文件信息
 _In_ int filenum  ;    //文件数目
}filemessage;

class ImageObject : public CObject
{
public:
ImageObject();
~ImageObject();
public:
         //设置文件所在路径
 void SetRetrievalPath(CString &strfile1);     
            //扫描当前目录中的图像个数并获得文件名            
FileMessage GetFileNameAndNumber(const CString &strfile, const CString &form);                                             
};


ImageClass.CPP文件

#include "ImageClass.h"
#include "Dib.h"
#include "atlbase.h"
#include "atlimage.h"  
#include "string"
#include "TypeConversion.h"

//功能: 设置文件所在路径
//strfile1:返回的文件路径
//:status:finfish
void ImageObject::SetRetrievalPath(CString &strfile1)
{
 CString m_strPath;    //检索库路径
 bool dir;//设置检索库标志位
 BROWSEINFO browse;
 ZeroMemory(&browse, sizeof(browse));       //fills a block of memory with zeros.
 browse.hwndOwner = NULL;
 browse.pszDisplayName = m_strPath.GetBuffer(MAX_PATH);
 browse.lpszTitle = "请选择一个目录";
//SHBrowseForFolder函数返回一个ITEMIDLIST结构的指针,包含了用户选择文件夹的信息
 LPITEMIDLIST lpItem = SHBrowseForFolder(&browse);
 if (lpItem == NULL) return;
 m_strPath.ReleaseBuffer();
//SHGetPathFromIDList把项目标志符列表转换为文档系统路径
 if (SHGetPathFromIDList(lpItem, m_strPath.GetBuffer(MAX_PATH)) == false) return;
 m_strPath.ReleaseBuffer();
 dir = true; //标志位设置为true,表示待检索文件已设置
//得到目录下文件的路径
 m_strPath.TrimRight(); m_strPath.TrimLeft(); //去除前后多余
 strfile1 = m_strPath;
}
//扫描当前目录中的图像个数并获得文件名
//strfile:文件夹路径
//form:文件格式
//FileMessage:filename[tempi]:文件名
//filenum:文件个数
//:status:finfish
FileMessage ImageObject::GetFileNameAndNumber(const CString &strfile, const CString &form)
{
 int tempi = 0;//临时的检索库图像计数器
 FileMessage temp;//检索库中图像路径
 temp.filenum = 0;
 BOOL yesno;
 CFileFind find;
 char tempFileFind[200];
 sprintf_s(tempFileFind, "%s\\*." + form, strfile);
 yesno = find.FindFile(tempFileFind); //在当前目录下查找form文件
 while (yesno)
 {
 yesno = find.FindNextFile();
 char foundFileName[200];//临时存储查找到的图像名
 strcpy_s(foundFileName, find.GetFileName().GetBuffer(200));//获取图像名
  if (!find.IsDots()) //过滤缺省目录
  {
  char tempFileName[200];
  sprintf_s(tempFileName, "%s\\%s", strfile, foundFileName);
  CString strfilepath1;
  strfilepath1.Format("%s", tempFileName);//获取图像完整路径
  temp.filenum++;
  temp.filename[tempi] = strfilepath1;//保存图像完整路径
  tempi++;
  }
 }
  find.Close();
  return temp;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值