C# 如何在应用程序处理时实时创建avi视频文件?
编辑: 这就是我所做的: 在表格1中:C# 如何在应用程序处理时实时创建avi视频文件?,c#,C#,编辑: 这就是我所做的: 在表格1中: ScreenCapture sc; AviFile af; 在Form1构造函数中: sc = new ScreenCapture(); af = new AviFile(); 在Form1 timer1 tick事件中: private void timer1_Tick(object sender, EventArgs e) { if (playImages == true) {
ScreenCapture sc;
AviFile af;
在Form1构造函数中:
sc = new ScreenCapture();
af = new AviFile();
在Form1 timer1 tick事件中:
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,它不是我的类,它来自我下载源文件的站点:
我怎样才能解决这个问题?从你的问题中不完全清楚你在问什么 但似乎您只想将屏幕截图直接放入
avi文件
,而不是将其保存到磁盘中,然后再将其加载
关键代码如下:
bitmap = (Bitmap)Bitmap.FromFile(Form1.imagesFiles[n]);
aviStream.AddFrame(bitmap);
bitmap.Dispose();
您只需执行以下操作,而不是从文件将其加载到内存中:
bitmap = new Bitmap(sc.CaptureScreen());
aviStream.AddFrame(bitmap);
bitmap.Dispose();
这样,您就不会保存到磁盘,而是直接插入AVI流。您的问题并不完全清楚您在问什么 但似乎您只想将屏幕截图直接放入
avi文件
,而不是将其保存到磁盘中,然后再将其加载
关键代码如下:
bitmap = (Bitmap)Bitmap.FromFile(Form1.imagesFiles[n]);
aviStream.AddFrame(bitmap);
bitmap.Dispose();
您只需执行以下操作,而不是从文件将其加载到内存中:
bitmap = new Bitmap(sc.CaptureScreen());
aviStream.AddFrame(bitmap);
bitmap.Dispose();
这样,您就不用保存到磁盘,只需直接插入AVI流。为什么不让CreateAvi接收位图列表并跳过文件保存/文件加载?甚至更好的是,让CreateAvi接收视频流本身。为什么不让CreateAvi接收位图列表并跳过文件保存/文件加载?或者更好的是,让CreateAvi接收视频流本身。Clint我在aviStream上看到它没有被分配,所以它将一直为空。用我更改的内容更新了我的问题。我将使用aviManager在停止计时器的按钮3单击事件中创建文件。或者我应该在计时器运行时使用它,这样它将创建文件并不断更新它?我将它设置为类中的一个字段变量,然后当计时器运行时,继续向其添加帧,我假设您的目标是继续拍摄屏幕截图并将其缝合到AVI中?如果是这样的话,您希望在每个屏幕截图中不断添加帧,那么完成后只需关闭管理器。克林特:这就是我到目前为止所做的,几秒钟后我遇到了一个异常。我会更新我的问题。Clint我在aviStream上发现它没有被分配,所以它将一直为空。用我更改的内容更新了我的问题。我将使用aviManager在停止计时器的按钮3单击事件中创建文件。或者我应该在计时器运行时使用它,这样它将创建文件并不断更新它?我将它设置为类中的一个字段变量,然后当计时器运行时,继续向其添加帧,我假设您的目标是继续拍摄屏幕截图并将其缝合到AVI中?如果是这样的话,您希望在每个屏幕截图中不断添加帧,那么完成后只需关闭管理器。克林特:这就是我到目前为止所做的,几秒钟后我遇到了一个异常。我会更新我的问题。