使用GD库生成的图片在windows中不能正常显示

xlstc7 2008-01-16 03:59:17
使用c++,生成后的文件有数据,可是在windows中打开显示的是乱七八糟的一些颜色,这只是针对jpeg的,如果生成png,在windows中打开都不行。
有在c++中使用过gd的吗?谢谢帮忙看看。

代码是官方的示例:

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);
}
...全文
253 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
marrylayking 2012-08-16
  • 打赏
  • 举报
回复
dhshuai,为什么我按照上面的设置了,我的程序还是不能运行啊?gdImagePng(im, pngout);这一步就出错 了!!!!!!因为我用的bgd.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>

#include"StdAfx.h"

void main() {
/* Declare the image */
gdImagePtr im;
/* Declare an output file */
FILE *out;
/* Declare color indexes */
int black;
int white;


typedef gdImagePtr(__stdcall *Create)(int sx, int sy);
typedef int(__stdcall *Allocate)(gdImagePtr im, int r, int g, int b);

typedef void(__stdcall *ImageLine)(gdImagePtr im, int x1, int y1, int x2, int y2, int color);

typedef void *(__stdcall *ImagePngPtr)(gdImagePtr im, int *size);

typedef void(__stdcall *ImageDestroy)(gdImagePtr im);




typedef void(__stdcall *ImagePng)(gdImagePtr im, FILE * out);

HINSTANCE hDLL;
hDLL=LoadLibrary("bgd.dll");

Create gdImageCreate;
gdImageCreate=(Create)GetProcAddress(hDLL,"gdImageCreate@8");


Allocate gdImageColorAllocate;
gdImageColorAllocate=(Allocate)GetProcAddress(hDLL,"gdImageColorAllocate@16");



ImageLine gdImageLine;
gdImageLine=(ImageLine)GetProcAddress(hDLL,"gdImageLine@24");


ImagePngPtr gdImagePngPtr;
gdImagePngPtr=(ImagePngPtr)GetProcAddress(hDLL,"gdImagePngPtr@8");

ImageDestroy gdImageDestroy;
gdImageDestroy=(ImageDestroy)GetProcAddress(hDLL,"gdImageDestroy@4");

ImagePng gdImagePng;
gdImagePng=(ImagePng)GetProcAddress(hDLL,"gdImagePng@8");

/* 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. */
out = fopen( "test.png ", "wb ");


/*int size;
char *data;


Output the image to the disk file.
data = (char *) gdImagePngPtr(im, &size);
fwrite(data, 1, size, out); */

/* Output the image to the disk file in PNG format. */
gdImagePng(im, out);



/* Close the file. */
fclose(out);

/* Destroy the image in memory. */
gdImageDestroy(im);
FreeLibrary(hDLL);

}
dhshuai 2009-04-06
  • 打赏
  • 举报
回复
我也遇到了这个问题,原来是没有认真读取GD安装要求:
Why does bgd.dll crash with my C program?
You are trying to use gd's stdio library functions with a different C runtime library. Read on for notes on individual compilers.
Microsoft Visual C++: If you want to use gdImageCreateFromPng, gdImagePng, and other functions that take a FILE *, you MUST use Visual C++ 6.0 or earlier and you MUST link with the "multithreaded DLL" runtime library option. If you wish to use the .NET compiler, read the "Borland C++" section for an alternative method.

具体操作如下(在VC6.0):
Project-->Setting--> C/C++ -->Category下拉框中选择Code Generation,在出现的界面中有一个Use run-time library选择框,下拉选择"multithreaded DLL",确定再运行即可

xlstc7 2008-01-17
  • 打赏
  • 举报
回复
我的gd版本也是官方最新的,如果你能正常显示,那是不是因为我的机器环境造成的呢?或者要进行什么配置?或者jpeg库或png库的版本太低?我用的是solaris 10,jpeg和png的库是自带的。
x86 2008-01-17
  • 打赏
  • 举报
回复
我用你的代码生成的jpg和png在windows下打开都没问题。
我用的是官方最新的版本,你可以到这里下载源码:
http://www.libgd.org/
编译很简单,只需./configure然后make install,缺省安装到/usr/local/下,在你的代码加上一行:
#include "/usr/local/include/gd.h"

编译:
g++ -o test test.cpp -lgd
xlstc7 2008-01-16
  • 打赏
  • 举报
回复
刚刚看了看jpeg库的文档 ,它只是用来转换图片格式的,压缩成jpeg和解压缩jpeg。

以前cximage也试过,同样的代码在windows中生成图片正常,可是放到solaris下生成的图片在windows中打开后读出来的就不正常,看到是一些乱七八糟的颜色。难道是solaris环境没设置好造成的?
xlstc7 2008-01-16
  • 打赏
  • 举报
回复
直接用jpeg库?就是那个libjpeg?它可不可以在图片中写字?还有读取另一个图片文件的像素?
ceasar1134 2008-01-16
  • 打赏
  • 举报
回复
gd库是么?我们使用的jpeg库...

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧