icm.cs

来自「实现微软OFFICEACCESE数据库自动备份功能」· CS 代码 · 共 501 行

CS
501
字号
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;

namespace gowk.multimedia
{
	/// <summary>
	/// Compress 的摘要说明。
	/// </summary>
	public class ICBase
	{
		protected int hic;
		protected ICMODE mode;
		protected BITMAPINFO _in;
		protected BITMAPINFO _out;
		protected COMPVARS pp;
        protected COMPVARS ppt;
		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)
		{
            
			
                Debug.Print("=======================================================================");
                Debug.Print("DIVX   "+FOURCC.DIVX.ToString());
                Debug.Print("MP42   " + FOURCC.MP42.ToString());
                Debug.Print("ICTYPE_VIDEO       " + FOURCC.ICTYPE_VIDEO.ToString());
                Debug.Print("ICM_FRAMERATE    " + FOURCC.ICM_FRAMERATE.ToString());
                Debug.Print("ICM_KEYFRAMERATE    " + FOURCC.ICM_KEYFRAMERATE.ToString());
                Debug.Print("streamtypeMIDI    " + FOURCC.streamtypeMIDI.ToString());
                Debug.Print("streamtypeVIDEO    " + FOURCC.streamtypeVIDEO.ToString());
                Debug.Print("comptypeDIB    " + FOURCC.comptypeDIB.ToString());
                Debug.Print("FOURCC_WVPL    " + FOURCC.FOURCC_WVPL.ToString());
                Debug.Print("FOURCC_wave    " + FOURCC.FOURCC_wave.ToString());
                Debug.Print("formtypeAVI    " + FOURCC.formtypeAVI.ToString());
                Debug.Print("listtypeAVIHEADER    " + FOURCC.listtypeAVIHEADER.ToString());
                Debug.Print("WinbondJPEG    " + FOURCC.WinbondJPEG.ToString());
                Debug.Print("HuffYUV    " + FOURCC.HuffYUV.ToString());
                Debug.Print("MJPG    " + FOURCC.MJPG.ToString());
                Debug.Print("MicrosoftVideo1    " + FOURCC.MicrosoftVideo1.ToString());
                Debug.Print("MicrosoftRLE    " + FOURCC.MicrosoftRLE.ToString());
                Debug.Print("On2VP31    " + FOURCC.On2VP31.ToString());
                Debug.Print("XVID    " + FOURCC.XVID.ToString());
                Debug.Print("X_264   " + FOURCC.X_264.ToString());
                Debug.Print("WMP9   " + FOURCC.WMP9.ToString());
                Debug.Print("MicrosoftH263   " + FOURCC.MicrosoftH263.ToString());
                Debug.Print("IntelH263   " + FOURCC.IntelH263.ToString());

                Debug.Print("======================================================================="); 
            /*
	 		pp=new COMPVARS();
			pp.cbSize=Marshal.SizeOf(pp);
			pp.dwFlags=0;
			pp.fccHandler=FOURCC.DIVX ;
			pp.fccType=FOURCC.ICTYPE_VIDEO  ;
			pp.hic=0;
			pp.lDataRate=0;
			pp.lKey=0;
			pp.lQ=-1;
            
			try
			{
                



               bool b=ICCompressorChoose((IntPtr)0,0,0,0,pp,"SSSSSS");
			}
			catch(System.Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
             
			ICINFO info=new ICINFO();
			try
			{
				info.dwSize=Marshal.SizeOf(info);
			}
			catch(System.Exception eee)
			{
				MessageBox.Show(eee.Message);
			}
		
			try
			{
					
                int rrrrrrrrr=ICGetInfo(pp.hic,info,Marshal.SizeOf(info));
			}
			catch(System.Exception exx)
			{
				MessageBox.Show(exx.Message);
			} 
            */
			//	int sssssf=ICM.ICSendMessage(hic,ICM.ICM_COMPRESS_BEGIN,ref this._in,ref this._out);
		//	int rrr=ICM.ICSendMessage(hic,ICM.ICM_COMPRESS_GET_FORMAT,ref this._in,ref this._out);
		//	bool isstart=ICSeqCompressFrameStart(pp,ref this._in);
			
			//	int rrr=	ICM.ICSendMessage(hic,0x4000+0x1000+2,info,Marshal.SizeOf(info));
			//	this.hic=ICLocate(FOURCC.ICTYPE_VIDEO,this.fourcc  ,ref _in.bmiHeader,ref _out.bmiHeader,this.mode);
                this.hic = ICOpen(FOURCC.ICTYPE_VIDEO  , this.fourcc , this.mode);
                Debug.Print("now using " + this.fourcc.ToString());
                this.Compvars.hic = this.hic;

			//	ICM.ICSendMessage(this.hic,ICM.DRV_USER+0x1000+10,(int)p,0);
			//	int result=ICSendMessage(hic,ICM_COMPRESS_BEGIN,ref _in,ref _out);
			//	bool sssst=	ICM.ICSeqCompressFrameStart(pp,ref this._in);
		}
		public virtual byte[] Process(byte[] data)
		{
		/*	bool key=false;long size=0;
			try
			{
				IntPtr r=(IntPtr)ICSeqCompressFrame(this.pp,0,data,ref key,ref size);
				System.Diagnostics.Trace.WriteLine(string.Format("isKeyFrame {0},size :{1}",key.ToString(),size.ToString()));
				byte[] b=new byte[size];
				Marshal.Copy(r,b,0,(int)size);
				return b;
				//	int r=ICCompress(hic,0,ref this._out.bmiHeader,pdata,ref this._in.bmiHeader,data,0,0,0,0,0,0,0);
			}
			catch(System.Exception ex)
			{
				gowk.utility.Diagnostics.Debug.Write(ex);
				System.Diagnostics.Trace.WriteLine(ex.Message);
			}*/
			return null;
		}

		public virtual void Close()
		{
			ICClose(hic);
		}
		
		#region API引用
		
		[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,  
			ICMODE wFlags                 
			);
        [DllImport("MSVFW32.dll")]
        public static extern void ICCompressorFree(
            ref COMPVARS pc
            );
		[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
	{
        bool nocodec;
        public bool HASIntelCodec
        {
            get
            {
                return nocodec;
            }
        }

		public ICCompressor(COMPVARS cp,BITMAPINFO biIn,int fourcc):base(cp,biIn,ICMODE.ICMODE_COMPRESS,fourcc)
		{
            nocodec = false;
		}

		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);
            //int y = 0;
		}
		public override byte[] Process(byte[] data)
		{
            if (this.hic == 0)
            {
                Debug.Print ("请先安装解码器:Microsoft H.263 Vedio Codec。");
                nocodec = true;
                return null;
            }
			bool key=false;long size=0;
			try
			{
				lock(data)
				{
					IntPtr r=(IntPtr)ICSeqCompressFrame(this.pp,0,data,ref key,ref size);
                    if (r == (IntPtr)0)
                    {
                        Debug.Print("压缩失败!");
                       return null;
                    }
                    Debug.Print("data  " + data.Length);
                    Debug.Print("size   " + size);
					byte[] b=new byte[size];
					Marshal.Copy(r,b,0,(int)size);

                    Debug.Print("压缩成功!!!!!!!!!!");
                    
					return b;
                    //return null;
                    
				}
				//	int r=ICCompress(hic,0,ref this._out.bmiHeader,pdata,ref this._in.bmiHeader,data,0,0,0,0,0,0,0);
			}
			catch(System.Exception ex)
			{
				 
				System.Diagnostics.Trace.WriteLine("compress error " +ex.Message);
			}
			return null;
		}
		public override void Close()
		{
			if(this.hic!=0)
			{
				try
				{
					ICSeqCompressFrameEnd(ref this.pp);
                    ICCompressorFree(ref this.pp);
				}
				catch(System.Exception ex)
				{
					gowk.utility.Diagnostics.Debug.Write(ex);
				}
			}
			base.Close ();
		}
	}






	public class ICDecompressor:ICBase
	{
        bool nocodec;
        public bool HASIntelCodec
        {
            get
            {
                return nocodec;
            }
        }

		public ICDecompressor(COMPVARS cp,BITMAPINFO biIn,int fourcc):base(cp,biIn,ICMODE.ICMODE_DECOMPRESS,fourcc)
		{
            nocodec = false;
		}
		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)
		{
            if (this.hic == 0)
            {
                nocodec = true;
                return null;
            }
			byte[] b=new byte[this._out.bmiHeader.biSizeImage];;
			try
			{
                lock (data)
                {
                    int i = ICDecompress(this.hic, 0, ref this._in.bmiHeader, data, ref this._out.bmiHeader, b);
                    Debug.Print("decompress return  " + i.ToString());
                    if (i < 0)
                    {
                        Debug.Print("解压缩发生错误!");
                        return null;
                    }

                }

                
			}
			catch(System.Exception ex)
			{
				//MessageBox.Show(ex.Message);
                Debug.Print(ex.Message);
                return null;
			}
			return b;
		}
		public override void Close()
		{
			if(this.hic!=0)
			{
				ICSendMessage(hic,ICM_USER+14,0,0);//get the output bitmapinfo
			}
			base.Close ();
		}
	}
	#region
    /*
	public class ICDraw:ICBase
	{
		public Control Control;
		public ICDraw(COMPVARS cp,BITMAPINFO biIn,int fourcc):base(cp,biIn,ICMODE.ICMODE_DRAW,fourcc)
		{
		}
		public override void Open()
		{
			base.Open ();
			System.Drawing.Graphics g=this.Control.CreateGraphics();
			IntPtr hdc=g.GetHdc();
			int rrr=ICSendMessage(hic,ICM_USER+10,ref this._in,ref this._out);
			int ret=ICDrawBegin(
				this.HIC,
				ICDRAWFlag.ICDRAW_HDC|ICDRAWFlag.ICDRAW_CONTINUE,
				Graphics.GetHalftonePalette(),
				this.Control.Handle,
				hdc,
				0,0,160,120,
				ref this._in.bmiHeader,
				0,0,160,120,
				120,
				8
			//	(int)(this.Compvars.lDataRate/this.Compvars.lKey)
				);
		}
		public override byte[] Process(byte[] data)
		{
			byte[] b=new byte[76032];;
			IntPtr p=IntPtr.Zero;
			try
			{
				int i=ICDecompress(this.hic,0,ref this._in.bmiHeader,data,ref this._out.bmiHeader,b);
				System.Diagnostics.Trace.WriteLine(p.ToString());
			}
			catch(System.Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return b;
		}
		protected override void Close()
		{
			base.Close ();
		}
		[DllImport("MSVFW32.dll")]
		static extern int ICDrawBegin(
			int hic,
			ICDRAWFlag dwFlags,        // flags
			IntPtr hpal,           // palette to draw with
			IntPtr    hwnd,           // window to draw to
			IntPtr  hdc,            // HDC to draw to
			int   xDst,           // destination rectangle
			int yDst,
			int dxDst,
			int dyDst,
			ref BITMAPINFOHEADER  lpbi,           // format of frame to draw
			int  xSrc,           // source rectangle
			int  ySrc,
			int  dxSrc,
			int  dySrc,
			int   dwRate,         // frames/second = (dwRate/dwScale)
			int dwScale
			);

	}*/
    #endregion
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?