亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在C# 中利用SDL2實現一個視頻播放窗口截圖

發布時間:2020-12-08 14:53:35 來源:億速云 閱讀:522 作者:Leah 欄目:開發技術

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

具體方法如下

 /// <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)
   {
    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); 
 
   //設置紋理的數據
   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();
  }
 }

播放測試代碼:

 if (isSaveScreenshot)
       {
        SDLScreenshot screenshot = new SDLScreenshot(sdlVideo.window, sdlVideo.sdlrenderer);
        screenshot.SaveBMP(nvVideoframe.VideoFrame->width, nvVideoframe.VideoFrame->height, "screenshot.bmp");
        isSaveScreenshot = false;
     }

測試效果圖:

怎么在C# 中利用SDL2實現一個視頻播放窗口截圖

注:此處截圖是直接獲取的播放窗口的圖像像素來實現的。

視頻字幕

 /// <summary>
 /// SDL2字幕顯示類
 /// </summary>
 public unsafe class SDLTTF
 {
  IntPtr renderer;// 播放窗口的渲染器(來自于已初始化的播放窗口渲染器)
 
  public SDLTTF(IntPtr renderer)
	{
   this.renderer = renderer;
  }
 
  /// <summary>
  /// 展示字幕文字
  /// </summary>
  /// <param name="text"></param>
  public void ShowText(string ttfPath, int fontSize,string text)
  {
   // 初始化 ttf
   if (SDL_ttf.TTF_Init() < 0)
   {
    Console.WriteLine("SDL_ttf.TTF_Init() failed.");
    return;
   }
   // 是否初始化完成
   int was_init = SDL_ttf.TTF_WasInit();
 
   if (was_init == 1)
    // SDL_ttf was already initialized
    Console.WriteLine("SDL_ttf was already initialized");
   else if (was_init == 0)
    // SDL_ttf was not already initialized
    Console.WriteLine("SDL_ttf was not already initialized");
 
   // 判斷是否初始化
   if (renderer == IntPtr.Zero)
   {
    Console.WriteLine("Not initialized by SDL_ttf.TTF_Init() ,please call Init() method.");
    return;
   }
 
   //如:打開ttfPath=simfang.ttf 字庫,設字體為fontSize=20號
   IntPtr font = SDL_ttf.TTF_OpenFont(ttfPath, fontSize);
   if (font == IntPtr.Zero)
   {
    Console.WriteLine("open font failed." + SDL.SDL_GetError());
    return;
   }
 
   // 設置文字顏色
   SDL.SDL_Color color;
   color.a = 255;
   color.r = 255;
   color.g = 255;
   color.b = 255;
 
   // 渲染文字效果
   //IntPtr surface = SDL_ttf.TTF_RenderUTF8_Blended(font, text, color);
   IntPtr surface = SDL_ttf.TTF_RenderUNICODE_Blended(font, text, color);
   if (surface == IntPtr.Zero)
   {
    Console.WriteLine("show surface failed." + SDL.SDL_GetError());
   }
   IntPtr texture = SDL.SDL_CreateTextureFromSurface(renderer, surface);
   if (texture == IntPtr.Zero)
   {
    Console.WriteLine("create texture failed." + SDL.SDL_GetError());
   }
   SDL.SDL_FreeSurface(surface);
   // 關閉字體
   SDL_ttf.TTF_CloseFont(font);
 
   // 停止顯示
   SDL_ttf.TTF_Quit();
 
   //設置紋理的數據
   SDL.SDL_Rect destrect;
   destrect.x = 0;
   destrect.y = 0;
   destrect.w = text.Length * 20;
   destrect.h = 20;
 
   SDL.SDL_Rect srcrect = destrect; 
   SDL.SDL_RenderClear(renderer);
   SDL.SDL_RenderCopy(renderer, texture, ref srcrect, ref destrect);
   SDL.SDL_RenderPresent(renderer);
   SDL.SDL_DestroyTexture(texture);
   SDL.SDL_DestroyRenderer(renderer);
  }
 }

事件測試字幕添加:

需要的引用庫下載:https://www.libsdl.org/projects/SDL_ttf/

 /// <summary>
  /// 字幕疊加****需要添加三個dll庫:SDL2_ttf.dll 、libfreetype-6.dll 、zlib1.dll
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void mbtnAddFontText_Click(object sender, EventArgs e)
  {
   Console.WriteLine("疊加字幕...............");
 
   sdlTTF = new SDLTTF(sdlVideo.sdlrenderer);
   // 中英文都需要兼容
   string text = "Hello 世界!";
   // 設置一個字體庫并設置字體大小和顯示文字內容
   sdlTTF.ShowText("simkai.ttf",12, text);
  }

測試效果圖:

怎么在C# 中利用SDL2實現一個視頻播放窗口截圖

如果是播放過程中顯示字幕一定要在視頻渲染完成后渲染字幕,如下面工具類的方法:

/// <summary>
  /// 播放視頻
  /// </summary>
  /// <param name="width"></param>
  /// <param name="height"></param>
  /// <param name="pixels"></param>
  /// <param name="pixelsSize"></param>
  /// <param name="pitch"></param>
  /// <returns></returns>
  public int SDL_Display(int width, int height, IntPtr pixels, int pixelsSize,
   int pitch)
  {
   lock (this)
   {
    while (isPause)
    {
     SDL.SDL_Delay(20);//延遲播放
    }
 
    #region SDL 視頻數據渲染播放
    //設置紋理的數據
    sdlrect.x = 0;
    sdlrect.y = 0;
    sdlrect.w = width;
    sdlrect.h = height;
    SDL.SDL_UpdateTexture(sdltexture, ref sdlrect, pixels, pitch);
    //SDL.SDL_UpdateTexture(sdltexture, IntPtr.Zero, pixels, pitch);//此處代碼導致播放窗口綠色陰影
    //復制紋理信息到渲染器目標
    SDL.SDL_RenderClear(sdltexture);
    //SDL.SDL_Rect srcRect = sdlrect;
    //SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
 
    SDL.SDL_RenderCopy(sdlrenderer, sdltexture, IntPtr.Zero, IntPtr.Zero);
    //字幕渲染顯示-特別提醒:此處必須放置于視頻渲染之后,否則字幕不會顯示
    if (ttfText!=null&&!ttfText.Equals(""))
    {
     RenderToShowTTF(ttfText);
    }
    //else
    //{
    // RenderToShowTTF( "未設置字幕內容");
    //}
    //視頻渲染顯示
    SDL.SDL_RenderPresent(sdlrenderer);
    //SDL.SDL_Delay(40);
    //SDL.SDL_PollEvent(out sdlevent);
    //switch (sdlevent.type)
    //{
    // case SDL.SDL_EventType.SDL_QUIT:
    //  SDL.SDL_Quit();
    //  return -1;
    // default:
    //  break;
    //}
    return 0;
   } 
 
   //SDL.SDL_RenderClear(sdlrenderer);
   //SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
   //SDL.SDL_RenderPresent(sdlrenderer);
   Delay 40ms 
   //SDL.SDL_Delay(40);
   #endregion 
 
   //#region SDL 視頻數據渲染播放
   //設置紋理的數據
   sdlrect.x = 0;
   sdlrect.y = 0;
   sdlrect.w = width;
   sdlrect.h = height;
   SDL.SDL_UpdateTexture(sdltexture, ref sdlrect, pixels, pitch);
   //復制紋理信息到渲染器目標
   SDL.SDL_Rect srcRect = sdlrect;
   SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref srcRect, ref sdlrect);
   //視頻渲染顯示
   SDL.SDL_RenderPresent(sdlrenderer);
   //SDL.SDL_Delay(40);
   SDL.SDL_PollEvent(out sdlevent);
   switch (sdlevent.type)
   {
    case SDL.SDL_EventType.SDL_QUIT:
     SDL.SDL_Quit();
     return -1;
    default:
     break;
   }
   return 0;
   //#endregion
  }
 
  /// <summary>
  /// 設置字幕顯示內容
  /// </summary>
  /// <param name="ttfPath"></param>
  /// <param name="fontSize"></param>
  public void SDL_TTF_TEXT(string ttfPath, string text, int fontSize)
  {
   this.ttfPath = ttfPath;
   this.ttfText = text;
   this.ttfFontSize = fontSize;
  }
 
  /// <summary>
  /// 渲染字幕
  /// </summary>
  /// <param name="text"></param>
  private void RenderToShowTTF(string text)
  {
   // 初始化 ttf
   if (SDL_ttf.TTF_Init() < 0)
   {
    Console.WriteLine("SDL_ttf.TTF_Init() failed.");
    return;
   }
   // 是否初始化完成
   int was_init = SDL_ttf.TTF_WasInit();
 
   if (was_init == 1)
    // SDL_ttf was already initialized
    Console.WriteLine("SDL_ttf was already initialized");
   else if (was_init == 0)
    // SDL_ttf was not already initialized
    Console.WriteLine("SDL_ttf was not already initialized");
 
   //如:打開ttfPath=simfang.ttf 字庫,設字體為fontSize=20號
   IntPtr font = SDL_ttf.TTF_OpenFont(ttfPath, ttfFontSize);
   if (font == IntPtr.Zero)
   {
    Console.WriteLine("open font failed." + SDL.SDL_GetError());
    return;
   }
 
   // 設置文字字體
   SDL_ttf.TTF_SetFontStyle(font, SDL_ttf.TTF_STYLE_BOLD);
 
   // 設置文字顏色
   SDL.SDL_Color color;
   color.a = 255;
   color.r = 255;
   color.g = 255;
   color.b = 255;
 
   // 渲染文字效果
   //IntPtr surface = SDL_ttf.TTF_RenderText_Blended(font, text, color);
   IntPtr surface = SDL_ttf.TTF_RenderUTF8_Blended(font, text, color);
   //IntPtr surface = SDL_ttf.TTF_RenderUNICODE_Blended(font, text, color);
   if (surface == IntPtr.Zero)
   {
    Console.WriteLine("show surface failed." + SDL.SDL_GetError());
   }
   IntPtr texture = SDL.SDL_CreateTextureFromSurface(sdlrenderer, surface);
   if (texture == IntPtr.Zero)
   {
    Console.WriteLine("create texture failed." + SDL.SDL_GetError());
   }
 
   SDL.SDL_FreeSurface(surface);
   // 關閉字體
   SDL_ttf.TTF_CloseFont(font);
 
   // 停止顯示
   SDL_ttf.TTF_Quit();
 
   // 計算合適的寬度和高度
   int texWidth = 0;
   int texHeight = 0;
   uint format = 0;
   int access = 0;
   // 下面這行代碼解決字體虛浮不清問題
   SDL.SDL_QueryTexture(texture, out format, out access, out texWidth, out texHeight);
   //設置紋理的數據
   SDL.SDL_Rect destrect;
   destrect.x = 0;
   destrect.y = 0;
   destrect.w = texWidth;
   destrect.h = texHeight;
 
   SDL.SDL_Rect srcrect = destrect;
   SDL.SDL_RenderCopy(sdlrenderer, texture, ref srcrect, ref destrect);
  }

效果就會好很多:

怎么在C# 中利用SDL2實現一個視頻播放窗口截圖

請看這里的“中華人民共和國”

注意:

常用中英文ttf字體包中包含了:times new roman,中山行書百年紀念版,calibri,Christopherhand,DejaVuSansMono,方正蘭亭黑,James Fajardo,Monaco,微軟雅黑,仿宋,黑體,楷體,宋體,yahei_mono,仿宋_GB2312,楷體_GB2312,迷你簡行楷碑等。

本文使用的是simkai.ttf。

下面是部分字體文件名:

bb1550.ttf

calibri.ttf

calibrib.ttf

calibrii.ttf

calibriz.ttf

comesinhandy.ttf

DejaVuSansMono-Bold.ttf

DejaVuSansMono-BoldOblique.ttf

DejaVuSansMono-Oblique.ttf

DejaVuSansMono.ttf

DroidSansFallback.ttf

James_Fajardo.ttf

Monaco.ttf

msyh.ttf

msyhbd.ttf

simfang.ttf

simhei.ttf

simkai.ttf

simsun.ttc

times.ttf

timesbd.ttf

timesbi.ttf

timesi.ttf

yahei_mono.ttf

以上就是怎么在C# 中利用SDL2實現一個視頻播放窗口截圖,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

津市市| 鄂托克旗| 和林格尔县| 安徽省| 谢通门县| 略阳县| 禄劝| 科技| 鄂托克旗| 屯留县| 克东县| 榆林市| 松溪县| 武安市| 金堂县| 芦溪县| 广水市| 永和县| 文登市| 左云县| 十堰市| 广西| 新邵县| 阿拉善盟| 若尔盖县| 洞头县| 化州市| 崇信县| 娄烦县| 绥宁县| 广饶县| 精河县| 临夏市| 兴仁县| 长泰县| 河曲县| 和田市| 舞钢市| 四子王旗| 称多县| 休宁县|