C#?wpf?通過HwndHost渲染視頻的實(shí)現(xiàn)方法
前言
日常開發(fā)中,特別是音視頻開發(fā),需要在界面上渲染視頻,比如制作一個(gè)播放器、或者視頻編輯工具、以及視頻會(huì)議客戶端。通常拿到的是像素格式數(shù)據(jù),此時(shí)需要渲染到wpf窗口上就需要一定的方法,本文介紹一種通過hwnd渲染的方法,控件既能提供hwnd又能嵌入wpf窗口里。
一、如何實(shí)現(xiàn)
通過繼承HwndHost并實(shí)現(xiàn)抽象方法即可作為一個(gè)帶句柄的wpf控件在xaml中使用,代碼如下:
win32Api版本:
class NativeHost : HwndHost { new public IntPtr Handle { get { return (IntPtr)GetValue(HandleProperty); } set { SetValue(HandleProperty, value); } } // Using a DependencyProperty as the backing store for Hwnd. This enables animation, styling, binding, etc... public static readonly DependencyProperty HandleProperty = DependencyProperty.Register("Handle", typeof(IntPtr), typeof(NativeHost), new PropertyMetadata(IntPtr.Zero)); protected override HandleRef BuildWindowCore(HandleRef hwndParent) { Handle = CreateWindowEx( 0, "static", "", WS_CHILD | WS_VISIBLE | LBS_NOTIFY, 0, 0, (int)Width, (int)Height, hwndParent.Handle, IntPtr.Zero, IntPtr.Zero, 0); return new HandleRef(this, Handle); } protected override void DestroyWindowCore(HandleRef hwnd) { DestroyWindow(hwnd.Handle); } const int WS_CHILD = 0x40000000; const int WS_VISIBLE = 0x10000000; const int LBS_NOTIFY = 0x001; [DllImport("user32.dll")] internal static extern IntPtr CreateWindowEx(int exStyle, string className, string windowName, int style, int x, int y, int width, int height, IntPtr hwndParent, IntPtr hMenu, IntPtr hInstance, [MarshalAs(UnmanagedType.AsAny)] object pvParam); [DllImport("user32.dll")] static extern bool DestroyWindow(IntPtr hwnd); }
HwndSource版本:
class NativeHost : HwndHost { new public IntPtr Handle { get { return (IntPtr)GetValue(HandleProperty); } set { SetValue(HandleProperty, value); } } // Using a DependencyProperty as the backing store for Hwnd. This enables animation, styling, binding, etc... public static readonly DependencyProperty HandleProperty = DependencyProperty.Register("Handle", typeof(IntPtr), typeof(NativeHost), new PropertyMetadata(IntPtr.Zero)); HwndSource _source; protected override HandleRef BuildWindowCore(HandleRef hwndParent) { _source = new HwndSource(0, WS_CHILD | WS_VISIBLE | LBS_NOTIFY, 0,0,0, (int)Width, (int)Height, "nativeHost", hwndParent.Handle); Handle = _source.Handle; return new HandleRef(this,Handle); } protected override void DestroyWindowCore(HandleRef hwnd) { _source.Dispose(); } const int WS_CHILD = 0x40000000; const int WS_VISIBLE = 0x10000000; const int LBS_NOTIFY = 0x001; }
二、使用方式
直接在xaml中使用上述實(shí)現(xiàn)的控件:
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApp1" xmlns:interop="clr-namespace:System.Windows.Interop;assembly=PresentationFramework" mc:Ignorable="d" Title="MainWindow" Height="440" Width="640" > <Grid> <!--控件有個(gè)Handle屬性,可以綁定,使用OneWaytoSource賦值給viewModel--> <local:NativeHost x:Name="NH_Plane" Height="360" Width="640" ></local:NativeHost> </Grid> </Window>
在Loaded事件中才能獲取到句柄,在此事件之前句柄還沒有生成。
private void Window_Loaded(object sender, RoutedEventArgs e) { //獲取控件句柄 var hwnd=NH_Plane.Handle //通過句柄進(jìn)行渲染 }
三、示例
示例代碼:
https://download.csdn.net/download/u013113678/40304426
注:示例代碼與文本所有代碼基本一致,渲染部分在c++的dll不可見,請(qǐng)根據(jù)需要下載。
效果預(yù)覽:
總結(jié)
通過HwndHost渲染視頻,本質(zhì)是獲取Hwnd渲染視頻,獲取Hwnd后渲染方式可以有多種選擇,用gdi、d3d、opengl都可以,其實(shí)就是相當(dāng)于在MFC上渲染視頻,很多方案有可以通用。但這種方法也有一些缺點(diǎn),其渲染和wpf控件有沖突,無法同時(shí)存在,即視頻上面無法放置任何控件、也無法做到圓角播放框。
到此這篇關(guān)于C# wpf 通過HwndHost渲染視頻的文章就介紹到這了,更多相關(guān)C# 渲染視頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# httpwebrequest訪問HTTPS錯(cuò)誤處理方法
下面小編就為大家?guī)硪黄狢# httpwebrequest訪問HTTPS錯(cuò)誤處理方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01解析C#中用Process類殺死進(jìn)程,執(zhí)行命令的深入分析
本篇文章是對(duì)C#中用Process類殺死進(jìn)程,執(zhí)行命令進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#中括號(hào)強(qiáng)轉(zhuǎn)、as、is區(qū)別詳解
本文主要介紹了C#中括號(hào)強(qiáng)轉(zhuǎn)、as、is區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02c#實(shí)現(xiàn)最簡潔的快速排序(你絕對(duì)可以看懂)
這篇文章主要給大家介紹了關(guān)于利用c#實(shí)現(xiàn)如何最簡潔的快速排序,實(shí)現(xiàn)的方法你絕對(duì)可以看懂,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Unity之跑馬燈抽獎(jiǎng)效果單抽與連抽(附demo)
這篇文章主要介紹了Unity之跑馬燈抽獎(jiǎng)效果單抽與連抽,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05C#實(shí)現(xiàn)獲取程序路徑方法小結(jié)
這篇文章主要介紹了C#實(shí)現(xiàn)獲取程序路徑方法,實(shí)例分析了C#獲取文件路徑的各種常用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08