视频图像处理系列 索引

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. IE8 ajax缓存问题

    娘希匹,又遇到缓存问题了. 下面的代码,在其他浏览器都是正常的,但是在IE8中出现诡异问题. $.ajax({ url:dataUrl, data:encodeURI(currentjsonform) ...

  2. Linux From Scratch(从零开始构建Linux系统,简称LFS)- Version 7.7(三)

    八. 构建LFS系统 1. 准备虚拟内核文件系统 内核会挂载几个文件系统用于自己和用户空间程序交换信息.这些文件系统是虚拟的,并不占用实际磁盘空间, 它们的内容会放在内存里. mkdir -pv $L ...

  3. Euclid求最大公约数

    Euclid求最大公约数算法 #include <stdio.h> int gcd(int x,int y){ while(x!=y){ if(x>y) x=x-y; else y= ...

  4. javascrip中parentNode和offsetParent之间的区别

    首先是 parentNode 属性,这个属性好理解,就是在 DOM 层次结构定义的上下级关系,如果元素A包含元素B,那么元素B就可以通过 parentElement 属性来获取元素A. 要明白 off ...

  5. 帆软报表FineReport SQLServer数据库连接失败常见解决方案

    1. 问题描述 帆软报表FineReport客户端连接SQLServer(2000.2005等),常常会出现如下错误:com.microsoft.sqlserver.jdbc.SQLServerExc ...

  6. Redis学习总结

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API,其实当前最热门的NoSQL数据库之一,NoSQL还包括了Mem ...

  7. python 聊天室

    server端程序 # -*- coding: utf-8 -*- #!/usr/bin/python """ """ import soc ...

  8. jdbc java数据库连接 6)类路径读取——JdbcUtil的配置文件

    之前的代码中,以下代码很多时候并不是固定的: private static String url = "jdbc:mysql://localhost:3306/day1029?useUnic ...

  9. sublime 插件

    由于之前的代码可视化方案太复杂,分析时间太长,不实用,另一方面是而且工作以后业余时间大大减少,因此决定放弃原有路线,从工作中最迫切的需求着手,逐步构建一个实用的工具. 新的方法仍然依赖understa ...

  10. WP-Cumulus插件

    链接: 5KB搞定wp-cumulus中文3D Tag问题 WordPress plugin: WP-Cumulus Flash based tag cloud WP-Cumulus支持的3D标签云实 ...