加载映像(GDIplus)后程序崩溃
Program crashes after loading image (GDIplus)
我想写一个程序,加载命令行给出的图像,然后对它做一些事情。我现在的问题是:每当我执行这个程序时,它都会打印出图像的调色板大小(这给了我一个错误的输出(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;
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃