参考资料:【1】https://blog.youkuaiyun.com/qq_32563489/article/details/83383812
【2】https://blog.youkuaiyun.com/liyuanhong13/article/details/9974683/
【4】https://blog.youkuaiyun.com/iamduoluo/article/details/6679451
【5】https://blog.youkuaiyun.com/weixin_30527143/article/details/101182276
我看到win10的待机壁纸很漂亮,想把它们复制出来自己用,但是这些文件没有格式,而且分辨率不一样,再加上我只想要1920*1080格式的文件,自己手动一个一个改名字格式又很麻烦,于是就想到写一个程序,批量复制这些文件并进行分辨率判断。
再网上查找资料并学习后,便有了以下代码。
#include<iostream>
#include<io.h>
#include<cstdlib>
#include<string>
#include<fstream>
#include<WINDOWS.H>
#include<gdiplus.h>
#include<locale>
using namespace std;
using namespace Gdiplus;
#pragma warning(disable : 4996)
#pragma comment(lib, "gdiplus.lib")
//参考资料【3】
wstring StringToWstring(const std::string str)// string转wstring
{
unsigned len = str.size() * 2;// 预留字节数
setlocale(LC_CTYPE, ""); //必须调用此函数
wchar_t *p = new wchar_t[len];// 申请一段内存存放转换后的字符串
mbstowcs(p, str.c_str(), len);// 转换
std::wstring str1(p);
delete[] p;// 释放申请的内存
return str1;
}
string WstringToString(const std::wstring str)// wstring转string
{
unsigned len = str.size() * 4;
setlocale(LC_CTYPE, "");
char *p = new char[len];
wcstombs(p, str.c_str(), len);
std::string str1(p);
delete[] p;
return str1;
}
int main()
{
const string oldpath = "C:\\Users\\SSSSSakuraZ\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets";//源路径
const string destinationpath = "D:\\pictest\\img2";//目标路径
const string oldname = oldpath + "\\*.*";
const char *oldname_tochar = oldname.c_str();//源路径内文件string转char *
//参考资料【2】
_finddata_t file;
long lf;
if ((lf = _findfirst(oldname_tochar, &file)) == -1l)//寻找文件
{
cout << "文件没有找到!\n";
}
else
{
cout << "文件列表:\n";
while (_findnext(lf, &file) == 0)//寻找下一个文件
{
cout << file.name << " | ";
string readoldnane = file.name;
string oldpathname = oldpath+"\\"+readoldnane;
const char *oldpathname_tochar = oldpathname.c_str();//源路径+文件名
string destinationpathname = destinationpath + "\\" + readoldnane + ".jpg";//更改为jpg格式
const char *destinationpathname_tochar = destinationpathname.c_str();//目标路径+文件名
参考资料【1】
if (CopyFile(oldpathname_tochar, destinationpathname_tochar, FALSE))//FALSE代表覆盖,TRUE代表不覆盖
{
//参考资料【5】
GdiplusStartupInput gdiplusstartupinput;
ULONG_PTR gdiplustoken;
GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
wstring infilename = StringToWstring(destinationpathname);
Bitmap *bmp = new Bitmap(infilename.c_str());
int height = bmp->GetHeight();
int width = bmp->GetWidth();
cout << "复制成功 | ";cout << "width:" << width << ", height:" << height;
delete bmp;
GdiplusShutdown(gdiplustoken);
if (width != 1920)//判断分辨率
{
//参考资料【4】
if (DeleteFile(destinationpathname_tochar))
{
cout << " | 删除成功!" << endl;
}
}
}
else
{
cout << "复制失败 | 源路径:";
cout << oldpathname_tochar << " | 目标路径:" << destinationpathname_tochar;
}
cout << endl;
}
}
system("pause");
return 0;
}
终于能把win10壁纸快捷转出了,但是360会报敲诈病毒的错误。。。这又该怎么解决呢?