ImgProcUnit.cpp
上传用户:pass2008
上传日期:2021-07-05
资源大小:3299k
文件大小:3k
- #include "StdAfx.h"
- #include "ImgProcUnit.h"
- #include <stdio.h>
- #include <WinInet.h>
- ImgProcUnit::ImgProcUnit(CStringArray& imgLinks, CString& dmURL)
- {
- if (imgLinks.GetCount()>0)
- {
- int count = imgLinks.GetCount();
- for (int i=0; i<count; i++)
- {
- imageLinks.Add(imgLinks.GetAt(i));
- }
- }
- domainURL = dmURL;
- imgProcClass = NULL;
- }
- ImgProcUnit::~ImgProcUnit(void)
- {
- if (imgProcClass)
- {
- imgProcClass = NULL;
- }
- }
- BOOL ImgProcUnit::downloadImages(void)
- {
- /*
- CInternetSession inetSession("ImageCollections");
- CHttpFile* imgFile;
- for (int i=0; i<imageLinks.GetCount(); i++)
- {
- try
- {
- imgFile = (CHttpFile*)inetSession.OpenURL(imageLinks.GetAt(i));
- if (imgFile != INVALID_HANDLE_VALUE)
- {
- dwFileSize = imgFile->GetLength();
- }
- }
- catch (CInternetException* e)
- {
- imgFile = NULL;
- e->Delete();
- }
- }
- HINTERNET hNet, hUrlFile;
- CFile imgFile;
- for (int i=0; i<imageLinks.GetCount(); i++)
- {
- hNet = ::InternetOpen(_T(""), PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
- hUrlFile = ::InternetOpenUrl(hNet, imageLinks.GetAt(i), NULL, 0, INTERNET_FLAG_RELOAD, 0);
- }
- */
- CString imgDirectory(_T("C:\ImageDirectory")); //图片保存总目录
- if (!::PathIsDirectory(imgDirectory))
- {
- if (!::CreateDirectory(imgDirectory, NULL))
- {
- return FALSE;
- }
- }
- CString recordName = domainURL; //每个网页图片保存目录
- elimInvalidPrefixSymbols(recordName);
- if (!::PathIsDirectory(imgDirectory+_T("\")+recordName))
- {
- if (!::CreateDirectory(imgDirectory+_T("\")+recordName, NULL))
- {
- return FALSE;
- }
- }
- /*
- HANDLE rhFile = ::CreateFile(_T("C:\Images\")+recordName,
- GENERIC_ALL,
- 0,
- NULL,
- OPEN_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- if (INVALID_HANDLE_VALUE == rhFile)
- {
- return FALSE;
- }
- */
- HRESULT hr;
- CString prefixName;
- CString imgName;
- for (int i=0; i<imageLinks.GetCount(); i++)
- {
- prefixName = imageLinks.GetAt(i);
- elimInvalidPrefixSymbols(prefixName);
- imgName = imgDirectory+_T("\")+recordName+_T("\")+prefixName; //文件名
- hr = ::URLDownloadToFile(NULL, imageLinks.GetAt(i), imgName, 0, NULL);
- if (hr == S_OK)
- {
- imageNames.Add(imgName);
- }
- else
- {
- imageNames.Add(_T("Download Error!"));
- }
- }
-
- return TRUE;
- }
- BOOL ImgProcUnit::elimInvalidPrefixSymbols(CString& strPrefix)
- {
- strPrefix.Remove('\');
- strPrefix.Remove('/');
- strPrefix.Remove(':');
- strPrefix.Remove('*');
- strPrefix.Remove('?');
- strPrefix.Remove('"');
- strPrefix.Remove('<');
- strPrefix.Remove('>');
- strPrefix.Remove('|');
- //strPrefix.Remove('.');
- strPrefix.Remove('_');
- strPrefix.Remove('-');
- return TRUE;
- }
- BOOL ImgProcUnit::imageEdgeProcess(void)
- {
- for (int i=0; i<imageNames.GetCount(); i++)
- {
- if (imageNames.GetAt(i) != _T("Download Error!"))
- {
- if (imgProcClass)
- {
- delete imgProcClass;
- imgProcClass = NULL;
- }
- imgProcClass = new CDib;
- imgProcClass->imageLoadOperation(imageNames.GetAt(i), 1);
- }
- }
- return TRUE;
- }