使用GD库生成的图片在windows中不能正常显示
使用c++,生成后的文件有数据,可是在windows中打开显示的是乱七八糟的一些颜色,这只是针对jpeg的,如果生成png,在windows中打开都不行。
有在c++中使用过gd的吗?谢谢帮忙看看。
代码是官方的示例:
- C/C++ code
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. */ pngout = fopen("test.png", "wb"); /* Do the same for a JPEG-format file. */ jpegout = fopen("test.jpg", "wb"); /* Output the image to the disk file in PNG format. */ gdImagePng(im, pngout); /* Output the same image in JPEG format, using the default JPEG quality setting. */ gdImageJpeg(im, jpegout, -1); /* Close the files. */ fclose(pngout); fclose(jpegout); /* Destroy the image in memory. */ gdImageDestroy(im); }
我用你的代码生成的jpg和png在windows下打开都没问题。
我用的是官方最新的版本,你可以到这里下载源码:
http://www.libgd.org/
编译很简单,只需./configure然后make install,缺省安装到/usr/local/下,在你的代码加上一行:
#include "/usr/local/include/gd.h"
编译:
g++ -o test test.cpp -lgd