C# wpf 通过HwndHost渲染视频的实现方法
作者:Alfred-N 发布时间:2023-08-30 03:33:35
标签:C#,wpf,视频
前言
日常开发中,特别是音视频开发,需要在界面上渲染视频,比如制作一个播放器、或者视频编辑工具、以及视频会议客户端。通常拿到的是像素格式数据,此时需要渲染到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不可见,请根据需要下载。
效果预览:
来源:https://blog.csdn.net/u013113678/article/details/121275982
0
投稿
猜你喜欢
- 本文实例讲述了Java统计字符串中字符出现次数的方法。分享给大家供大家参考,具体如下:package com.wenzhi;import j
- Intellij是进行scala开发的一个非常好用的工具,可以非常轻松查看scala源码,当然用它来开发Java也是很爽的,之前一直在用sc
- 1介绍MVC框架是什么MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(control
- 错误Mybatis-Plus (简称MP) 是mybatis的一个增强工具,在mybatis的基础上只做增强不做改变,简化了开发效率。其实就
- 什么是ShiroApache Shiro是一个功能强大且易于使用的Java安全框架,它执行身份验证、授权、加密和会话管理。借助Shiro易于
- 作为开发人员,掌握开发环境下的调试技巧十分有必要。我们在编写java程序的过程中,经常会遇到各种莫名其妙的问题,为了检测程序是哪里出现问题,
- 数据库事务是被当作单个工作单元的操作序列。这些操作要么全部完成或全部不成功。事务管理是面向企业应用程序,以确保数据的完整性和一致性RDBMS
- 一、实现了Aware的接口Spring中有很多继承于aware中的接口,这些接口到底是做什么用到的,下面我们就一起来看看吧。Aware 接口
- 本文实例讲述了java数据结构与算法之快速排序。分享给大家供大家参考,具体如下:交换类排序的另一个方法,即快速排序。快速排序:改变了冒泡排序
- 一般而言,Spring的依赖注入有三种:构造器注入、setter注入以及接口注入。本文主要讲构造器注入与setter注入。1、构造器注入为了
- 基于SMTP发送一个简单的邮件首先,需要一个认证器:package No001_基于SMTP的文本邮件;import javax.mail.
- 先上代码新建一个Thread,代码如下:package com.thread.test;public class MyThread exte
- 一、错误处理原理分析使用SpringBoot创建的web项目中,当我们请求的页面不存在(http状态码为404),或者器发生异常(http状
- 1. List1.1 List 的常见方法方法描述boolean add(E e)尾插 evoid add(int index, E ele
- 一、定时任务的使用场景和常见的定时任务某个时间定时处理某个任务、发邮件、短信、消息提醒、订单通知、统计报表等定时任务划分单机定时任务:单机的
- 一、为何要使用netty开发由于之前已经用Java中的socket写过一版简单的聊天室,这里就不再对聊天室的具体架构进行细致的介绍了,主要关
- Java泛型是JDK 5引入的一个特性,它允许我们定义类和接口的时候使用参数类型,泛型在集合框架中被广泛使用。类型擦除是泛型中最让人困惑的部
- 简介MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)
- 基本概念Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数,并提供了同步机制。使用Semaphore可以控制并发访问
- 一、实战-内存溢出堆内存溢出栈内存溢出方法区溢出直接内存溢出二、实战-堆内存溢出演示堆内存溢出代码,并且定位问题总结堆内存溢出的场景与解决方