SDL 显示jpg、png等格式图片失败。

一、概述

想用SDL2来做一个简易的播放器,想在用户点击暂停时候,能显示有个暂停按钮的一张图片,利用ffmpeg 截取视频保存为jpg图片,然后利用ffmpeg 把截取的图片加上一张暂停按钮的水印,结果在利用SDL加载jpg图片时一直返回0。后来找了很多资料没能有实际的方法,最后是发现缺少sdl_image相关库导致的。


二、问题

奇怪的是IMG_Load("xxx.bmp") 针对bmp格式的图片是可以成功的。

SDL_Surface* t_surFace = IMG_Load("logo.jpg");

t_surFace一直为null,通过SDL_GetError()函数返回没有任何内容,找不到原因


三、解决方案

 初始化加载的库:

IMG_Init(IMG_INIT_JPG)

还要加上libjpeg-9.dll这个库


四、SDL_image这个库在我的资源里面下载

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用SDL2显示灰阶格式图片的示例代码,其中假设灰阶图片的宽度为width,高度为height,像素值存储在名为pixels的unsigned char类型数组中: ``` #include <SDL2/SDL.h> int main(int argc, char* argv[]) { SDL_Window* window = NULL; SDL_Surface* surface = NULL; SDL_Event event; unsigned char* pixels = NULL; int width = 640; int height = 480; if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); return 1; } window = SDL_CreateWindow("Gray Image", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN); if (window == NULL) { printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); return 1; } surface = SDL_GetWindowSurface(window); if (surface == NULL) { printf("Surface could not be created! SDL_Error: %s\n", SDL_GetError()); return 1; } pixels = (unsigned char*)surface->pixels; // 将像素值拷贝到surface中 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int index = i * width + j; pixels[index] = pixels[index + 1] = pixels[index + 2] = pixels[index + 3] = pixels[index]; } } // 更新surface SDL_UpdateWindowSurface(window); while (1) { if (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { break; } } } SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` 代码中,先创建一个窗口,然后获取该窗口的表面(surface)并将其存储在指针surface中。接下来,将unsigned char类型数组中的像素值拷贝到surface->pixels中,并将其显示在窗口上。最后,使用SDL_PollEvent函数来处理退出事件,等待用户关闭窗口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值