AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

AVIWriter "Failed to create compressed stream"

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

AVIWriter "Failed to create compressed stream"

Postby TheMonz31 » Wed Oct 06, 2010 2:59 pm

Hi

Here is the code I use :

public class ScreenCapture
{
VideoCaptureDevice dev = new VideoCaptureDevice();
AVIWriter writer = null;
bool DoCapture = false;
private Rectangle captureRec = Rectangle.Empty;
private bool captureCursor = true;

public bool CaptureCursor
{
get { return captureCursor; }
set { captureCursor = value; }
}


public bool IsUnderCapture
{
get
{
return DoCapture;
}
}

public void SetCaptureSource(Rectangle rec)
{
captureRec = rec;
}

public void SetCaptureSource(Control c)
{
captureRec = c.ClientRectangle;
}

public List<string> GetCodec()
{
List<string> codecs = new List<string>();
FilterInfoCollection codecList = new FilterInfoCollection(FilterCategory.VideoCompressorCategory);
foreach (FilterInfo info in codecList)
{
codecs.Add(info.Name);
}
return codecs;
}


public void StartCapture(string filename, int width, int height, string codec)
{
//FilterInfoCollection codecList = new FilterInfoCollection(FilterCategory.VideoCompressorCategory);
//foreach (FilterInfo info in codecList)
//{
// Trace.WriteLine(info.MonikerString);
//}
//writer = new AVIWriter(codecList[7].Name);

writer = new AVIWriter(codec);
writer.Open(filename, width, height);
writer.FrameRate = 15;
Thread videoThread = new Thread(new ThreadStart(Capture));
DoCapture = true;
videoThread.Start();
}

public void StopCapture()
{
DoCapture = false;
}

private void Capture()
{
Stopwatch time = new Stopwatch();
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
Bitmap bmp = new Bitmap(screenWidth, screenHeight);
while (DoCapture)
{
time.Start();
Capture(bmp);
writer.AddFrame(bmp);
time.Stop();
double timeToSleep = 40 - time.ElapsedMilliseconds;
if (timeToSleep > 0)
{
Thread.Sleep((int)timeToSleep);
}
time.Reset();

}
writer.Close();
}

private void Capture(Bitmap bmp)
{
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(Point.Empty, Point.Empty, bmp.Size);
if (captureCursor)
{
Rectangle cursorBounds = new Rectangle(Cursor.Position, Cursor.Current.Size);
Cursors.Default.Draw(g, cursorBounds);
}
g.Dispose();
}

private Bitmap Capture(Rectangle rect, bool isRect)
{
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;

Bitmap screenshot = null;

screenshot = new Bitmap(screenWidth, screenHeight);

//if (!isRect)

//else if (isRect)
// screenshot = new Bitmap(rect.Width, rect.Height);

Graphics g = Graphics.FromImage(screenshot);
if (!isRect)
{
g.CopyFromScreen(Point.Empty, Point.Empty, screenshot.Size);
}
else if (isRect)
{
g.CopyFromScreen(new Point(rect.X, rect.Y), Point.Empty, rect.Size);
}


g.Dispose();
return screenshot;
}

}

public partial class Form1 : Form
{
ScreenCapture capture = null;
string DestinationFile = string.Empty;

public Form1()
{
InitializeComponent();

capture = new ScreenCapture();
FillCodecList();

}

private void FillCodecList()
{
List<string> codecs = capture.GetCodec();
comboBox1.Items.AddRange(codecs.ToArray());
}


private void button1_Click(object sender, EventArgs e)
{
if (capture.IsUnderCapture)
{
capture.StopCapture();
button1.Text = "Start Capture";
}
else
{
string codec = comboBox1.Text;
if ((codec == string.Empty) || (codec == null))
{
MessageBox.Show("No codec has been selected !!!");
return;
}

if (DestinationFile == string.Empty)
{
MessageBox.Show("Please select a destination file !!!");
return;
}

capture.CaptureCursor = !checkBox1.Checked;
//capture.StartCapture(DestinationFile, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, codec);
capture.StartCapture(DestinationFile, 320, 240, codec);
button1.Text = "Stop Capture";
}
}

//private void Form1_Load(object sender, EventArgs e)
//{
// //WindowState = FormWindowState.Maximized;
// //this.Opacity = 0.8f;
//}

//private void Form1_MouseMove(object sender, MouseEventArgs e)
//{
// //this.Text = "X = " + e.X + " Y " + e.Y;
//}



private void button3_Click(object sender, EventArgs e)
{
if (sfd.ShowDialog() == DialogResult.OK)
{
DestinationFile = sfd.FileName;
}
}
}


Everything is working fine on my Laptop, running Windows 7 32 Bits.

The program is compiled under 0x86 CPU Directive.

when I try to make it work on other computer (Vista 32, XP 32), it crashes with the "famous" "Failed Creating compressed stream"...

I've tried the "DIB " issue... work on my computer, not on other computer)

I've tried "DIB " + size of 320*240 ==> Work on my machine but not on other machine

I tried to install AForge framework on other crashing machine ==> Same result, still crashing on the open method.

Out of idea, I was wondering what else I could try ? test ? do ? in order to find what is going on around this AVIWriter class...

I Can provide the full VS2008 Solution if anyone has time and kindness to test it on its computer...

Thanks for your help... anyone :) !!!!
TheMonz31
 
Posts: 3
Joined: Wed Oct 06, 2010 2:54 pm



Return to AForge.NET Framework