未将对象的引用设置到对象的实例,怎么解决啊

qijun1994 2013-06-30 12:01:35
直接上代码,从网上下载的一个将AVI视频拆分为BMP图像文件的程序。
自己单步调试了一下,发现 private void cmdGetBitmaps_Click(object sender, EventArgs e)函数中,执行到 bih = (BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(pDib), bih.GetType());
语句之后,便没有执行后面的处理得到BMP文件的代码,直接跳到了报错,未将对象的引用设置到对象的实例。
前面已经定义了一下BITMAPINFOHEADER bih = new BITMAPINFOHEADER();为什么还会报错呢?求大神指点
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);

[DllImport("avifil32.dll")] static extern int AVIFileInfo(int pFile, ref AVI_FILE_INFO aviInfo, int Size);
[DllImport("avifil32.dll")] static extern int AVIStreamGetFrameOpen(IntPtr pAvi, ref BITMAPINFOHEADER bih);
[DllImport("avifil32.dll")] public static extern int AVIStreamGetFrame(int pGetFrame, int Pos);
[DllImport("avifil32.dll")] public static extern int AVIStreamGetFrameClose(int pGetFrame);

public struct AVI_RECT
{
public UInt32 left;
public UInt32 top;
public UInt32 right;
public UInt32 bottom;
};

public struct AVI_STREAM_INFO
{
public UInt32 fccType;
public UInt32 fccHandler;
public UInt32 dwFlags;
public UInt32 dwCaps;
public UInt16 wPriority;
public UInt16 wLanguage;
public UInt32 dwScale;
public UInt32 dwRate;
public UInt32 dwStart;
public UInt32 dwLength;
public UInt32 dwInitialFrames;
public UInt32 dwSuggestedBufferSize;
public UInt32 dwQuality;
public UInt32 dwSampleSize;
public AVI_RECT rcFrame;
public UInt32 dwEditCount;
public UInt32 dwFormatChangeCount;
public string szName;
} ;


public struct AVI_FILE_INFO
{
long dwMaxBytesPerSecond;
long dwFlags;
long dwCaps;
long dwStreams;
long dwSuggestedBufferSize;
long dwWidth;
long dwHeight;
long dwScale;
long dwRate;
long dwLength;
long dwEditCount;
long szFileType;
}

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct BITMAPINFOHEADER
{
public UInt32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public Int16 biPlanes;
public Int16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BITMAPFILEHEADER
{
public Int16 bfType;
public Int32 bfSize;
public Int16 bfReserved1;
public Int16 bfReserved2;
public Int32 bfOffBits;
}

int writeDenied = 0x20;
BITMAPINFOHEADER bih = new BITMAPINFOHEADER();

int streamtypeVIDEO = 1935960438; // 'equivalent to: mmioStringToFOURCC("vids", 0&)

int AVIFile = 0;
IntPtr aviStream;


public Form1()
{
InitializeComponent();
}

private IntPtr IntPtr(int pDib)
{
throw new Exception("The method or operation is not implemented.");
}

private void Form1_Load(object sender, EventArgs e)
{
AVIFileInit();
}

private void cmdGetBitmaps_Click(object sender, EventArgs e)
{
int result;
String fileName;
int pointerAVI = 0;
int pointerAVIStream = 0;
int numberFrames = 0;
int firstFrame = 0;

AVI_FILE_INFO fileInfo = new AVI_FILE_INFO();
AVI_STREAM_INFO streamInfo = new AVI_STREAM_INFO();

fileName = "D:\\highway.avi";

try
{
result = AVIFileOpen(ref pointerAVI, fileName, writeDenied, 0);
result = AVIFileGetStream(pointerAVI, out aviStream, streamtypeVIDEO, 0);
firstFrame = AVIStreamStart(aviStream.ToInt32());
numberFrames = AVIStreamLength(aviStream.ToInt32());

result = AVIFileInfo(pointerAVI, ref fileInfo, System.Runtime.InteropServices.Marshal.SizeOf(fileInfo));
result = AVIStreamInfo(aviStream.ToInt32(), ref streamInfo, Marshal.SizeOf(streamInfo));

bih.biBitCount = 24;
bih.biClrImportant = 0;
bih.biClrUsed = 0;
bih.biCompression = 0;
bih.biHeight = (Int32)(streamInfo.rcFrame.bottom);
bih.biWidth = (Int32)streamInfo.rcFrame.right;
bih.biPlanes = 1;
bih.biSize = (UInt32)Marshal.SizeOf(bih);
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;

int getFrameObject = 0;

String dstFileName = "D:\\bitmap";

getFrameObject = AVIStreamGetFrameOpen(aviStream, ref bih);



for (int i = firstFrame; i < numberFrames - 1; i++)
{
int pDib = AVIStreamGetFrame(getFrameObject, firstFrame + i);


bih = (BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(pDib), bih.GetType());

//Copy the image
byte[] bitmapData = new byte[bih.biSizeImage];
int address = pDib + Marshal.SizeOf(bih);
for (int offset = 0; offset < bitmapData.Length; offset++)
{
bitmapData[offset] = Marshal.ReadByte(new IntPtr(address));
address++;
}


//Copy bitmap info
byte[] bitmapInfo = new byte[Marshal.SizeOf(bih)];
IntPtr ptr;
ptr = Marshal.AllocHGlobal(bitmapInfo.Length);
Marshal.StructureToPtr(bih, ptr, false);
address = ptr.ToInt32();
for (int offset = 0; offset < bitmapInfo.Length; offset++)
{
bitmapInfo[offset] = Marshal.ReadByte(new IntPtr(address));
address++;
}

//Create file header
BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
bfh.bfType = BMP_MAGIC_COOKIE;
bfh.bfSize = (Int32)(55 + bih.biSizeImage); //size of file as written to disk
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);



//Create or overwrite the destination file
FileStream fs = new FileStream(dstFileName + i.ToString() + ".bmp", System.IO.FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);

//Write header
bw.Write(bfh.bfType);
bw.Write(bfh.bfSize);
bw.Write(bfh.bfReserved1);
bw.Write(bfh.bfReserved2);
bw.Write(bfh.bfOffBits);
//Write bitmap info
bw.Write(bitmapInfo);
//Write bitmap data
bw.Write(bitmapData);
bw.Close();
fs.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}
}
...全文
501 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
linghujun2000 2013-07-08
  • 打赏
  • 举报
回复
我也遇到这个问题,怎么办?坐等答案
tcmakebest 2013-06-30
  • 打赏
  • 举报
回复
那就可能是pDib是0啊
  • 打赏
  • 举报
回复
你在异常时,从vs调试器上,确认了 bih 是null了吗? 没有调试,你怎么认定一定是这个变量值的问题?
threenewbee 2013-06-30
  • 打赏
  • 举报
回复
http://hi.baidu.com/dongyuejiang/item/1badc7eadd263c2f5b7cfbe9
qijun1994 2013-06-30
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
你在异常时,从vs调试器上,确认了 bih 是null了吗? 没有调试,你怎么认定一定是这个变量值的问题?
查看了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(如果系统无法找到解压器),怎么理解啊,还望大神指点,十分感谢
qijun1994 2013-06-30
  • 打赏
  • 举报
回复
引用 2 楼 tcmakebest 的回复:
那就可能是pDib是0啊
是的,pDib的值为0,bih没有问题。 再调调看吧,十分感谢
qijun1994 2013-06-30
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
你在异常时,从vs调试器上,确认了 bih 是null了吗? 没有调试,你怎么认定一定是这个变量值的问题?
十分感谢,跟踪了一下,发现是getFrameObject = AVIStreamGetFrameOpen(aviStream, ref bih);这一句有问题,getFrameObject得值始终为0,导致后面的 int pDib = AVIStreamGetFrame(getFrameObject, firstFrame + i); pDib的值也为0,而aviStream与 bih都不为Null。

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧