本篇文章給大家分享的是有關怎么在C# 中利用SDL2實現(xiàn)一個視頻播放窗口截圖,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

具體方法如下
/// <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;
// 獲取圖像數(shù)據(jù)
SDL.SDL_Surface* surface= (SDL.SDL_Surface*)SDL.SDL_CreateRGBSurface(0, width, height, 32, Rmask, Gmask, Bmask, Amask);
//設置紋理的數(shù)據(jù)
SDL.SDL_Rect destrect;
destrect.x = 0;
destrect.y = 0;
destrect.w = width;
destrect.h = height;
// 讀取并渲染圖像數(shù)據(jù)
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)
{
Console.WriteLine("renderer is null ,please call Init() method.");
return;
}
// 加載圖片
IntPtr surface = SDL.SDL_LoadBMP(path);
if (surface == IntPtr.Zero)
{
Console.WriteLine("load bmp failed." + SDL.SDL_GetError());
return;
}
IntPtr texture = SDL.SDL_CreateTextureFromSurface(renderer, surface);
if (texture == IntPtr.Zero)
{
Console.WriteLine("create texture failed." + SDL.SDL_GetError());
return;
}
SDL.SDL_FreeSurface(surface);
//設置紋理的數(shù)據(jù)
SDL.SDL_Rect destrect;
destrect.x = 0;
destrect.y = 0;
destrect.w = width;
destrect.h = height;
SDL.SDL_Rect srcrect = destrect;
//SDL.SDL_RenderClear(renderer);
SDL.SDL_RenderCopy(renderer, texture, ref srcrect, ref destrect);
SDL.SDL_RenderPresent(renderer);
//SDL.SDL_Delay(20);
SDL.SDL_DestroyTexture(texture);
//SDL.SDL_DestroyRenderer(renderer);
//SDL.SDL_DestroyWindow(screen);
//Quit SDL
//SDL.SDL_Quit();
}
}
分享標題:怎么在C#中利用SDL2實現(xiàn)一個視頻播放窗口截圖-創(chuàng)新互聯(lián)
鏈接URL:http://chinadenli.net/article14/pspge.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、外貿(mào)網(wǎng)站建設、網(wǎng)站策劃、全網(wǎng)營銷推廣、網(wǎng)站排名、移動網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容