`
wangxiaohigh
  • 浏览: 1419244 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多
继续上一篇blog

usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;

namespaceorg.loon.util
...{
/**////<summary>
///AVIWriter的摘要说明,chenpeng,Email:ceponline@yahoo.com.cn。
///</summary>

publicclassAVIWriter
...{
conststringAVIFILE32="AVIFIL32";
privateint_pfile=0;
privateIntPtr_ps=newIntPtr(0);
privateIntPtr_psCompressed=newIntPtr(0);
privateUInt32_frameRate=0;
privateint_count=0;
privateUInt32_width=0;
privateUInt32_stride=0;
privateUInt32_height=0;
//avi标识
privateUInt32_fccType=1935960438;//vids
privateUInt32_fccHandler=808810089;//IV50

privateBitmap_bmp;

[DllImport(AVIFILE32)]
privatestaticexternvoidAVIFileInit();

[DllImport(AVIFILE32)]
privatestaticexternintAVIFileOpenW(refintptr_pfile,[MarshalAs(UnmanagedType.LPWStr)]stringfileName,intflags,intdummy);

[DllImport(AVIFILE32)]
privatestaticexternintAVIFileCreateStream(intptr_pfile,outIntPtrptr_ptr_avi,refAVISTREAMINFOWptr_streaminfo);
[DllImport(AVIFILE32)]
privatestaticexternintAVIMakeCompressedStream(outIntPtrppsCompressed,IntPtraviStream,refAVICOMPRESSOPTIONSao,intdummy);

[DllImport(AVIFILE32)]
privatestaticexternintAVIStreamSetFormat(IntPtraviStream,Int32lPos,refBITMAPINFOHEADERlpFormat,Int32cbFormat);

[DllImport(AVIFILE32)]
unsafeprivatestaticexternintAVISaveOptions(inthwnd,UInt32flags,intnStreams,IntPtr*ptr_ptr_avi,AVICOMPRESSOPTIONS**ao);

[DllImport(AVIFILE32)]
privatestaticexternintAVIStreamWrite(IntPtraviStream,Int32lStart,Int32lSamples,IntPtrlpBuffer,Int32cbBuffer,Int32dwFlags,Int32dummy1,Int32dummy2);

[DllImport(AVIFILE32)]
privatestaticexternintAVIStreamRelease(IntPtraviStream);

[DllImport(AVIFILE32)]
privatestaticexternintAVIFileRelease(intpfile);

[DllImport(AVIFILE32)]
privatestaticexternvoidAVIFileExit();

[StructLayout(LayoutKind.Sequential,Pack
=1)]
privatestructAVISTREAMINFOW
...{
publicUInt32fccType;
publicUInt32fccHandler;
publicUInt32dwFlags;
publicUInt32dwCaps;
publicUInt16wPriority;
publicUInt16wLanguage;
publicUInt32dwScale;
publicUInt32dwRate;
publicUInt32dwStart;
publicUInt32dwLength;
publicUInt32dwInitialFrames;
publicUInt32dwSuggestedBufferSize;
publicUInt32dwQuality;
publicUInt32dwSampleSize;
publicUInt32rect_left;
publicUInt32rect_top;
publicUInt32rect_right;
publicUInt32rect_bottom;
publicUInt32dwEditCount;
publicUInt32dwFormatChangeCount;
publicUInt16szName0;
publicUInt16szName1;
publicUInt16szName2;
publicUInt16szName3;
publicUInt16szName4;
publicUInt16szName5;
publicUInt16szName6;
publicUInt16szName7;
publicUInt16szName8;
publicUInt16szName9;
publicUInt16szName10;
publicUInt16szName11;
publicUInt16szName12;
publicUInt16szName13;
publicUInt16szName14;
publicUInt16szName15;
publicUInt16szName16;
publicUInt16szName17;
publicUInt16szName18;
publicUInt16szName19;
publicUInt16szName20;
publicUInt16szName21;
publicUInt16szName22;
publicUInt16szName23;
publicUInt16szName24;
publicUInt16szName25;
publicUInt16szName26;
publicUInt16szName27;
publicUInt16szName28;
publicUInt16szName29;
publicUInt16szName30;
publicUInt16szName31;
publicUInt16szName32;
publicUInt16szName33;
publicUInt16szName34;
publicUInt16szName35;
publicUInt16szName36;
publicUInt16szName37;
publicUInt16szName38;
publicUInt16szName39;
publicUInt16szName40;
publicUInt16szName41;
publicUInt16szName42;
publicUInt16szName43;
publicUInt16szName44;
publicUInt16szName45;
publicUInt16szName46;
publicUInt16szName47;
publicUInt16szName48;
publicUInt16szName49;
publicUInt16szName50;
publicUInt16szName51;
publicUInt16szName52;
publicUInt16szName53;
publicUInt16szName54;
publicUInt16szName55;
publicUInt16szName56;
publicUInt16szName57;
publicUInt16szName58;
publicUInt16szName59;
publicUInt16szName60;
publicUInt16szName61;
publicUInt16szName62;
publicUInt16szName63;
}


[StructLayout(LayoutKind.Sequential,Pack
=1)]
privatestructAVICOMPRESSOPTIONS
...{
publicUInt32fccType;
publicUInt32fccHandler;
publicUInt32dwKeyFrameEvery;

publicUInt32dwQuality;
publicUInt32dwBytesPerSecond;

publicUInt32dwFlags;
publicIntPtrlpFormat;
publicUInt32cbFormat;
publicIntPtrlpParms;
publicUInt32cbParms;
publicUInt32dwInterleaveEvery;
}


[StructLayout(LayoutKind.Sequential,Pack
=1)]
publicstructBITMAPINFOHEADER
...{
publicUInt32biSize;
publicInt32biWidth;
publicInt32biHeight;
publicInt16biPlanes;
publicInt16biBitCount;
publicUInt32biCompression;
publicUInt32biSizeImage;
publicInt32biXPelsPerMeter;
publicInt32biYPelsPerMeter;
publicUInt32biClrUsed;
publicUInt32biClrImportant;
}


publicclassAviException:ApplicationException
...{
publicAviException(strings):base(s)...{}
publicAviException(strings,Int32hr)
:
base(s)
...{

if(hr==AVIERR_BADPARAM)
...{
err_msg
="AVIERR_BADPARAM";
}

else
...{
err_msg
="unknown";
}

}


publicstringErrMsg()
...{
returnerr_msg;
}

privateconstInt32AVIERR_BADPARAM=-2147205018;
privatestringerr_msg;
}


publicBitmapCreate(stringfileName,UInt32frameRate,intwidth,int
height)
...{
_frameRate
=frameRate;
_width
=(UInt32)width;
_height
=(UInt32)height;
_bmp
=newBitmap(width,height,PixelFormat.Format24bppRgb);
//锁定为24位位图
BitmapDatabmpDat=_bmp.LockBits(newRectangle(0,0,width,
height),ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);
_stride
=(UInt32)bmpDat.Stride;
_bmp.UnlockBits(bmpDat);
AVIFileInit();
inthr=AVIFileOpenW(ref_pfile,fileName,4097,0);
if(hr!=0)
...{
thrownewAviException("Create错误!");
}


CreateStream();
SetOptions();

return_bmp;
}


publicvoidAddFrame()
...{

BitmapDatabmpDat
=_bmp.LockBits(
newRectangle(0,0,(int)_width,(int)_height),
ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);

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

if(hr!=0)
...{
thrownewAviException("AVIStreamWrite");
}


_bmp.UnlockBits(bmpDat);

_count
++;
}


publicvoidLoadFrame(Bitmapnextframe)
...{

_bmp
=newBitmap(nextframe);
}


publicvoidClose()
...{
AVIStreamRelease(_ps);
AVIStreamRelease(_psCompressed);

AVIFileRelease(_pfile);
AVIFileExit();
}


/**////<summary>
///创建流文件
///</summary>

privatevoidCreateStream()
...{
AVISTREAMINFOWstrhdr
=newAVISTREAMINFOW();
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;

inthr=AVIFileCreateStream(_pfile,out_ps,refstrhdr);

if(hr!=0)
...{
thrownewAviException("AVIFileCreateStream");
}

}


/**////<summary>
///设置参数
///</summary>

unsafeprivatevoidSetOptions()
...{
AVICOMPRESSOPTIONSopts
=newAVICOMPRESSOPTIONS();
opts.fccType
=_fccType;
opts.fccHandler
=0;
opts.dwKeyFrameEvery
=0;
opts.dwQuality
=0;
opts.dwFlags
=0;
opts.dwBytesPerSecond
=0;
opts.lpFormat
=newIntPtr(0);
opts.cbFormat
=0;
opts.lpParms
=newIntPtr(0);
opts.cbParms
=0;
opts.dwInterleaveEvery
=0;

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

IntPtrx
=_ps;
IntPtr
*ptr_ps=&x;

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

inthr=AVIMakeCompressedStream(out_psCompressed,_ps,ref
opts,
0);
if(hr!=0)
...{
thrownewAviException("AVIMakeCompressedStream");
}


BITMAPINFOHEADERbi
=newBITMAPINFOHEADER();
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,refbi,40);
if(hr!=0)
...{
thrownewAviException("AVIStreamSetFormat",hr);
}

}




}
;

}


运行:

org.loon.util.FileUtilfile=neworg.loon.util.FileUtil();
org.loon.util.AVIWriteraviWriter
=neworg.loon.util.AVIWriter();
//ps:avi中所有图像皆不能小于width及height
Bitmapavi_frame=aviWriter.Create("d:/mytest.avi",1,700,700);
//获得指定目录下文件列表的list
IListlist=file.ListFile("D:/罗德岛战记/罗德岛战记1/");
foreach(stringsinlist)
...{
//获得图像
Bitmapcache=newBitmap("D:/罗德岛战记/罗德岛战记1/"+s);
//由于转化为avi后呈现相反,所以翻转
cache.RotateFlip(RotateFlipType.Rotate180FlipX);
//载入图像
aviWriter.LoadFrame(cache);
aviWriter.AddFrame();
}

//释放资源
aviWriter.Close();
avi_frame.Dispose();


播放画面如下:


生成的文件属性如下,与我们设定的参数相符:






是 不是很简单?实际上,借助于avifile32 api,我们甚至可以改变文件压缩格式,加入背景音乐,乃至植入病毒(咳,通常我们结合桌面截图及avi生成还有模拟键盘,就可以远程操作或者监控、记录 电脑操作……)总之,在windows下操作avi文件是一件再简单没有的事情了。

有兴趣的话可以这两篇博文为基础,将其修改为自己的字幕组工具、屏幕录像工具、乃至远程监控工具等。有疑问的话,可以发email到:ceponline@yahoo.com.cn
分享到:
评论
1 楼 penkee 2014-05-15  
怎样不弹出 视频压缩 选择压缩程序的窗口
求教

相关推荐

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的使用、面向对象...压缩文件、C#与Word互操作、高效应用Excel、基本图形绘制、图像处理技术、常用图表应用、动画处理技术、音频与视频控制...

    摄像头 截图 录制 压缩视频 图片生成视频

    4、将某一目录下的图片生成一段视频。 所有的函数都在 VedioCapture.dll 中,使用delphi编写。 视频压缩使用FXMPEG4,这是微软的一个免费的mp4视频压缩编码器。 录制视频前必须安装这个插件。mpeg4fix.inf上点击...

    C#开发典型模块大全

    19.2.3 向AVI文件中添加视频 489 19.2.4 分解AVI文件 490 19.2.5 设置快捷键 491 19.3 设计过程 494 19.3.1 主窗体设计 494 19.3.2 抓图功能 494 19.3.3 选项设置 496 19.3.4 屏幕录像 497 19.3.5 ...

    C#开发典型模块大全(光盘)第二部分

    明日科技出版的c#开发典型模块大全配套光盘,要的可以下载,因大小受限,分开上传! 第1章 认识C#及开发环境 1.1 C#概述 1.1.1 C#发展历程 1.1.2 C#语言编程环境 1.2.NETFramework2.0简介 1.2.1 什么是....

    C#编程经验技巧宝典

    70 &lt;br&gt;0096 如何将数字转换为字符串 70 &lt;br&gt;0097 如何将字符串转换为数字 70 &lt;br&gt;0098 如何将数字转换为日期格式 71 &lt;br&gt;0099 如何将数字转换为货币格式 71 &lt;br&gt;0100 将字母全部转换大写 ...

    C#开发典型模块大全(光盘)

    19.2.3 向AVI文件中添加视频 19.2.4 分解AVI文件 19.2.5 设置快捷键 19.3 设计过程 19.3.1 主窗体设计 19.3.2 抓图功能 19.3.3 选项设置 19.3.4 屏幕录像 19.3.5 视频分割 第20章 EXE文件加密器 20.1 设计思路 ...

    C#开发典型模块大全(光盘)第一部分

    明日科技出版的c#开发典型模块大全配套光盘,要的可以下载,因大小受限,分开上传! 第1章 认识C#及开发环境 1.1 C#概述 1.1.1 C#发展历程 1.1.2 C#语言编程环境 1.2.NETFramework2.0简介 1.2.1 什么是....

    摄像头 截图 录制 压缩视频 图片生成视频 _1.1

    4、将某一目录下的图片生成一段视频。 所有的函数都在 VedioCapture.dll 中,使用delphi编写。 视频压缩使用FXMPEG4,这是微软的一个免费的mp4视频压缩编码器。录制视频前必须安装这个插件。mpeg4fix.inf上点击右键...

    C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

    2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示 3.可在预览点开之后进行拍照,也可边录制视频边拍照 4.添加了视频和...

    Universal Viewer Pro 6.4.5.1

    该软件集成于已经安装到你的计算机上的大多数现有的浏览器,并且允许你为任何未知的文件类型使用一个十六进制,文本或者二进制格式进行浏览。通常情况下,该软件可以打开被 Internet Explorer 和 MS Office(如果...

    Nufsoft Nature Illusion Studio v2.31

    并提供九种预置的方案,只需看下其提供的范例就能上手,操作简单,发挥您天马行空的想象,即可将处理过的图象存为屏保,可选择程序,压缩为AVI 文件,和生成动画GIF文件,并能为它加上声音效果,给你静止的图片添加...

    多样化生成视频

    4、将某一目录下的图片生成一段视频。 所有的函数都在 VedioCapture.dll 中,使用delphi编写。 视频压缩使用FXMPEG4,这是微软的一个免费的mp4视频压缩编码器。录制视频前必须安装这个插件。mpeg4fix.inf上点击右键...

    vc++ 应用源码包_1

    内含(重启、图片操作、ip操作、键盘与鼠标、客户端以及服务端、文件传输等实例源码) 多个VC++加密解密算法库(CRYPT++) 详细讲解了Crypt++的加密解密的使用以及其它的加密解密方法(例如base64加解密、哈希加...

    vc++ 应用源码包_2

    内含(重启、图片操作、ip操作、键盘与鼠标、客户端以及服务端、文件传输等实例源码) 多个VC++加密解密算法库(CRYPT++) 详细讲解了Crypt++的加密解密的使用以及其它的加密解密方法(例如base64加解密、哈希加...

    vc++ 应用源码包_6

    内含(重启、图片操作、ip操作、键盘与鼠标、客户端以及服务端、文件传输等实例源码) 多个VC++加密解密算法库(CRYPT++) 详细讲解了Crypt++的加密解密的使用以及其它的加密解密方法(例如base64加解密、哈希加...

    vc++ 应用源码包_5

    内含(重启、图片操作、ip操作、键盘与鼠标、客户端以及服务端、文件传输等实例源码) 多个VC++加密解密算法库(CRYPT++) 详细讲解了Crypt++的加密解密的使用以及其它的加密解密方法(例如base64加解密、哈希加...

Global site tag (gtag.js) - Google Analytics