登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> 程序员学前班[不再更新,只读] >> 主题: gd 库日志     [回主站]     [分站链接]
标题
gd 库日志
clq
浏览(0) + 2007-09-26 10:43:20 发表 编辑

关键字:

gd 库日志

图像写入内存.

来自 http://www.boutell.com/gd/faq.html

How do I save an image to a buffer in memory?
The following C code shows how to save a gd image to a memory buffer, and then write that buffer to a file on disk. You could also write it directly to stdout, preceded by a Content-type: image/png header and two CR/LF sequences, to directly return an image from a CGI program.

Note to Borland C++ programmers: this code will allow you to save an image using the bgd.dll library, because no FILE * objects are passed across the DLL boundary.

For your convenience, gd allocates the buffer for you; when you are done with it, you must call gdFree to release it.

Of course, there is a gdImageJpegPtr function available as well. This particular example saves a PNG image.
#include
#include
#include

void mySavePng(char *filename,
gdImagePtr im)
{
FILE *out;
int size;
char *data;
out = fopen("filename, "wb");
if (!out) {
/* Error */
}
data = (char *) gdImagePngPtr(im, &size);
if (!data) {
/* Error */
}
if (fwrite(data, 1, size, out) != size) {
/* Error */
}
if (fclose(out) != 0) {
/* Error */
}
gdFree(data);
}

clq
2007-9-26 13:37:43 发表 编辑

gd 库必须要 multithreaded DLL 链接库!

[url]

http://www.newbt.net:8022/read.csp?tid=1341

clq
2007-9-26 13:40:05 发表 编辑

奇怪为什么 bgd_a.lib 链接不上?
clq
2007-9-26 15:24:50 发表 编辑

写入 std::string 中.

来自 http://www.codeguru.com/forum/showthread.php?t=428722

CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
Binary data from void * to std::string?

#1
July 13th, 2007, 02:05 PM
bakani
Junior Member Join Date: Jul 2007, Posts: 2
Reputation:

Binary data from void * to std::string?
Hi,

I'm currently trying to use the GD library. I want to create an image in memory and put that image into a std::string for output using an stream.

void *str = gdImageJpegPtr(im, &size, -1);

size is an integer that contains the size of the actual data
str is a pointer to the data in memory

What is the best way to get that binary data into a std::string?


Thank you for your help


Alex

#2
July 13th, 2007, 02:26 PM
ZuK
Member + Join Date: Oct 2002, Posts: 639
Location: Austria
Reputation:

Re: Binary data from void * to std::string?
I don't think that it is very useful to store binary data in a string.
If all you want to do is save the data in an output stream then I would use ofstream::write.
something like this

Code:
ofstream fout( "image.dat", ios::binary );
void *str = gdImageJpegPtr(im, &size, -1);
fout.write(static_cast(str), size );

Kurt

#3
July 13th, 2007, 03:13 PM
HighCommander4
Senior Member Join Date: Apr 2004, Posts: 1,087
Location: Canada
Reputation:

Re: Binary data from void * to std::string?
You can put it into an std::string quite easily.

Code:
void *str = gdImageJpegPtr(im, &size, -1);
std::string s(static_cast(str), static_cast(str) + size);
__________________
Rewards come to those who persist after others have given up.

#4
July 13th, 2007, 04:03 PM
bakani
Junior Member Join Date: Jul 2007, Posts: 2
Reputation:

Re: Binary data from void * to std::string?
Thank you very much! That did the trick.

Alex


« Previous Thread | Next Thread »

CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
Binary data from void * to std::string?


clq
2007-9-27 15:24:28 发表 编辑

cairo 一个可能更强大也更庞大的库.

[url]
http://www.newbt.net:8022/read.csp?tid=1958

clq
2008-1-20 11:36:41 发表 编辑

图片使用后要记得

gdImageDestroy(im);

php 里面是不用,C 代码里面不用会死得很难看。另外似乎调用这个函数后 gdImageJpegPtr() 生成的结果好象仍然是可用的?必须要用 gdFree() 释放?


总数:5 页次:1/1 首页 尾页  
总数:5 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1