加载映像(GDIplus)后程序崩溃

Program crashes after loading image (GDIplus)

本文关键字:程序 崩溃 GDIplus 映像 加载      更新时间:2023-10-16

我想写一个程序,加载命令行给出的图像,然后对它做一些事情。我现在的问题是:每当我执行这个程序时,它都会打印出图像的调色板大小(这给了我一个错误的输出(12)对于我输入的jpg格式的每张图片),然后它崩溃了,但我无法找出我的错误。我做错了什么?

inline bool exists(const std::string& name) {
    if (FILE *file = fopen(name.c_str(), "r")) {
        fclose(file);
        return true;
    }
    else {
        return false;
    }
}
int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        std::cout << "Too few/too much arguments. Usage: ShrinkImage <Input-File> <Number of target colours>n";
        return 0;
    }
    int num_colors;
    char * filepath = argv[1];
    if (!exists(filepath))
    {
        std::cout << "File does not exist.n";
        return 0;
    }
    num_colors = std::stoi(argv[2], nullptr);
    ULONG_PTR(m_gdiplusToken);
    Gdiplus::GdiplusStartupInput gdiplusstartupinput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusstartupinput, NULL);
    const size_t cSize = strlen(filepath) + 1;
    size_t Size = cSize - 1;
    wchar_t *wc = new wchar_t[cSize];
    swprintf(wc, cSize, L"%hs", filepath);
    Gdiplus::Image image(wc);
    std::cout << image.GetPaletteSize() << 'n';
    std::cout << "Printed PaletteSizen";
    delete wc;
    Gdiplus::GdiplusShutdown(m_gdiplusToken);
    std::cout << "After Shutdownn";
    return 0;
}

解决方案是:
书写代替

Gdiplus::Image image(wc);

:

Gdiplus::Image * image = Gdiplus::Image::FromFile(wc);
delete image;
image = 0;