Проверьте im
и jpegout
, прежде чем пытаться использовать их, чтобы убедиться, что они оба были выделены.
[Редактировать] Было бы лучше разделить назначение дескриптора файла из теста на его достоверность. Вы пробовали пример libgd ?
[Edit2] Я скачал тот же источник и т. Д., Настроил проект в VS2008 и получил точно такую же проблему. Вы можете попробовать это предложение ..
одна важная вещь в GD - убедиться, что он построен на том же CRT, что и основной проект, поскольку он использует такие структуры, как FILE, и если вы вызываете GD DLL, созданную с одной версией компилятора, из исполняемого файла, созданного с другой версией, вы столкнетесь с нарушениями доступа к памяти.
Там есть фрагмент кода, который исправляет сбой на моей машине:
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
errno_t result1 = fopen_s(&pngout, "C:\\Projects\\Experiments\\LibGD\\test.png", "wb");
/* Do the same for a JPEG-format file. */
errno_t result2 = fopen_s(&jpegout, "C:\\Projects\\Experiments\\LibGD\\test.jpg", "wb+");
/* Output the image to the disk file in PNG format. */
int size;
char* data = (char*)gdImagePngPtr(im, &size);
fwrite(data, sizeof(char), size, pngout);
gdFree(data);
data = (char*)gdImageJpegPtr(im, &size, -1);
fwrite(data, sizeof(char), size, jpegout);
gdFree(data);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}