C#使用 DirectX SDK 9做视频播放器 并在视频画线添加文字 VMR9
视频图像处理系列 索引
VS2013下测试通过。
在百度中搜索关键字“DirectX SDk”,或者进入微软官网https://www.microsoft.com/en-us/download/details.aspx?id=6812,
下载最新版本的SDK,即DirectX SDK June10
下载后安装。
下载DirectShowLib-2005.dll并引用到工程里。
代码如下:
using DirectShowLib; using Microsoft.DirectX.Direct3D;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml; namespace VideoConfig
{
public partial class frmMain : Form
{private Color colorKey = Color.Violet; // The color use as ColorKey for GDI operations
private Bitmap alphaBitmap; // A ARGB bitmap used for Direct3D operations // Managed Direct3D magic number to retrieve unmanaged Direct3D interfaces
private const int DxMagicNumber = -;
private Device device = null; // A Managed Direct3D device
private PresentParameters presentParams;
private Surface surface = null; // A Direct3D suface filled with alphaBitmap
private IntPtr unmanagedSurface; // A pointer on the unmanaged surface private IFilterGraph2 graphBuilder = null;
private IMediaControl mediaControl = null;
private IBaseFilter vmr9 = null;
private IVMRMixerBitmap9 mixerBitmap = null;
private IVMRWindowlessControl9 windowlessCtrl = null;
private bool handlersAdded = false; // Needed to remove delegates public frmMain()
{
InitializeComponent();this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
// Init Managed Direct3D
InitializeDirect3D(); CloseInterfaces();
BuildGraph(@"F:\测试视频.avi");
RunGraph();
} private void InitializeDirect3D()
{
Device.IsUsingEventHandlers = false; // Basic Presentation Parameters...
presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard; // Assume a hardware Direct3D device is available
// Add MultiThreaded to be safe. Each DirectShow filter runs in a separate thread...
device = new Device(
,
DeviceType.Hardware,
this,
CreateFlags.SoftwareVertexProcessing | CreateFlags.MultiThreaded,
presentParams
); alphaBitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height, PixelFormat.Format24bppRgb); // Create a surface from our alpha bitmap
surface = new Surface(device, alphaBitmap, Pool.SystemMemory);
// Get the unmanaged pointer
unmanagedSurface = surface.GetObjectByValue(DxMagicNumber);
} private void CloseInterfaces()
{
if (mediaControl != null)
mediaControl.Stop(); if (handlersAdded)
RemoveHandlers(); if (vmr9 != null)
{
Marshal.ReleaseComObject(vmr9);
vmr9 = null;
windowlessCtrl = null;
mixerBitmap = null;
} if (graphBuilder != null)
{
Marshal.ReleaseComObject(graphBuilder);
graphBuilder = null;
mediaControl = null;
}
} private void RemoveHandlers()
{
// remove handlers when they are no more needed
handlersAdded = false;
this.pictureBox1.Paint -= new PaintEventHandler(MainForm_Paint);
this.pictureBox1.Resize -= new EventHandler(MainForm_ResizeMove);
this.pictureBox1.Move -= new EventHandler(MainForm_ResizeMove);
this.pictureBox1.MouseDown -= new MouseEventHandler(panel2_MouseDown);
this.pictureBox1.MouseUp -= new MouseEventHandler(panel2_MouseUp);
this.pictureBox1.MouseMove -= new MouseEventHandler(panel2_MouseMove);
SystemEvents.DisplaySettingsChanged -= new EventHandler(SystemEvents_DisplaySettingsChanged);
} private void AddHandlers()
{
// Add handlers for VMR purpose
this.pictureBox1.Paint += new PaintEventHandler(MainForm_Paint); // for WM_PAINT
this.pictureBox1.Resize += new EventHandler(MainForm_ResizeMove); // for WM_SIZE
this.pictureBox1.Move += new EventHandler(MainForm_ResizeMove); // for WM_MOVE
this.pictureBox1.MouseDown += new MouseEventHandler(panel2_MouseDown);
this.pictureBox1.MouseUp += new MouseEventHandler(panel2_MouseUp);
this.pictureBox1.MouseMove += new MouseEventHandler(panel2_MouseMove);
SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged); // for WM_DISPLAYCHANGE
handlersAdded = true;
} private void MainForm_Paint(object sender, PaintEventArgs e)
{
if (windowlessCtrl != null)
{
IntPtr hdc = e.Graphics.GetHdc();
int hr = windowlessCtrl.RepaintVideo(this.Handle, hdc);
e.Graphics.ReleaseHdc(hdc); System.Drawing.Pen pen = new System.Drawing.Pen(Color.LimeGreen);
pen.Width = ;
//实时的画矩形
e.Graphics.DrawRectangle(pen, point1.X, point1.Y, point2.X - point1.X, point2.Y - point1.Y); pen.Dispose();
}
} private void MainForm_ResizeMove(object sender, EventArgs e)
{
if (windowlessCtrl != null)
{
int hr = windowlessCtrl.SetVideoPosition(null, DsRect.FromRectangle(this.pictureBox1.ClientRectangle));
}
} private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
if (windowlessCtrl != null)
{
int hr = windowlessCtrl.DisplayModeChanged();
}
} private void RunGraph()
{
if (mediaControl != null)
{
int hr = mediaControl.Run();
DsError.ThrowExceptionForHR(hr);
}
} private void StopGraph()
{
if (mediaControl != null)
{
int hr = mediaControl.Stop();
DsError.ThrowExceptionForHR(hr);
}
} private void BuildGraph(string fileName)
{
int hr = ; try
{
graphBuilder = (IFilterGraph2)new FilterGraph();
mediaControl = (IMediaControl)graphBuilder; vmr9 = (IBaseFilter)new VideoMixingRenderer9(); ConfigureVMR9InWindowlessMode(); hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9");
DsError.ThrowExceptionForHR(hr); hr = graphBuilder.RenderFile(fileName, null);
DsError.ThrowExceptionForHR(hr); mixerBitmap = (IVMRMixerBitmap9)vmr9;
}
catch (Exception e)
{
CloseInterfaces();
MessageBox.Show("An error occured during the graph building : \r\n\r\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} private void ConfigureVMR9InWindowlessMode()
{
int hr = ; IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9)vmr9; // Not really needed for VMR9 but don't forget calling it with VMR7
hr = filterConfig.SetNumberOfStreams();
DsError.ThrowExceptionForHR(hr); // Change VMR9 mode to Windowless
hr = filterConfig.SetRenderingMode(VMR9Mode.Windowless);
DsError.ThrowExceptionForHR(hr); windowlessCtrl = (IVMRWindowlessControl9)vmr9; // Set "Parent" window
hr = windowlessCtrl.SetVideoClippingWindow(this.pictureBox1.Handle);
DsError.ThrowExceptionForHR(hr); // Set Aspect-Ratio
hr = windowlessCtrl.SetAspectRatioMode(VMR9AspectRatioMode.LetterBox);
DsError.ThrowExceptionForHR(hr); // Add delegates for Windowless operations
AddHandlers(); // Call the resize handler to configure the output size
MainForm_ResizeMove(null, null);
} ..............................
..............................
..............................
..............................
在视频画线添加文字参考之前的文章。主要是播放控件的MouseDown,MouseMove,MouseUp事件,把画好的东西记到数组内存里,然后在paint事件里重绘。
可以轻松的画线、各种图形、文字及叠加图片等。
C# winform开发:Graphics、pictureBox同时画多个矩形
平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#
更多内容可以参考DirectShowLib的例子及DirectX SDK的例子
C#使用 DirectX SDK 9做视频播放器 并在视频画线添加文字 VMR9的更多相关文章
- iphone SE 自带视频播放器要求的视频格式转换参数
- Android [VP]视频播放器播放本地视频时收到短信/彩信,需要界面提示 M
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 基于libVLC的视频播放器
本文来自于:http://blog.csdn.net/leixiaohua1020/article/details/42363079 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放 ...
- FFmpeg再学习 -- FFmpeg+SDL+MFC实现图形界面视频播放器
继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作最后一篇,主要是想学一下 MFC 创建和配置. 一.创建 MFC 工程 文件->新建->项目->Visual ...
- 使用vcastr22.swf做flash版网页视频播放器
flash的安装设置参考 Flash设置(各种版本浏览器包括低版本IE) 百度搜索下载vcastr22.swf文件 然后使用方式很简单,浏览器安装flash相关插件就能看了 视频路径主要在这里,视频 ...
- Android 音视频深入 十九 使用ijkplayer做个视频播放器(附源码下载)
项目地址https://github.com/979451341/Myijkplayer 前段时候我觉得FFmpeg做个视频播放器好难,虽然播放上没问题,但暂停还有通过拖动进度条来设置播放进度,这些都 ...
- 使用VLC Activex插件做网页版视频播放器
网上找的一个小例子,包括时长播放时间等等都有. mrl可以设置本地文件,这样发布网站后只能播放本地有的文件, 如果视频文件全在服务器上,其他电脑想看的话,则可以IIS上发布个视频文件服务器,类似htt ...
- 转:最简单的基于 DirectShow 的视频播放器
50行代码实现的一个最简单的基于 DirectShow 的视频播放器 本文介绍一个最简单的基于 DirectShow 的视频播放器.该播放器对于初学者来说是十分有用的,它包含了使用 DirectSho ...
- Android 视频播放器 (四):使用ExoPlayer播放视频
一.简介 ExoPlayer是一个Android应用层的媒体播放器,它提供了一套可替换Android MediaPlayer的API,可以播放本地或者是线上的音视频资源.ExoPlayer支持一些An ...
随机推荐
- Java多线程-并发容器
Java多线程-并发容器 在Java1.5之后,通过几个并发容器类来改进同步容器类,同步容器类是通过将容器的状态串行访问,从而实现它们的线程安全的,这样做会消弱了并发性,当多个线程并发的竞争容器锁的时 ...
- 一致性哈希算法与Java实现
原文:http://blog.csdn.net/wuhuan_wp/article/details/7010071 一致性哈希算法是分布式系统中常用的算法.比如,一个分布式的存储系统,要将数据存储到具 ...
- Ubuntu安装Svn,提供http访问
安装相关package sudo apt-get install subversion subversion-tools apache2 libapache2-svn apache2-utils 创建 ...
- POJ 2955 Brackets --最大括号匹配,区间DP经典题
题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...
- springboot Serving Web Content with Spring MVC
Serving Web Content with Spring MVC This guide walks you through the process of creating a "hel ...
- [tem]树状数组
通过差分可以玩区间: bi=ai-a(i-1) 查询时考虑位置对答案的贡献 推导一下 #include<iostream> #include<cstdio> #include& ...
- 原生态ajax
用户名是否被注册过? 创建出注册信息: <h1>注册信息</h1> <input type="text" name="txtName&quo ...
- 【转】【MySql】脚本备份数据库
#!/bin/bash #this is a script of mysql backup if [ ! -d /mydata/data1/backup ] ;then mkdir /mydata/d ...
- HTML编写需要注意的事项
HTML在编写过程中需要注意许多关键的事项,就如最近我在学习中遇到的问题如下: 代码规范问题: 在代码视图中编写代码,一定要规范的格式,不要把代码全部都写到一块,这样不仅影响效率,更加影响视觉,当出现 ...
- C#基础系列——一场风花雪月的邂逅:接口和抽象类
前言:最近一个认识的朋友准备转行做编程,看他自己边看视频边学习,挺有干劲的.那天他问我接口和抽象类这两个东西,他说,既然它们如此相像, 我用抽象类就能解决的问题,又整个接口出来干嘛,这不是误导初学者吗 ...