Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 613817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:02:41+00:00 2026-05-13T18:02:41+00:00

I’ve been seeking for JPG saving library for long time for c++, but i

  • 0

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

I downloaded the library from: http://www.libgd.org/releases/gd-latest-win32.zip

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

Edit: 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:02:41+00:00Added an answer on May 13, 2026 at 6:02 pm

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

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

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

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.