How does SDL Texture save pictures?

  • I need to save the texture as PNG please help
  • Otherwise I can’t continue to use SDL and this game is dead
    Why not continue with Ex renderer? Because I want to combine multiple textures into a single texture

I’m working with SDL_RenderCopyEx and I’m trying to save it and IT’s SDL_Texture and I can’t save it

So I want to convert SDL_Texture to SDL_Surface to save the shape

If the texture contents were created from pixels you uploaded, then you can just use those pixels directly and don’t try to recover them from the texture; just save them somewhere for when you need them.

If the texture was created as a render target (it was used with SDL_SetRenderTarget() and you used SDL_RenderCopyEx() to draw into that texture), then you can use SDL_RenderReadPixels() to recover the pixels from the texture.

Once you have the pixels, you can do whatever you like with them; if you put them in an SDL_Surface, you can use SDL_SaveBMP() to write a .bmp file to disk, or use the raw pixels with something that will encode it in .png format, etc…there are several options for this, but SDL doesn’t encode PNG files itself.

1 Like

I don’t think SDL support encoding to PNG.
You might want to use 3rd party libraries like cairo.

Use SDL_image: IMG_SavePNG.

1 Like
  • Problem solved Thank you

  • There are still some small black spots in some PNG background removals

  • The solution is as follows
    ```
    sc = IMG_Load(“1.png”); // Set up the surface
    sc->clip_rect.x =0;
    sc->clip_rect.y = 0;
    sc->clip_rect.w = 500;
    sc->clip_rect.h = 500;
    if (SDL_RenderReadPixels(renderer, &sc->clip_rect, sc->format->format, sc->pixels, sc->pitch)) {
    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, “asdasd”,
    SDL_GetError(),NULL);
    } //Put the renderer pixel region into the surface

    Uint64 colorkey = SDL_MapRGB(sc->format, 0x00, 0x00, 0x00);
    SDL_SetColorKey(sc, 1, colorkey);
    IMG_SavePNG(sc, “2.png”);

2 Likes