C#将图像文件压缩为AVI文件播放

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

namespace AVI
{
    /**//// <summary>
    /// AVIWriter 的摘要说明,chenpeng,Email:[email protected]。
    /// </summary>
    public class AVIWriter
    {
        const string AVIFILE32 = "AVIFIL32";
        private int _pfile = 0;
        private IntPtr _ps = new IntPtr(0);
        private IntPtr _psCompressed = new IntPtr(0);
        private UInt32 _frameRate = 0;
        private int _count = 0;
        private UInt32 _width = 0;
        private UInt32 _stride = 0;
        private UInt32 _height = 0;
        //avi标识
        private UInt32 _fccType = 1935960438; // vids
        private UInt32 _fccHandler = 808810089;// IV50
    
        private Bitmap _bmp;

[DllImport(AVIFILE32)]
        private static extern void AVIFileInit();

[DllImport(AVIFILE32)]
        private static extern int AVIFileOpenW(ref int ptr_pfile, [MarshalAs(UnmanagedType.LPWStr)]string fileName, int flags, int dummy);

[DllImport(AVIFILE32)]
        private static extern int AVIFileCreateStream(int ptr_pfile, out IntPtr ptr_ptr_avi, ref AVISTREAMINFOW ptr_streaminfo);
        [DllImport(AVIFILE32)]
        private static extern int AVIMakeCompressedStream(out IntPtr ppsCompressed, IntPtr aviStream, ref AVICOMPRESSOPTIONS ao, int dummy);

[DllImport(AVIFILE32)]
        private static extern int AVIStreamSetFormat(IntPtr aviStream, Int32 lPos, ref BITMAPINFOHEADER lpFormat, Int32 cbFormat);

[DllImport(AVIFILE32)]
        unsafe private static extern int AVISaveOptions(int hwnd, UInt32 flags,int nStreams, IntPtr* ptr_ptr_avi, AVICOMPRESSOPTIONS** ao);

[DllImport(AVIFILE32)]
        private static extern int AVIStreamWrite(IntPtr aviStream, Int32 lStart,Int32 lSamples, IntPtr lpBuffer, Int32 cbBuffer, Int32 dwFlags, Int32 dummy1, Int32 dummy2);

[DllImport(AVIFILE32)]
        private static extern int AVIStreamRelease(IntPtr aviStream);

[DllImport(AVIFILE32)]
        private static extern int AVIFileRelease(int pfile);

[DllImport(AVIFILE32)]
        private static extern void AVIFileExit();

[StructLayout(LayoutKind.Sequential, Pack = 1)]
            private struct AVISTREAMINFOW
        ...{
            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 UInt32 rect_left;
            public UInt32 rect_top;
            public UInt32 rect_right;
            public UInt32 rect_bottom;
            public UInt32 dwEditCount;
            public UInt32 dwFormatChangeCount;
            public UInt16 szName0;
            public UInt16 szName1;
            public UInt16 szName2;
            public UInt16 szName3;
            public UInt16 szName4;
            public UInt16 szName5;
            public UInt16 szName6;
            public UInt16 szName7;
            public UInt16 szName8;
            public UInt16 szName9;
            public UInt16 szName10;
            public UInt16 szName11;
            public UInt16 szName12;
            public UInt16 szName13;
            public UInt16 szName14;
            public UInt16 szName15;
            public UInt16 szName16;
            public UInt16 szName17;
            public UInt16 szName18;
            public UInt16 szName19;
            public UInt16 szName20;
            public UInt16 szName21;
            public UInt16 szName22;
            public UInt16 szName23;
            public UInt16 szName24;
            public UInt16 szName25;
            public UInt16 szName26;
            public UInt16 szName27;
            public UInt16 szName28;
            public UInt16 szName29;
            public UInt16 szName30;
            public UInt16 szName31;
            public UInt16 szName32;
            public UInt16 szName33;
            public UInt16 szName34;
            public UInt16 szName35;
            public UInt16 szName36;
            public UInt16 szName37;
            public UInt16 szName38;
            public UInt16 szName39;
            public UInt16 szName40;
            public UInt16 szName41;
            public UInt16 szName42;
            public UInt16 szName43;
            public UInt16 szName44;
            public UInt16 szName45;
            public UInt16 szName46;
            public UInt16 szName47;
            public UInt16 szName48;
            public UInt16 szName49;
            public UInt16 szName50;
            public UInt16 szName51;
            public UInt16 szName52;
            public UInt16 szName53;
            public UInt16 szName54;
            public UInt16 szName55;
            public UInt16 szName56;
            public UInt16 szName57;
            public UInt16 szName58;
            public UInt16 szName59;
            public UInt16 szName60;
            public UInt16 szName61;
            public UInt16 szName62;
            public UInt16 szName63;
        }

[StructLayout(LayoutKind.Sequential, Pack = 1)]
            private struct AVICOMPRESSOPTIONS
        ...{
            public UInt32 fccType;
            public UInt32 fccHandler;
            public UInt32 dwKeyFrameEvery;
   
            public UInt32 dwQuality;
            public UInt32 dwBytesPerSecond;
       
            public UInt32 dwFlags;
            public IntPtr lpFormat;
            public UInt32 cbFormat;
            public IntPtr lpParms;
            public UInt32 cbParms;
            public UInt32 dwInterleaveEvery;
        }

[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;
        }

public class AviException : ApplicationException
        {
            public AviException(string s) : base(s) ...{ }
            public AviException(string s, Int32 hr)
                : base(s)
            {

if (hr == AVIERR_BADPARAM)
                {
                    err_msg = "AVIERR_BADPARAM";
                }
                else
                {
                    err_msg = "unknown";
                }
            }

public string ErrMsg()
            {
                return err_msg;
            }
            private const Int32 AVIERR_BADPARAM = -2147205018;
            private string err_msg;
        }

public Bitmap Create(string fileName, UInt32 frameRate, int width, int
            height)
        {
            _frameRate = frameRate;
            _width = (UInt32)width;
            _height = (UInt32)height;
            _bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            //锁定为24位位图
            BitmapData bmpDat = _bmp.LockBits(new Rectangle(0, 0, width,
                height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            _stride = (UInt32)bmpDat.Stride;
            _bmp.UnlockBits(bmpDat);
            AVIFileInit();
            int hr = AVIFileOpenW(ref _pfile, fileName, 4097, 0);
            if (hr != 0)
            {
                throw new AviException("Create错误!");
            }

CreateStream();
            SetOptions();

return _bmp;
        }

public void AddFrame()
        {

BitmapData bmpDat = _bmp.LockBits(
                new Rectangle(0, 0, (int)_width, (int)_height),
                ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

int hr = AVIStreamWrite(_psCompressed, _count, 1,
                bmpDat.Scan0,
                (Int32)(_stride * _height),
                0,
                0,
                0);

if (hr != 0)
            {
                throw new AviException("AVIStreamWrite");
            }

_bmp.UnlockBits(bmpDat);

_count++;
        }

public void LoadFrame(Bitmap nextframe)
        {

_bmp = new Bitmap(nextframe);
        }

public void Close()
        {
            AVIStreamRelease(_ps);
            AVIStreamRelease(_psCompressed);

AVIFileRelease(_pfile);
            AVIFileExit();
        }

/**//// <summary>
        /// 创建流文件
        /// </summary>
        private void CreateStream()
        {
            AVISTREAMINFOW strhdr = new AVISTREAMINFOW();
            strhdr.fccType = _fccType;
            strhdr.fccHandler = _fccHandler;
            strhdr.dwFlags = 0;
            strhdr.dwCaps = 0;
            strhdr.wPriority = 0;
            strhdr.wLanguage = 0;
            strhdr.dwScale = 1;
            strhdr.dwRate = _frameRate;
            strhdr.dwStart = 0;
            strhdr.dwLength = 0;
            strhdr.dwInitialFrames = 0;
            strhdr.dwSuggestedBufferSize = _height * _stride;
            strhdr.dwQuality = 0xffffffff;
            strhdr.dwSampleSize = 0;
            strhdr.rect_top = 0;
            strhdr.rect_left = 0;
            strhdr.rect_bottom = _height;
            strhdr.rect_right = _width;
            strhdr.dwEditCount = 0;
            strhdr.dwFormatChangeCount = 0;
            strhdr.szName0 = 0;
            strhdr.szName1 = 0;

int hr = AVIFileCreateStream(_pfile, out _ps, ref strhdr);

if (hr != 0)
            {
                throw new AviException("AVIFileCreateStream");
            }
        }

/**//// <summary>
        /// 设置参数
        /// </summary>
        unsafe private void SetOptions()
        {
            AVICOMPRESSOPTIONS opts = new AVICOMPRESSOPTIONS();
            opts.fccType = _fccType;
            opts.fccHandler = 0;
            opts.dwKeyFrameEvery = 0;
            opts.dwQuality = 0;
            opts.dwFlags = 0;
            opts.dwBytesPerSecond = 0;
            opts.lpFormat = new IntPtr(0);
            opts.cbFormat = 0;
            opts.lpParms = new IntPtr(0);
            opts.cbParms = 0;
            opts.dwInterleaveEvery = 0;

AVICOMPRESSOPTIONS* p = &opts;
            AVICOMPRESSOPTIONS** pp = &p;

IntPtr x = _ps;
            IntPtr* ptr_ps = &x;

AVISaveOptions(0, 0, 1, ptr_ps, pp);

int hr = AVIMakeCompressedStream(out _psCompressed, _ps, ref
                opts, 0);
            if (hr != 0)
            ...{
                throw new AviException("AVIMakeCompressedStream");
            }

BITMAPINFOHEADER bi = new BITMAPINFOHEADER();
            bi.biSize = 40;
            bi.biWidth = (Int32)_width;
            bi.biHeight = (Int32)_height;
            bi.biPlanes = 1;
            bi.biBitCount = 24;
            bi.biCompression = 0;
            bi.biSizeImage = _stride * _height;
            bi.biXPelsPerMeter = 0;
            bi.biYPelsPerMeter = 0;
            bi.biClrUsed = 0;
            bi.biClrImportant = 0;

hr = AVIStreamSetFormat(_psCompressed, 0, ref bi, 40);
            if (hr != 0)
            {
                throw new AviException("AVIStreamSetFormat", hr);
            }
        }
    }
}

---------------------------------------------------------------------------------

private void button1_Click(object sender, EventArgs e)

{

//FileUtil file = new FileUtil();

AviFile_Library.AVIWriter aviWriter = new AviFile_Library.AVIWriter();

Bitmap avi_frame = aviWriter.Create(@"d:\mytest.avi",1, 700, 700);

/*

//获得指定目录下文件列表的list

IList list=file.ListFile(@"D:\pic\");

foreach(string s in list)

{

//获得图像

Bitmap cache=new Bitmap(@"D:\pic\"+s);

//由于转化为avi后呈现相反,所以翻转

cache.RotateFlip(RotateFlipType.Rotate180FlipX);

//载入图像

aviWriter.LoadFrame(cache);

aviWriter.AddFrame();

}

*/

//获得图像

Bitmap cache = new Bitmap(@"D:\pic\0502344516.jpg");

//由于转化为avi后呈现相反,所以翻转

cache.RotateFlip(RotateFlipType.Rotate180FlipX);

//载入图像

aviWriter.LoadFrame(cache);

aviWriter.AddFrame();

//释放资源

aviWriter.Close();

avi_frame.Dispose();

}

}

-------------------------------------------------------------

c#生成AVI自动设置压缩格式,不调用AVISaveOptions

最开始的代码:

AVICOMPRESSOPTIONS opts = new AVICOMPRESSOPTIONS();

opts.fccType = _fccType;

opts.fccHandler = 0;

opts.dwKeyFrameEvery = 0;

opts.dwQuality = 0;

opts.dwFlags = 0;

opts.dwBytesPerSecond = 0;

opts.lpFormat = new IntPtr(0);

opts.cbFormat = 0;

opts.lpParms = new IntPtr(0);

opts.cbParms = 0;

opts.dwInterleaveEvery = 0;

AVICOMPRESSOPTIONS* p = &opts;

AVICOMPRESSOPTIONS** pp = &p;

IntPtr x = _ps;

IntPtr* ptr_ps = &x;

AVISaveOptions(0, 0, 1, ptr_ps, pp);

int hr = AVIMakeCompressedStream(out _psCompressed, _ps, ref opts, 0);

修改后的代码

opts.fccType = 0;

           opts.fccHandler = (int)0x6376736d;

           opts.dwKeyFrameEvery = 0;

           opts.dwQuality = 10000;

           opts.dwFlags = 8;

           opts.dwBytesPerSecond = 0;

           opts.lpFormat = new IntPtr(0);

           opts.cbFormat = 0;

           opts.lpParms = new IntPtr(0);

           opts.cbParms = 4;

           opts.dwInterleaveEvery = 0;

           AVICOMPRESSOPTIONS* p = &opts;

           AVICOMPRESSOPTIONS** pp = &p;

           IntPtr x = _ps;

           IntPtr* ptr_ps = &x;

           opts.lpParms = Marshal.AllocHGlobal(sizeof(int));

           //AVISaveOptions(0, 0, 1, ptr_ps, pp);

           int hr = AVIMakeCompressedStream(out _psCompressed, _ps, ref opts, 0);  

思路就是,Debug模式下,跟踪设置完压缩格式后

AVICOMPRESSOPTIONS结构体中各元素的值,其中lpParms需要利用

Marshal.AllocHGlobal(sizeof(int))来获取,其余根据监视的值设定即可

         

时间: 2024-11-10 14:27:51

C#将图像文件压缩为AVI文件播放的相关文章

vc++实现avi文件的操作

为了对avi进行读写,微软提供了一套API,总共50个函数,他们的用途主要有两类,一个是avi文件的操作,一类是数据流streams的操作. 1.打开和关闭文件 AVIFileOpen ,AVIFileAddRef, AVIFileRelease 2.从文件中读取文件信息 通过AVIFileInfo可以获取avi文件的一些信息,这个函数返回一个AVIFILEINFO结构,通过AVIFileReadData可以用来获取AVIFileInfo函数得不到的信息.这些信息也许不包含在文件的头部,比如拥有

【C#】AviFile使用(播放AVI文件,兼容性比较差)

最近在做一个视频识别项目,需要用到视频处理,在codeproject上找到了一个关于对Avi的操作库,感觉不错,在这里把一些要点记录下来 http://www.codeproject.com/Articles/7388/A-Simple-C-Wrapper-for-the-AviFile-Library Avi视频文件的编码有很多,这个库只支持部分Avi文件,有些Avi文件不支持,具体哪些不支持还没搞清楚 AviFile库提供了 1.从视频流中图片的处理 2.视频中音频的处理 3.压缩和解压视频

C#转换AVI文件为BMP文件

AVI英文全称为Audio Video Interleaved,即音频视频交错格式.它是一种将语音和影像同步组合在一起的文件格式.AVI支持256色和RLE压缩,主要应用在多媒体光盘上,主要用来保存电视.电影等各种影像信息. 在Windows系统中,借助于API之利,我们能够轻易的实现AVI文件的分解与重组.下面,我给出一个C#版本的AVI分解示例. using System; using System.Drawing; using System.Runtime.InteropServices;

C#压缩、解压缩文件(夹)(rar、zip)

主要是使用Rar.exe压缩解压文件(夹)(*.rar),另外还有使用SevenZipSharp.dll.zLib1.dll.7z.dll压缩解压文件(夹)(*.zip).需要注意的几点如下: 1.注意Rar.exe软件存放的位置,此次放在了Debug目录下 2.SevenZipSharp.dll.zLib1.dll.7z.dll必须同时存在,否则常报“加载7z.dll错误”,项目引用时,只引用SevenZipSharp.dll即可 3.另外找不到7z.dll文件也会报错,测试时发现只用@"..

(转)AVI文件格式解析+AVI文件解析工具

AVI文件解析工具下载地址:http://download.csdn.net/detail/zjq634359531/7556659 AVI(Audio Video Interleaved的缩写)是一种RIFF(Resource Interchange File Format的缩写)文件格式,多用于音视频捕捉.编辑.回放等应用程序中.通常情况下,一个AVI文件可以包含多个不同类型的媒体流(典型的情况下有一个音频流和一个视频流),不过含有单一音频流或单一视频流的AVI文件也是合法的.AVI可以算是

linux学习笔记——打包、压缩、远程文件传输

############打包.压缩########################## 1.打包 (打包表示把一堆文件变成一个)tar            ##打包工具        -f        ##指定生成包的名字        -c        ##创建包        -v        ##显示创建过程        -t        ##查看包中内容        -x        ##解包        -r        ##追加文件到包中        -C  

xcode UIImageView创建、图片加载、 音频文件播放、 延迟调用

代码创建 /** 创建UIImageView */ UIImageView * imageView=[[UIImageView alloc]init]; /** 设置尺寸位置 */ imageView.frame=(CGRect){{50,50},{230,230}}; /** 创建图片 */ UIImage * image=[[UIImage alloc]init]; /** 获取图片 */ image=[UIImage imageNamed:@"图片名称"]; /** 把图片给容器

HTML5 Audio时代的MIDI音乐文件播放

大家都知道,HTML5 Audio标签能够支持wav, webm, mp3, ogg, acc等格式,但是有个很重要的音乐文件格式midi(扩展名mid)却在各大浏览器中都没有内置的支持,因为mid文件并不像其他声音文件那样记录声音的采样信息,而是记录了乐器的演奏指令,二者的原理截然不同. mid文件格式的最突出的有点是文件极小,几分钟的音乐可能只 大家都知道,HTML5 Audio标签能够支持wav, webm, mp3, ogg, acc等格式,但是有个很重要的音乐文件格式midi(扩展名m

Linux tar(用来压缩和解压文件)

通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在任何设备上创建档案.利用tar命令,可以把一大堆的文件和目录全部打包成一个文件,这对于备份文件或将几个文件组合成为一个文件以便