using System;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;using Server.Public;namespace Server.Multimedia.Video
{
/// <summary>
/// Compress 的摘要说明。
/// </summary>
public class ICBase
{
protected int hic;
protected ICMODE mode;
protected BITMAPINFO _in;
protected BITMAPINFO _out;
protected COMPVARS pp;
protected int fourcc;
protected COMPVARS Compvars
{
get{return this.pp;}
}
public ICBase(COMPVARS cp,BITMAPINFO biIn,ICMODE mode,int fourcc)
{
this.pp=cp;
this._in=biIn;
this.mode=mode;
this.fourcc=fourcc;
_out=new BITMAPINFO();
} #region
public int HIC
{
get{return this.hic;}
}
public BITMAPINFO InFormat
{
get{return this._in;}
}
public BITMAPINFO OutFormat
{
get{return this._out;}
}
public ICMODE Mode
{
get{return this.mode;}
}
public int Fourcc
{
get{return this.fourcc;}
}
#endregion public virtual void Open()//(System.IntPtr p)
{
this.hic=ICOpen(FOURCC.ICTYPE_VIDEO,this.fourcc,this.mode); 
this.Compvars.hic=this.hic;
}
public virtual byte[] Process(byte[] data)
{
return null;
} public virtual void Close()
{
ICClose(hic);
}

#region

[DllImport("MSVFW32.dll")]
public static extern void ICSeqCompressFrameEnd(ref COMPVARS pc);
/* [DllImport("MSVFW32.dll")]
public static extern IntPtr ICSeqCompressFrame(
 COMPVARS pc,  
 int uiFlags,  
 IntPtr lpBits, 
 ref bool pfKey,  
 ref int plSize  
 );*/
[DllImport("MSVFW32.dll")]
public static extern int ICCompressGetFormatSize(
int hic,       
ref BITMAPINFO lpbiInput  
);

[DllImport("MSVFW32.dll")]
public static extern bool ICSeqCompressFrameStart(
COMPVARS pc,        
ref BITMAPINFO lpbiIn  
);
[DllImport("MSVFW32.dll")]
public static extern int ICSeqCompressFrame(
COMPVARS pc,  
int uiFlags,  
byte[] lpBits, 
ref bool pfKey,  
ref long plSize  
);
[DllImport("MSVFW32.dll",CharSet=CharSet.Ansi)]
public static extern int ICGetInfo(
int hic,            
ICINFO lpicinfo,  
int cb            
);
[DllImport("MSVFW32.dll")]
public static extern bool ICCompressorChoose(
IntPtr hwnd,      
int uiFlags,   
//ref BITMAPINFO pvIn,   
int pvIn,
int lpData,  
COMPVARS pc,   
string lpszTitle 
);
[DllImport("MSVFW32.dll")]
public static extern int ICLocate(
int fccType,              
int fccHandler,           
ref BITMAPINFOHEADER lpbiIn,  
ref BITMAPINFOHEADER lpbiOut,  
short wFlags                 
);
[DllImport("MSVFW32.dll"),PreserveSig]
public static extern int ICOpen(int fccType,int fccHandler,ICMODE wMode); 
[DllImport("MSVFW32.dll")]
public static extern int ICClose(int hic);
[DllImport("MSVFW32.dll")]
public static extern int ICCompress(
int hic,
int dwFlags,        // flags
ref BITMAPINFOHEADER lpbiOutput,     // output format
IntPtr lpData,         // output data
ref BITMAPINFOHEADER  lpbiInput,      // format of frame to compress
IntPtr lpBits,         // frame data to compress
int lpckid,         // ckid for data in AVI file
int lpdwFlags,      // flags in the AVI index.
int lFrameNum,      // frame number of seq.
int dwFrameSize,    // reqested size in bytes. (if non zero)
int dwQuality,      // quality within one frame
// BITMAPINFOHEADER  lpbiPrev,       // format of previous frame
int  lpbiPrev,       // format of previous frame
int lpPrev          // previous frame
);
[DllImport("MSVFW32.dll")]
public static extern int ICDecompress(
int hic,                        
int dwFlags,                  
ref BITMAPINFOHEADER lpbiFormat,  
byte[] lpData,                  
ref BITMAPINFOHEADER lpbi    ,    
byte[] lpBits                   
);
[DllImport("MSVFW32.dll")]
public static extern int ICSendMessage(int hic,int msg,ref BITMAPINFO dw1,ref BITMAPINFO dw2);
[DllImport("MSVFW32.dll")]
public static extern int ICSendMessage(int hic,int msg,int dw1,int dw2);
[DllImport("MSVFW32.dll")]
public static extern int ICSendMessage(int hic,int msg,ICINFO dw1,int dw2);
public static readonly int DRV_USER                =0x4000;
public static readonly int ICM_USER          =(DRV_USER+0x0000);
public static readonly int ICM_COMPRESS_BEGIN          =(ICM_USER+7);    // begin a series of compress calls.
public static readonly int ICM_COMPRESS                =(ICM_USER+8) ;   // compress a frame
public static readonly int ICM_COMPRESS_END            =(ICM_USER+9) ;   // end of a series of compress calls.
public static readonly int ICM_COMPRESS_GET_FORMAT     =(ICM_USER+4);
public static readonly int ICM_DECOMPRESS_BEGIN        =(ICM_USER+12);   // start a series of decompress calls
public static readonly int ICM_DECOMPRESS              =(ICM_USER+13);   // decompress a frame
public static readonly int ICM_DECOMPRESS_END          =(ICM_USER+14);
#endregion
}
public class ICCompressor:ICBase//视频压缩
{
public ICCompressor(COMPVARS cp,BITMAPINFO biIn,int fourcc):base(cp,biIn,ICMODE.ICMODE_COMPRESS,fourcc)
{
}
public override void Open()
{
base.Open ();
int r=ICSendMessage(hic,ICM_COMPRESS_GET_FORMAT,ref this._in,ref this._out);
bool s=ICSeqCompressFrameStart(this.Compvars,ref this._in);
}
public override byte[] Process(byte[] data)
{
if(this.hic==0)return null;
bool key=false;long size=0;
try
{
lock(data)
{ IntPtr r=(IntPtr)ICSeqCompressFrame(this.pp,0,data,ref key,ref size);
byte[] b=new byte[size];
Marshal.Copy(r,b,0,(int)size);
return b;
}
}
catch(System.Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}
return null;
}
public override void Close()
{
if(this.hic!=0)
{
try
{
ICSeqCompressFrameEnd(ref this.pp);
}
catch
{
}
}
base.Close ();
}
}
public class ICDecompressor:ICBase//视频解压缩
{
public ICDecompressor(COMPVARS cp,BITMAPINFO biIn,int fourcc):base(cp,biIn,ICMODE.ICMODE_DECOMPRESS,fourcc)
{
}
public override void Open()
{
base.Open ();
int r=ICSendMessage(hic,ICM_USER+10,ref this._in,ref this._out);//get the output bitmapinfo
r=ICSendMessage(hic,ICM_DECOMPRESS_BEGIN,ref this._in,ref this._out);
}
public override byte[] Process(byte[] data)
{
return data;// if(this.hic==0)return null;
// byte[] b=new byte[this._out.bmiHeader.biSizeImage];;
// try
// {
// int i=ICDecompress(this.hic,0,ref this._in.bmiHeader,data,ref this._out.bmiHeader,b);
// }
// catch
// {
// }
// return b;
}
public override void Close()
{
if(this.hic!=0)
{
ICSendMessage(hic,ICM_USER+14,0,0);//get the output bitmapinfo
}
base.Close ();
}
}
}

解决方案 »

  1.   

    上面这个是压缩和解类这个类     byte[] buf = this.cp.Process(bs);我就是这样调用的 override byte[] Process(byte[] data)  返回来是空的
      

  2.   

    public override byte[] Process(byte[] data)
            {
                if (this.hic == 0) return null;
                bool key = false; long size = 0;
                try
                {
                    lock (data)
                    {                    IntPtr r = (IntPtr)ICSeqCompressFrame(this.pp, 0, data, ref key, ref size);
                        byte[] b = new byte[size];
                        Marshal.Copy(r, b, 0, (int)size);
                        return b;
                    }
                }
                catch (System.Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex.Message);
                }
                return null;
            }问题肯定在这里!
      

  3.   

    if (this.hic == 0) return null;你的hic是不是进来的时候就是0 啊!
    你跟代码了么?
      

  4.   

    public virtual void Open()//(System.IntPtr p)
    {
    this.hic=ICOpen(FOURCC.ICTYPE_VIDEO,this.fourcc,this.mode); 
    this.Compvars.hic=this.hic;
    }这个地方是获取hic,这个要调用系统函数
      

  5.   

    对,逐语句调试下,看下面这个操作是否已经获得对应的地址
    IntPtr r=(IntPtr)ICSeqCompressFrame(this.pp,0,data,ref key,ref size);
      

  6.   

    代码能发给我份看看吗?
    [email protected]
      

  7.   

    public   virtual   void   Open()//(System.IntPtr   p)
    {
    this.hic=ICOpen(FOURCC.ICTYPE_VIDEO,this.fourcc,this.mode);  
    this.Compvars.hic=this.hic;

    这个地方的hic的值老是0,
    把if   (this.hic   ==   0)   return   null; 
    这行给注释掉
    IntPtr   r   =   (IntPtr)ICSeqCompressFrame(this.pp,   0,   data,   ref   key,   ref   size); 其中r的值为0,则返回为空
    是呀,解决没有呀,兄弟们都等信呢!!!都是这个问题!郁闷中!还忘楼主给贴出答案,,谢啦!
      

  8.   

    学习
    能不能给我一份关键代码研究一下啊?
    谢谢
    邮箱:[email protected]
      

  9.   

    问题出在
    public   virtual   void   Open()//(System.IntPtr   p)
    {
    this.hic=ICOpen(FOURCC.ICTYPE_VIDEO,this.fourcc,this.mode);  
    this.Compvars.hic=this.hic;
    } 因为你系统没装有对应的解码器,所以这个icopen一直返回零。
    微软的文档是这样说明这个返回值的:"Returns a handle to a compressor or decompressor if successful or zero otherwise"解决的办法是下载你程序编码所用的解码器,我估计你程序里是用MPEG codec。
    到这个连接去下载解码器:http://www.movavi.com/codec/mp42.html
    希望可以帮助大家
      

  10.   

    我遇到了同样的问题,刚刚解决了,没有安装解码器下载个mpg4c32.dll解码器即可