LibGD库不工作:保存图像时崩溃 [英] LibGD library is not working: crash when saving image

查看:197
本文介绍了LibGD库不工作:保存图像时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找JPG长时间为c ++保存库,但我似乎没有什么工作。现在我正在尝试使用LibGD:

I've been seeking for JPG saving library for long time for c++, but i cant seem to get anything to work. Now i am trying use LibGD:

什么做错了?它似乎工作,但节省崩溃。代码:

What im doing wrong ? It seems to work, but the saving crashes. Code:

...
#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文件

I have the library / include / bgd.dll files in correct directories etc.

编辑:答案下面包含了修复我的问题的代码:

Answer below includes this code that fixed my problem:

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


推荐答案

检查 im

Check im and jpegout before you try and use them to make sure they were both allocated.

您可以使用c $ c>和 jpegout ]从测试中分配文件句柄的有效性会更好。您是否尝试过的例子的libgd 的?

It would be better to split assigning the file handle from the test for it's validity. Have you tried the libgd example?

[Edit2]我下载了相同的源等,在VS2008中设置一个项目,并得到完全相同的问题。您可以尝试此建议 ..

I downloaded the same source etc, set up a project in VS2008 and get the exact same problem. You could try this suggestion..

有关GD一件重要的事情是要确保它是针对同一个CRT为主要项目的建成,因为它使用了这种结构的文件,如果你调用GD DLL用的一个版本内置从另一个版本建立一个可执行的编译器,你会遇到内存访问的违规行为。

one important thing about GD is to make sure it's built against the same CRT as the main project because it uses such structures as FILE and if you call GD DLL built with one version of the compiler from an executable built with another version, you will run into memory access violations.

那里面有一段代码,修正了崩溃在我的机器上:

There's a code snippet in there which fixes the crash on my machine:

/* 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);
}

这篇关于LibGD库不工作:保存图像时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆