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 ...
随机推荐
- MongoDB学习笔记~关于官方驱动集成IQueryable之后的一些事
回到目录 关于官方驱动集成IQueryable之后的一些事,有好事也有坏事,好事就是它会将你的linq语句非常友好的翻译成MongoDB语句,而坏事就是有一些linq语句不会被翻译,不会被翻译的代价就 ...
- mysql编码格式设置
两个地方能影响到mysql的编码格式 ①.[mysql]下default-character-set=GBK//指的是客户端输入的内容的编码(GBK只是支持中英文)②.[mysqld]下# The d ...
- vs与qt配置
1.error: these Qt version are inaccessible;Qt5.4.0 in D:\qt5.4.0\5.4\msvc2013Make sure that you have ...
- Docker 1.12 集群
环境介绍 虚拟机两台,vmware ,网络为NAT node139:192.168.190.139 Node140: 192.168.190.140 设置hostname 以139为例 ...
- C库函数使用与总结之字符串处理函数
1. memcpy(拷贝内存内容) [头文件]#include <string.h> [函数原型]void *memcpy(void *dest, const void *src, siz ...
- 关于JS的编码转换问题
在进行JS开发过程中,尤其是在开发报表时,报表已集成到Web页面中,通过在页面传递参数至报表中时,会发现有时某些参数值,传递到报表中是显示为问号或乱码等等一系列不能正常显示的情况. 这是由于浏览器和报 ...
- Android APP 读取 AndroidManifest.xml 中的版本信息详解
APP都会涉及到版本的问题,Android APP的版本信息保存在AndroidManifest.xml文件的顶部.如下图: 有2个属性表示,“android:versionCode”和“androi ...
- java笔记
ANT概述:http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html http://baitai.iteye.com/blog/7 ...
- 【2016-11-3】【坚持学习】【Day18】【我认识的ORM】
我学过或者用过的ORM有几种 EF NHibernate DevExpress xpo (第一家公司用这个,也是很省力的orm,现在已经不记得了.) 今天晚上想找一下有没有轻量级的orm: flue ...
- c++ const函数是什么意思
c++ const函数是什么意思 只读类型 const修饰在类的成员函数的尾部,表示这个函数不会修改类的成员.相当于this指针是const的.