C# wpf 通过HwndHost渲染视频的实现方法

作者:Alfred-N 时间:2023-08-30 03:33:35 

前言

日常开发中,特别是音视频开发,需要在界面上渲染视频,比如制作一个播放器、或者视频编辑工具、以及视频会议客户端。通常拿到的是像素格式数据,此时需要渲染到wpf窗口上就需要一定的方法,本文介绍一种通过hwnd渲染的方法,控件既能提供hwnd又能嵌入wpf窗口里。

一、如何实现

通过继承HwndHost并实现抽象方法即可作为一个带句柄的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中使用上述实现的控件:


<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>
         <!--控件有个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
   //通过句柄进行渲染
}

三、示例

示例代码:
https://download.csdn.net/download/u013113678/40304426
注:示例代码与文本所有代码基本一致,渲染部分在c++的dll不可见,请根据需要下载。
效果预览:

C# wpf 通过HwndHost渲染视频的实现方法

来源:https://blog.csdn.net/u013113678/article/details/121275982

标签:C#,wpf,视频
0
投稿

猜你喜欢

  • c#添加图片、文本水印到PDF文件

    2021-08-30 13:36:57
  • Java 如何实现时间控制

    2023-02-20 06:19:23
  • c#获取数组中最大数的值

    2022-07-20 07:49:02
  • Android实现视图轮播效果

    2023-04-14 08:52:10
  • Android开发实现SubMenu选项菜单和子菜单示例

    2022-05-31 06:32:10
  • 详解C++中的指针、数组指针与函数指针

    2023-04-11 05:35:24
  • Android7.0自动更新适配 包解析异常

    2022-02-13 11:21:00
  • 使用java实现各种数据统计图(柱形图,饼图,折线图)

    2022-03-09 22:12:50
  • android 网络请求库volley方法详解

    2022-01-01 01:34:30
  • Java加载property文件配置过程解析

    2023-10-07 07:53:03
  • Session过期后自动跳转到登录页面的实例代码

    2022-01-30 13:48:56
  • C#中Convert.ToString和ToString的区别分析

    2023-02-04 22:18:06
  • C#中WPF使用多线程调用窗体组件的方法

    2023-04-24 11:47:57
  • C# 禁用鼠标中间键的方法

    2022-01-24 04:17:24
  • SpringBoot整合Quartz实现定时任务详解

    2021-11-22 06:40:57
  • C#中使用JSON.NET实现JSON、XML相互转换

    2022-12-11 04:34:12
  • java实现仿射密码加密解密

    2022-10-09 04:04:49
  • Java计算文本MD5加密值的方法示例

    2023-11-15 13:18:48
  • springMVC如何将controller中Model数据传递到jsp页面

    2023-05-25 23:46:58
  • Android RecyclerView添加搜索过滤器的示例代码

    2022-03-08 21:44:49
  • asp之家 软件编程 m.aspxhome.com