日期:2014-05-17  浏览次数:21106 次

C# 运用windows自带的API,将AVI视频拆分为BMP图片,求指点
最近做的一个课程设计,需要拆分视频。
从网上下载了一个程序,但是调试之后,发现出现“未将对象的引用设置到对象的实例”的错误。

跟踪了一下,发现是getFrameObject = AVIStreamGetFrameOpen(aviStream, ref bih);这一句有问题,getFrameObject的值始终为0,导致后面的 int pDib = AVIStreamGetFrame(getFrameObject, firstFrame + i);  pDib的值也为0,而aviStream与 bih都不为Null。执行到 bih = (BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(pDib), bih.GetType());
语句之后,便没有执行后面的处理得到BMP文件的代码,直接跳到了报错,未将对象的引用设置到对象的实例。

又查阅了MSDN中对于AVIStreamGetFrameOpen function的解释,返回值的阐述如下:
Return value
Returns a GetFrame object that can be used with the AVIStreamGetFrame function. If the system cannot find a decompressor that can decompress the stream to the given format, or to any RGB format, the function returns NULL.
其中的If the system cannot find a decompressor(如果系统无法找到解压器),怎么理解啊,还望大神指点,十分感谢
附上代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Globalization;
using System.IO;
 
namespace GetAVIFrames
{
    public partial class Form1 : Form
    {
        public const int BMP_MAGIC_COOKIE = 19778;
        [DllImport("avifil32.dll")] public static extern void AVIFileInit();
        [DllImport("avifil32.dll")] public static extern int AVIFileOpen(ref int ppFile, String szFile, int Mode, int pclsidHandler);
        [DllImport("avifil32.dll")] public static extern int AVIFileGetStream(int pFile, out IntPtr ppAvi, int fccType, int lParam);
 
        [DllImport("avifil32.dll")] public static extern int AVIStreamStart(int pavi);
        [DllImport("avifil32.dll")] public static extern int AVIStreamLength(int pavi);
        [DllImport("avifil32.dll")] public static extern int AVIStreamInfo(int pAVIStream, ref AVI_STREAM_INFO pStrInfo, int Size);