I have been looking for a JPG library for saving in C ++ for a long time, but I cannot get anything to work. Now I am trying to use LibGD:
What am I doing wrong? It seems to work, but the savings are falling. The code:
... #pragma comment(lib, "bgd.lib") #include <gd/gd.h> ... void save_test(){ gdImagePtr im; FILE *jpegout; int black; int white; im = gdImageCreateTrueColor(64, 64); black = gdImageColorAllocate(im, 0, 0, 0); white = gdImageColorAllocate(im, 255, 255, 255); gdImageLine(im, 0, 0, 63, 63, white); if(jpegout = fopen("test.jpg", "wb")){ if(im){ gdImageJpeg(im, jpegout, -1); // crash here! } fclose(jpegout); } gdImageDestroy(im); }
I downloaded the library from: http://www.libgd.org/releases/gd-latest-win32.zip
I have library / include / bgd.dll files in the correct directories, etc.
Edit: The answer below is this code that fixed my problem:
int size; char* data = (char*)gdImagePngPtr(im, &size); fwrite(data, sizeof(char), size, out); gdFree(data);