服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# wpf 通过HwndHost渲染视频的实现方法

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

2022-12-08 11:29Alfred-N C#

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

前言

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

一、如何实现

通过继承HwndHost并实现抽象方法即可作为一个带句柄的wpf控件在xaml中使用,代码如下:
win32Api版本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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版本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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中使用上述实现的控件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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事件中才能获取到句柄,在此事件之前句柄还没有生成。

?
1
2
3
4
5
6
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渲染视频的实现方法

总结

通过HwndHost渲染视频,本质是获取Hwnd渲染视频,获取Hwnd后渲染方式可以有多种选择,用gdi、d3d、opengl都可以,其实就是相当于在MFC上渲染视频,很多方案有可以通用。但这种方法也有一些缺点,其渲染和wpf控件有冲突,无法同时存在,即视频上面无法放置任何控件、也无法做到圆角播放框。

到此这篇关于C# wpf 通过HwndHost渲染视频的文章就介绍到这了,更多相关C# 渲染视频内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/u013113678/article/details/121275982

延伸 · 阅读

精彩推荐
  • C#C#.NET 图片水印添加代码

    C#.NET 图片水印添加代码

    这篇文章主要为大家详细介绍了C#.NET 图片水印添加代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    熊哥8642021-12-01
  • C#详解c# 泛型类的功能

    详解c# 泛型类的功能

    这篇文章主要介绍了c# 泛型类的功能,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下...

    一只独行的猿9702022-10-13
  • C#C#中字符串编码处理

    C#中字符串编码处理

    C#中字符串编码处理,需要的朋友可以参考一下...

    C#菜鸟教程4622020-12-19
  • C#关于nancy中的身份验证

    关于nancy中的身份验证

    在nancy中,身份验证分为basic ,form ,token,stateless几种,basic和form这里不说了,本文重点介绍token验证,需要的朋友一起看看吧...

    99re7572021-10-26
  • C#C#利用ReportViewer生成报表

    C#利用ReportViewer生成报表

    这篇文章主要为大家详细介绍了C#利用ReportViewer生成报表的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    飞翔的月亮4702022-01-06
  • C#猜数字小游戏C#实现代码

    猜数字小游戏C#实现代码

    这篇文章主要为大家详细介绍了C#实现猜数字小游戏的代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    考研必胜10632022-10-25
  • C#C# 中使用 Exceptionless的方法

    C# 中使用 Exceptionless的方法

    这篇文章主要介绍了C# 中使用 Exceptionless的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    UP技术控8062022-10-21
  • C#Quartz.Net任务和触发器实现方法详解

    Quartz.Net任务和触发器实现方法详解

    这篇文章主要介绍了Quartz.Net任务和触发器实现方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可...

    David Huang4582022-10-19