gpt4 book ai didi

c++ - 加载图像后程序崩溃 (GDIplus)

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:55 24 4
gpt4 key购买 nike

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

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 PaletteSize\n";

delete wc;
Gdiplus::GdiplusShutdown(m_gdiplusToken);
std::cout << "After Shutdown\n";
return 0;
}

最佳答案

解决方法是:
写作而不是

Gdiplus::Image image(wc);

这个:

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

这解决了我的问题。

关于c++ - 加载图像后程序崩溃 (GDIplus),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23873983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com