[英]How can I create an avi video file in real time while my application is processing?
编辑:
这就是我所做的:在 Form1 中:
ScreenCapture sc;
AviFile af;
在 Form1 构造函数中:
sc = new ScreenCapture();
af = new AviFile();
在 Form1 timer1 滴答事件中:
private void timer1_Tick(object sender, EventArgs e)
{
if (playImages == true)
{
timer1.Enabled = false;
play();
timer1.Enabled = true;
}
else
{
af.CreateAvi(this.sc);
}
}
更改后的 AviFile 类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AviFile;
using ScreenShotDemo;
namespace ScreenVideoRecorder
{
class AviFile
{
AviManager aviManager;
Bitmap bmp;
public AviFile()
{
aviManager = new AviManager(@"d:\testdata\new.avi", false);
}
public void CreateAvi(ScreenCapture sc)
{
bmp = new Bitmap(sc.CaptureScreen());
VideoStream aviStream = aviManager.AddVideoStream(false, 25, bmp);//dlg.Rate, bmp);
aviStream.AddFrame(bmp);
bmp.Dispose();
}
public AviManager avim
{
get
{
return aviManager;
}
set
{
aviManager = value;
}
}
}
}
在 Form1 按钮单击事件停止:
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
if (af != null)
{
af.avim.Close();
}
}
但是在截取屏幕截图几秒钟后运行它之后停止之前:这是按钮1,它启动计时器并截取屏幕截图:
private void button1_Click(object sender, EventArgs e)
{
playImages = false;
timer1.Enabled = true;
}
几秒钟后,我得到一个例外:
Exception in AVIFileCreateStream: -2147205019
System.Exception was unhandled
HResult=-2146233088
Message=Exception in AVIFileCreateStream: -2147205019
Source=AviFile
StackTrace:
at AviFile.VideoStream.CreateStreamWithoutFormat()
at AviFile.VideoStream.CreateStream()
at AviFile.VideoStream..ctor(Int32 aviFile, Boolean writeCompressed, Double frameRate, Bitmap firstFrame)
at AviFile.AviManager.AddVideoStream(Boolean isCompressed, Double frameRate, Bitmap firstFrame)
at ScreenVideoRecorder.AviFile.CreateAvi(ScreenCapture sc) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\AviFile.cs:line 27
at ScreenVideoRecorder.Form1.timer1_Tick(Object sender, EventArgs e) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\Form1.cs:line 61
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ScreenVideoRecorder.Program.Main() in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
例外是在课程中: VideoStream 这不是我的课程,它来自我下载源文件的网站: http : //www.codeproject.com/Articles/7388/A-Simple-C-Wrapper-for-the-AviFile -图书馆
我该如何解决?
从你的问题中并不完全清楚你在问什么。
但似乎您只想将屏幕截图直接放入AviFile
,而不是将它们保存到磁盘然后加载它们。
关键的一段代码是这样的:
bitmap = (Bitmap)Bitmap.FromFile(Form1.imagesFiles[n]);
aviStream.AddFrame(bitmap);
bitmap.Dispose();
您可以执行以下操作,而不是将它们从文件加载到内存中:
bitmap = new Bitmap(sc.CaptureScreen());
aviStream.AddFrame(bitmap);
bitmap.Dispose();
这样你就不会保存到磁盘,你只是直接插入到 AVI 流中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.