huanhai 发表于 2025-12-31 22:50:31

合并目录图片

可以横向,可以纵向
顺序可以用123456789的这种顺序来表示,比如1.png

<火山程序 类型 = "通常" 版本 = 1 />

方法 合并目录图片 <公开 类型 = 逻辑型 注释 = "合并指定目录内的所有图片" 折叠>
参数 目录路径 <类型 = 文本型 注释 = "图片所在目录">
参数 输出路径 <类型 = 文本型 注释 = "合并后的输出路径">
参数 纵向合并 <类型 = 逻辑型 注释 = "真为纵向合并,假为横向合并">
{
    变量 结果 <类型 = 逻辑型>

    @ #include <gdiplus.h>
    @ #pragma comment(lib, "gdiplus.lib")
    @ using namespace Gdiplus;
    @
    @ GdiplusStartupInput gdiplusStartupInput;
    @ ULONG_PTR gdiplusToken;
    @ GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    @
    @ // 获取目录中的所有图片文件
    @ WIN32_FIND_DATAW findData;
    @ HANDLE hFind = FindFirstFileW((LPCWSTR)(@<目录路径> + L"\\*.png").GetText(), &findData);
    @
    @ if (hFind == INVALID_HANDLE_VALUE)
    @ {
    @   GdiplusShutdown(gdiplusToken);
    @   return FALSE;
    @ }
    @
    @ std::vector<Image*> images;
    @ INT totalWidth = 0, totalHeight = 0;
    @ INT maxWidth = 0, maxHeight = 0;
    @
    @ do
    @ {
    @   if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    @   {
    @         CWString filePath = (LPCWSTR)(@<目录路径> + L"\\" + findData.cFileName).GetText();
    @         Image* img = new Image(filePath.GetText());
    @
    @         if (img->GetLastStatus() == Ok)
    @         {
    @             images.push_back(img);
    @             maxWidth = max(maxWidth, (INT)img->GetWidth());
    @             maxHeight = max(maxHeight, (INT)img->GetHeight());
    @         }
    @         else
    @         {
    @             delete img;
    @         }
    @   }
    @ } while (FindNextFileW(hFind, &findData));
    @
    @ FindClose(hFind);
    @
    @ if (images.empty())
    @ {
    @   GdiplusShutdown(gdiplusToken);
    @   return FALSE;
    @ }
    @
    @ // 计算合并后的尺寸
    @ if (@<纵向合并>)
    @ {
    @   totalWidth = maxWidth;
    @   for (auto img : images)
    @         totalHeight += img->GetHeight();
    @ }
    @ else
    @ {
    @   totalHeight = maxHeight;
    @   for (auto img : images)
    @         totalWidth += img->GetWidth();
    @ }
    @
    @ // 创建合并后的图片
    @ Bitmap* resultBitmap = new Bitmap(totalWidth, totalHeight, PixelFormat32bppARGB);
    @ Graphics* graphics = Graphics::FromImage(resultBitmap);
    @ graphics->Clear(Color::White);
    @
    @ INT currentX = 0, currentY = 0;
    @
    @ for (auto img : images)
    @ {
    @   if (@<纵向合并>)
    @   {
    @         // 纵向合并,自适应宽度
    @         graphics->DrawImage(img, currentX, currentY, maxWidth, img->GetHeight());
    @         currentY += img->GetHeight();
    @   }
    @   else
    @   {
    @         // 横向合并,自适应高度
    @         graphics->DrawImage(img, currentX, currentY, img->GetWidth(), maxHeight);
    @         currentX += img->GetWidth();
    @   }
    @ }
    @
    @ // 保存合并后的图片为BMP格式(GDI+原生支持)
    @ CLSID bmpClsid = {0x557cf400, 0x1a04, 0x11d3, {0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e}};
    @ Status status = resultBitmap->Save((LPCWSTR)@<输出路径>.GetText(), &bmpClsid, NULL);
    @
    @ // 清理资源
    @ delete graphics;
    @ delete resultBitmap;
    @
    @ for (auto img : images)
    @   delete img;
    @
    @ GdiplusShutdown(gdiplusToken);
    @
    @ @<结果> = (status == Ok);

    返回 (结果)
}

兵三进一 发表于 2025-12-31 23:33:02

这个好,收藏备用{:3_52:}{:3_52:}{:3_52:}

kingsoft 发表于 2026-1-1 00:13:58

感谢分享,点赞支持

itismine 发表于 2026-1-3 15:54:18

学习一下。
页: [1]
查看完整版本: 合并目录图片