2010-02-21 20 views
1

我一直在尋找JPG保存庫很長一段時間的C++,但我似乎無法得到任何工作。現在我正在嘗試使用LibGD:LibGD庫不工作:保存圖像時崩潰

什麼即時做錯了?它似乎工作,但節省的崩潰。代碼:

... 
#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); 
} 

我從網上下載庫:http://www.libgd.org/releases/gd-latest-win32.zip

我有庫/包括在正確的目錄等/ bgd.dll文件

編輯:回答以下包含此代碼,固定我的問題:

int size; 
char* data = (char*)gdImagePngPtr(im, &size); 
fwrite(data, sizeof(char), size, out); 
gdFree(data); 

回答

2

檢查imjpegout BEF你嘗試使用它們來確保它們都被分配。

[編輯]它將更好地分裂文件句柄從測試它的有效性。你有沒有試過the libgd example

[編輯2]我下載了相同的源碼等,在VS2008中設置了一個項目,並得到完全相同的問題。你可以嘗試this suggestion ..

約GD一個重要的事情是要確保它是針對同一個CRT爲主要項目的建設,因爲它使用了這種結構的文件,如果你調用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); 
} 
+0

耶。我現在更新了我的帖子,它仍然在這條線上崩潰。 – Newbie 2010-02-21 23:57:08

+0

是的,我從該網站的代碼,這是完全相同的代碼btw。 – Newbie 2010-02-22 12:46:29

+0

我的意思是它沒有工作。仍然在同一行代碼中崩潰。 – Newbie 2010-02-22 12:52:02