视频图像处理系列 索引

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# winform 动态画矩形 矩形大小可以拖动

GDI画图,判断鼠标点击点在某一画好的多边形、矩形、图形里

平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#

更多内容可以参考DirectShowLib的例子及DirectX SDK的例子

C#使用 DirectX SDK 9做视频播放器 并在视频画线添加文字 VMR9的更多相关文章

  1. iphone SE 自带视频播放器要求的视频格式转换参数

  2. Android [VP]视频播放器播放本地视频时收到短信/彩信,需要界面提示 M

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  3. 基于libVLC的视频播放器

    本文来自于:http://blog.csdn.net/leixiaohua1020/article/details/42363079 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放 ...

  4. FFmpeg再学习 -- FFmpeg+SDL+MFC实现图形界面视频播放器

    继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作最后一篇,主要是想学一下 MFC 创建和配置. 一.创建 MFC 工程 文件->新建->项目->Visual ...

  5. 使用vcastr22.swf做flash版网页视频播放器

    flash的安装设置参考  Flash设置(各种版本浏览器包括低版本IE) 百度搜索下载vcastr22.swf文件 然后使用方式很简单,浏览器安装flash相关插件就能看了 视频路径主要在这里,视频 ...

  6. Android 音视频深入 十九 使用ijkplayer做个视频播放器(附源码下载)

    项目地址https://github.com/979451341/Myijkplayer 前段时候我觉得FFmpeg做个视频播放器好难,虽然播放上没问题,但暂停还有通过拖动进度条来设置播放进度,这些都 ...

  7. 使用VLC Activex插件做网页版视频播放器

    网上找的一个小例子,包括时长播放时间等等都有. mrl可以设置本地文件,这样发布网站后只能播放本地有的文件, 如果视频文件全在服务器上,其他电脑想看的话,则可以IIS上发布个视频文件服务器,类似htt ...

  8. 转:最简单的基于 DirectShow 的视频播放器

    50行代码实现的一个最简单的基于 DirectShow 的视频播放器 本文介绍一个最简单的基于 DirectShow 的视频播放器.该播放器对于初学者来说是十分有用的,它包含了使用 DirectSho ...

  9. Android 视频播放器 (四):使用ExoPlayer播放视频

    一.简介 ExoPlayer是一个Android应用层的媒体播放器,它提供了一套可替换Android MediaPlayer的API,可以播放本地或者是线上的音视频资源.ExoPlayer支持一些An ...

随机推荐

  1. MongoDB学习笔记~索引提高查询效率

    回到目录 索引这个东西大家不会陌生,只要接触到稍微大一点的数据,都会用到这东西,它可以提升查询的速度,相当代价就是占用了更多的存储空间,这也是正常的,符合“能量守恒定理”,哈哈!今天说的是MongoD ...

  2. Linux Shell编程入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  3. 1、linux网络服务实验 用PuTTY连接Linux

    这个是大三下学期的Linux网络服务配置详解时,感觉老师上得简单,就整理下,岭南师范学院师弟妹有福,如果是蔡老师交的话,可以拿来预习,复习. 一.用PuTTY连接Linux ①.装有redhat系统的 ...

  4. runv nslistener源码分析

    nslistener的作用实质上就是将新的namespace里的veth网卡的配置信息通过proxy进程传输出来,并且利用该信息对tap进行相同的配置,最终用tap模拟新的namespace里的vet ...

  5. 【读书笔记《Bootstrap 实战》】5.电子商务网站

    构建了公司网站之后,接下来就可以考虑设计一个在线商店了. 此次的设计以上一章的设计为基础, 只是添加了一个包含如下元素的新页面: □ 包含商品小图.标题和说明的产品网格: □ 位于左侧的变懒,用于按类 ...

  6. return和finally的执行和联系

    1.try{}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后? 也许你的答案是在return之前,但往更细地说, ...

  7. Spring多种注入方式及注解实现DI

    一.Bean作用域 spring容器创建的时候,会将所有配置的bean对象创建出来,默认bean都是单例的.代码通过getBean()方法从容器获取指定的bean实例,容器首先会调用Bean类的无参构 ...

  8. JS重载

    Js 不支持函数的重载,可以用相同的名字在同一作用区域,定义两个函数,而不会引起错误,但真正使用的是最后一个. Js 不会验证传递给函数的参数个数是否和函数定义的参数的个数相同,开发人员定义的函数都可 ...

  9. 当Python在appium中使用if……else语句不好使怎么办

    前几天写自动化脚本的时候,有个地方需要用if--else判断获得的ID和name是哪个,从而决定点击哪个按钮,我用if--else去判断,可是总是提示我找不到对应的元素, 在网上爬了好久,最终终于找到 ...

  10. JSP之WEB服务器:Apache与Tomcat的区别 ,几种常见的web/应用服务器

    注意:此为2009年的blog,注意时效性(针对常见服务器)     APACHE是一个web服务器环境程序 启用他可以作为web服务器使用 不过只支持静态网页 如(asp,php,cgi,jsp)等 ...