I’ve been seeking for JPG saving library for long time for c++, but i cant seem to get anything to work. Now i am trying use LibGD:
What im doing wrong ? It seems to work, but the saving crashes. 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 the library / include / bgd.dll files in correct directories etc.
Edit: Answer below includes this code that fixed my problem:
int size;
char* data = (char*)gdImagePngPtr(im, &size);
fwrite(data, sizeof(char), size, out);
gdFree(data);
Check
imandjpegoutbefore you try and use them to make sure they were both allocated.[Edit] It would be better to split assigning the file handle from the test for it’s validity. Have you tried the libgd example?
[Edit2] I downloaded the same source etc, set up a project in VS2008 and get the exact same problem. You could try this suggestion..
There’s a code snippet in there which fixes the crash on my machine: