Home > Backend Development > C#.Net Tutorial > How to display pictures in C language code

How to display pictures in C language code

下次还敢
Release: 2024-04-04 22:39:29
Original
1354 people have browsed it

To display images in C language, you can use the SDL2 library: initialize the SDL2 library; create a window; create a renderer; load an image; create an image texture; clear the renderer; render an image; update the display; main loop; destroy resource.

How to display pictures in C language code

Display pictures in C language

In C language, you can use the SDL2 library (Simple DirectMedia Layer) to display image. The following steps describe how to use SDL2 to display images in C language:

1. Initialize the SDL2 library

#include <stdio.h>
#include <SDL2/SDL.h>

int main() {
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
    return 1;
  }
Copy after login

2. Create a window

  SDL_Window *window = SDL_CreateWindow("Image Viewer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
  if (window == NULL) {
    fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError());
    SDL_Quit();
    return 1;
  }
Copy after login

3. Create a renderer

  SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  if (renderer == NULL) {
    fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError());
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 1;
  }
Copy after login

4. Load an image

  SDL_Surface *image = SDL_LoadBMP("image.bmp");
  if (image == NULL) {
    fprintf(stderr, "SDL_LoadBMP Error: %s\n", SDL_GetError());
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 1;
  }
Copy after login

5. Create an image texture

  SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);
  SDL_FreeSurface(image);
  if (texture == NULL) {
    fprintf(stderr, "SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError());
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 1;
  }
Copy after login

6. Clear renderer

  SDL_RenderClear(renderer);
Copy after login

7. Render image

  int w, h;
  SDL_QueryTexture(texture, NULL, NULL, &w, &h);
  SDL_Rect dstrect = { 0, 0, w, h };
  SDL_RenderCopy(renderer, texture, NULL, &dstrect);
Copy after login

8. Update Display

  SDL_RenderPresent(renderer);
Copy after login

9. Main loop

  SDL_Event event;
  while (SDL_PollEvent(&event)) {
    if (event.type == SDL_QUIT) {
      break;
    }
  }
Copy after login

10. Destroy resources

  SDL_DestroyTexture(texture);
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
Copy after login

The above is the detailed content of How to display pictures in C language code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template