视频图像处理系列 索引

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. ligerDialog的使用

    1.通过ViewBag来传值. @if (ViewBag.ReturnMessage != null) 2.脚本代码: 对话框设计与赋值问题. <script type="text/j ...

  2. 我心中的MySQL DBA

    原文网址链接:http://wangwei007.blog.51cto.com/68019/1718311 MySQL是一个跨平台的开源关系型数据库管理系统,目前MySQL被广泛地应用在Interne ...

  3. PostgreSQL-数据目录与pg_ctl

    # tail /etc/profile PATH="$PATH":/usr/lib/postgresql/9.2/bin/ export PATH export PGDATA=/v ...

  4. Linux 如何实现 VLAN - 每天5分钟玩转 OpenStack(12)

    LAN 表示 Local Area Network,本地局域网,通常使用 Hub 和 Switch 来连接 LAN 中的计算机.一般来说,两台计算机连入同一个 Hub 或者 Switch 时,它们就在 ...

  5. CentOS配置Nginx+Tomcat7的多站点支持

    注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. #准备工作# 在这之前需要先把Nginx和Tomcat安装好,具体见:<CentOS安装Nginx-1.6.2+安全配置&g ...

  6. php 上传文件

    $_FILES["file"]["name"] - 被上传文件的名称 $_FILES["file"]["type"] - ...

  7. SQL SERVER 2014 各个版本支持的功能

    转自:https://technet.microsoft.com/library/cc645993 转换箱规模限制 功能名称 Enterprise Business Intelligence Stan ...

  8. Java Generics and Collections-2.1

    2.1 子类化以及替换原理 为什么List<Integer> 不是List<Number> 的子类? 首先看下面的代码,这段代码是编译不过的 package java_gene ...

  9. Oracle约束(Constraint)详解

    概述 约束是数据库用来确保数据满足业务规则的手段,不过在真正的企业开发中,除了主键约束这类具有强需求的约束,像外键约束,检查约束更多时候仅仅出现在数据库设计阶段,真实环境却很少应用,更多是放到程序逻辑 ...

  10. linux学习(2)

    自从安装了虚拟机和各种工具软件之后,学习Linux的过程不断被打断,一直想把Ubuntu烧录到itop4412开发板里面去,却总是失败,感觉这个过程都加强我的抗打击能力了,现在来说说,对于一个第一次烧 ...