C++将源文件夹下的文件复制到目标文件夹,读取判断图片分辨率

参考资料:【1】https://blog.csdn.net/qq_32563489/article/details/83383812

【2】https://blog.csdn.net/liyuanhong13/article/details/9974683/

【3】https://blog.csdn.net/kunlunjunzi/article/details/50573627?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4

【4】https://blog.csdn.net/iamduoluo/article/details/6679451

【5】https://blog.csdn.net/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会报敲诈病毒的错误。。。这又该怎么解决呢?

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值