title: SDL2 png image won't load
body: <p>I have tried many (more than 10) SDL2 example files for C &amp; C++ that load images and any that attempt to load png fails. </p>
  
  <p>The C code below is about as simple and well commented as I can find and it has the same problem.</p>
  
  <p><strong><em>The code works fine with bmp images</em></strong> but will not work for a <strong><em>png file</em></strong>. (I am compiling from a win7 batch file using minGW.</p>
  
  <p>This line causes the error when using .png but works for bmp... <br>
  <em>SDL_Surface* surface = IMG_Load("smile.bmp");</em> </p>
  
  <p>Full code listing</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;SDL2/SDL.h&gt;
      #include &lt;SDL2/SDL_image.h&gt;
  
      int main(int argc, char* args[])
      {
        // attempt to initialize graphics and timer system
        if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
        {
          printf("error initializing SDL: %s\n", SDL_GetError());
          getchar(); // show error message, hit any key to quit
          return 1;  // return error value
        }
  
  // load support for the JPG and PNG image formats
  int flags=IMG_INIT_JPG|IMG_INIT_PNG;
  int initted=IMG_Init(flags);
  if((initted&amp;flags) != flags) {
      printf("IMG_Init: Failed to init required jpg and png support!\n");
      printf("IMG_Init: %s\n", IMG_GetError());
      // handle error
  }
  
        SDL_Window* win = SDL_CreateWindow("SDL2 image library test",
                                           SDL_WINDOWPOS_CENTERED,
                                           SDL_WINDOWPOS_CENTERED,
                                           400, 400, 0);
        if (!win)
        {
          printf("error creating window: %s\n", SDL_GetError());
          getchar(); // show error message, hit any key to quit
          SDL_Quit();
          return 1; // return error value
        }
  
        // print video card drivers
        SDL_RendererInfo info;
          int cnt, iDrivers;
          iDrivers = SDL_GetNumRenderDrivers();
          while ( cnt &lt; iDrivers )
          {
            if ( SDL_GetRenderDriverInfo(cnt,&amp;info) == 0 )
            {
              printf("%s\n",info.name);
            }
            cnt++;
          }  
  
  
        // create a renderer, which sets up the graphics hardware
        Uint32 render_flags = SDL_RENDERER_PRESENTVSYNC | IMG_INIT_JPG | IMG_INIT_PNG;
        SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
        if (!rend)
        {
          printf("error creating renderer: %s\n", IMG_GetError());
          getchar(); // show error message, hit any key to quit
          SDL_DestroyWindow(win);
          SDL_Quit();
          return 1; // return error value
        }
  
        // load the image into ram memory using SDL_image library function
        SDL_Surface* surface = IMG_Load("smile.png");
        if (!surface)
        {
          printf("error creating surface: %s \n", SDL_GetError());
          getchar(); // show error message, hit any key to quit
          SDL_DestroyRenderer(rend);
          SDL_DestroyWindow(win);
          SDL_Quit();
          return 1; // return error value
        }
  
        // load the image data into the graphics hardware's memory
        SDL_Texture* tex = SDL_CreateTextureFromSurface(rend, surface);
        SDL_FreeSurface(surface);
        if (!tex)
        {
          printf("error creating texture: %s\n", SDL_GetError());
          getchar(); // show error message, hit any key to quit
          SDL_DestroyRenderer(rend);
          SDL_DestroyWindow(win);
          SDL_Quit();
          return 1;
        }
  
        SDL_RenderClear(rend);       // clear the window
  
        SDL_RenderCopy(rend, tex, NULL, NULL); // draw the image to the window
        SDL_RenderPresent(rend);
  
        SDL_Delay(10000);           // pause 10 seconds before quitting
  
        SDL_DestroyTexture(tex);    // clean up resources before exiting
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(win);
        SDL_Quit();
        return 0;                   // return success value
      }
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <p>[SOLVED] With the help of many contributors below, I was able to eventually feel confident the program was correct and I had some type of installation problem. After a good nights sleep I downloaded and reloaded <strong><em>mingw, SDL2_image, and zlib</em></strong>, making sure I used 64bit versions. <strong><em>I am happy to report the problem has gone away.</em></strong></p>
  
  <p>To assist anyone in the future, the initialization code </p>
  
  <pre>int flags=IMG_INIT_JPG|IMG_INIT_PNG;
  int initted=IMG_Init(flags);</pre>
  
  <p>recommended by <em>"Roger Rapid &amp; Keltar"</em> is not absolutely necessary (SDL2 can figure this out dynamical) but having it is highly recommended because it helps trap errors, which was useful in this case. For that reason I have left it in this code listing.</p>
  
  <p>I am not quick to use forums but after 4 days I just didn't have a clue what could be causing the problem.</p>
  
  <p>Thanks for all the help :)</p>