C# 实现实现SDL2进行视频播放窗口截图和字幕添加进行视频播放窗口截图和字幕添加
使用使用SDL2进行视频播放窗口截图和字幕添加操作进行视频播放窗口截图和字幕添加操作
SDL API查看:https://wiki.libsdl.org/APIByCategory
视频截图视频截图
我就废话不多说了,大家还是直接看代码吧~
/// <summary>
/// SDL2截图操作类
/// </summary>
public unsafe class SDLScreenshot
{
IntPtr window;// 窗口对象
IntPtr renderer;// 播放窗口的渲染器(来自于已初始化的播放窗口渲染器)
public SDLScreenshot(IntPtr window, IntPtr renderer)
{
this.window = window;
this.renderer = renderer;
}
/// <summary>
/// 保存截图
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="path"></param>
public void SaveBMP(int width, int height,string path)
{
// 判断渲染器是否初始化
if (renderer == IntPtr.Zero)
{
Console.WriteLine("renderer is null ,please call Init() method.");
return;
}
uint Rmask=0x00FF0000, Gmask = 0x0000FF00, Bmask = 0x000000FF, Amask = 0x00000000;
// 获取图像数据
SDL.SDL_Surface* surface= (SDL.SDL_Surface*)SDL.SDL_CreateRGBSurface(0, width, height, 32, Rmask, Gmask, Bmask, Amask);
//设置纹理的数据
SDL.SDL_Rect destrect;
destrect.x = 0;
destrect.y = 0;
destrect.w = width;
destrect.h = height;
// 读取并渲染图像数据
SDL.SDL_RenderReadPixels(renderer, ref destrect, SDL.SDL_PIXELFORMAT_ARGB8888, surface->pixels, surface-
>pitch);
//保存图片
int i = SDL.SDL_SaveBMP((IntPtr)surface, path);
if (i != 0)
{
Console.WriteLine("screenshot failed." + SDL.SDL_GetError());
}
SDL.SDL_FreeSurface((IntPtr)surface);
//SDL.SDL_RenderClear(renderer);
//SDL.SDL_DestroyRenderer(renderer);
}
/// <summary>
/// 加载截图
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="path"></param>
public void LoadBMP(int width, int height, string path)
{
// 判断渲染器是否初始化
if (renderer == IntPtr.Zero)
{