Im getting error: DisconnectedContext was detected when extracting frames from a viddeo file. Whats

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
The program is extracting frames from a video file. In this case im doing that it will extract 1/20(20 frames per second).
And im using a 99.5MB video file wich is .3gp(Htc desire phone file video i recorded).

Its extracting all the frames about 5519 files ! When its getting to the end im getting the error and i cant close the program regular it dosent close like when its finished. I see the icon of the running program when standing on it with the mouse i
see: Form1 but thats it.
I need to make OK on the error message window and then the program quit.

This is the error im message:

Context 0x5abe30 is disconnected. Releasing the interfaces from the current context (context 0x5abcc0). This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely
done with the RuntimeCallableWrappers that represent COM components that live inside them.

Im clicking OK on this message error and then im doing Debug>Stop Debugging

Cant figure out why this error is popup and if all frames realy extracted before the error popup.
I can see the frames and edit each one. Each frame is saved on my hard disk as .bmp files the first one name is: 000000,000001...last one is: 005519

This is the full code of my program in Form1.cs:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using ExtractFrames.Properties;
<span style="color:Blue; using ExtractFrames;
<span style="color:Blue; using JockerSoft.Media;
<span style="color:Blue; using JockerSoft;
<span style="color:Blue; using Interop.qedit;
<span style="color:Blue; using Interop;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using System.IO;

<span style="color:Blue; namespace ExtractFrames
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
FolderBrowserDialog fb;
OpenFileDialog openFileDialog1;
<span style="color:Blue; private <span style="color:Blue; bool _connect = <span style="color:Blue; false;
<span style="color:Blue; private <span style="color:Blue; bool _rightButton;
List<LinePoint> _points = <span style="color:Blue; null;
<span style="color:Blue; private <span style="color:Blue; float _circleWidth = 10;
DirectoryInfo dir1;
FileInfo[] fi;
<span style="color:Blue; double streamlength;
MediaDet mdd;
<span style="color:Blue; double x;
<span style="color:Blue; string sf;
<span style="color:Blue; double[] streamDouble ;
<span style="color:Blue; string strVideoFile;
<span style="color:Blue; public Form1()
{
InitializeComponent();
fb = <span style="color:Blue; new FolderBrowserDialog();
openFileDialog1 = <span style="color:Blue; new OpenFileDialog();
mdd = <span style="color:Blue; new MediaDet();
streamDouble = <span style="color:Blue; new <span style="color:Blue; double[1];
sf = <span style="color:#A31515; @"d:Frames";
dir1 = <span style="color:Blue; new DirectoryInfo(sf);
fi = dir1.GetFiles(<span style="color:#A31515; "*.bmp");
<span style="color:Blue; if (fi.Length == 0)
{
trackBar1.Enabled = <span style="color:Blue; false;
}
<span style="color:Blue; else
{
trackBar1.Minimum = 0;
trackBar1.Maximum = fi.Length - 1;
trackBar1.Value = 0;<span style="color:Green; //fi.Length - 1;
Bitmap newImage;
newImage = <span style="color:Blue; new Bitmap(fi[0].FullName);
pictureBox1.Image = newImage;
}
}

<span style="color:Blue; private <span style="color:Blue; void Form1_Load(<span style="color:Blue; object sender, EventArgs e)
{

}

<span style="color:Blue; private <span style="color:Blue; void getFrameRate()
{
<span style="color:Green; // Get framerate
MediaDet md = <span style="color:Blue; new MediaDet();
md.Filename = strVideoFile;
<span style="color:Green; //md.CurrentStream = 0; // choose the video stream
x = md.FrameRate;
<span style="color:Blue; double i = md.StreamLength;

}

<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void SaveFramesFromVideo(<span style="color:Blue; string videoFile, <span style="color:Blue; double[] positions, <span style="color:Blue; string outputBitmapFiles)
{
<span style="color:Blue; if (videoFile == <span style="color:Blue; null)
<span style="color:Blue; throw <span style="color:Blue; new ArgumentNullException(<span style="color:#A31515; "videoFile");

<span style="color:Blue; double streamLength;

IMediaDet mediaDet = <span style="color:Blue; null;
<span style="color:Blue; try
{
_AMMediaType mediaType;
<span style="color:Blue; if (openVideoStream(videoFile, <span style="color:Blue; out mediaDet, <span style="color:Blue; out mediaType))
{
streamLength = mediaDet.StreamLength;
Size target = getVideoSize(mediaType);
<span style="color:Blue; int iteration = 0;
<span style="color:Blue; foreach (<span style="color:Blue; double position <span style="color:Blue; in positions)
{
iteration++;
<span style="color:Blue; string outputBitmapFile = <span style="color:Blue; string.Format(outputBitmapFiles, iteration);
mediaDet.WriteBitmapBits(position, target.Width, target.Height, outputBitmapFile);
}
<span style="color:Blue; return;
}
}
<span style="color:Blue; catch (COMException)
{
<span style="color:Blue; throw <span style="color:Blue; new InvalidVideoFileException();
}
<span style="color:Blue; finally
{
<span style="color:Blue; if (mediaDet != <span style="color:Blue; null)
Marshal.ReleaseComObject(mediaDet);
}

<span style="color:Blue; throw <span style="color:Blue; new InvalidVideoFileException(<span style="color:#A31515; "No video stream was found");
}


<span style="color:Blue; private <span style="color:Blue; static Size getVideoSize(_AMMediaType mediaType)
{
WinStructs.VIDEOINFOHEADER videoInfo = (WinStructs.VIDEOINFOHEADER)Marshal.PtrToStructure(mediaType.pbFormat, <span style="color:Blue; typeof(WinStructs.VIDEOINFOHEADER));

<span style="color:Blue; return <span style="color:Blue; new Size(videoInfo.bmiHeader.biWidth, videoInfo.bmiHeader.biHeight);
}

<span style="color:Blue; private <span style="color:Blue; static Size scaleToFit(Size target, Size original)
{
<span style="color:Blue; if (target.Height * original.Width > target.Width * original.Height)
target.Height = target.Width * original.Height / original.Width;
<span style="color:Blue; else
target.Width = target.Height * original.Width / original.Height;

<span style="color:Blue; return target;
}
<span style="color:Blue; private <span style="color:Blue; static Size scaleToFitSmart(Size target, Size original)
{
target = scaleToFit(target, original);

<span style="color:Blue; if (target.Width > original.Width || target.Height > original.Height)
<span style="color:Blue; return original;

<span style="color:Blue; return target;
}


<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; bool openVideoStream(<span style="color:Blue; string videoFile, <span style="color:Blue; out IMediaDet mediaDet, <span style="color:Blue; out _AMMediaType aMMediaType)
{
mediaDet = <span style="color:Blue; new MediaDet();

<span style="color:Green; //loads file
mediaDet.Filename = videoFile;

<span style="color:Green; //gets # of streams
<span style="color:Blue; int streamsNumber = mediaDet.OutputStreams;

<span style="color:Green; //finds a video stream
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < streamsNumber; i++)
{
mediaDet.CurrentStream = i;
_AMMediaType mediaType = mediaDet.StreamMediaType;

<span style="color:Blue; if (mediaType.majortype == JockerSoft.Media.MayorTypes.MEDIATYPE_Video)
{
<span style="color:Green; //video stream found
aMMediaType = mediaType;
<span style="color:Blue; return <span style="color:Blue; true;
}
}

<span style="color:Green; //no video stream found
Marshal.ReleaseComObject(mediaDet);
mediaDet = <span style="color:Blue; null;
aMMediaType = <span style="color:Blue; new _AMMediaType();
<span style="color:Blue; return <span style="color:Blue; false;
}


<span style="color:Blue; private <span style="color:Blue; bool LoadPictureAt(<span style="color:Blue; int nIndex, <span style="color:Blue; object c)
{

<span style="color:Blue; bool bRet = <span style="color:Blue; false;

<span style="color:Blue; if (nIndex >= 0 && nIndex < fi.Length)
{
<span style="color:Blue; if (c.Equals(trackBar1))

pictureBox1.Load(fi[nIndex].FullName);
bRet = <span style="color:Blue; true;

}

<span style="color:Blue; return bRet;
}

<span style="color:Blue; private <span style="color:Blue; void trackBar1_Scroll(<span style="color:Blue; object sender, EventArgs e)
{
LoadPictureAt(trackBar1.Value, sender);
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_Paint(<span style="color:Blue; object sender, PaintEventArgs e)
{
<span style="color:Green; /*if (_points != null)
{
Pen p = new Pen(Color.Red);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
using (SolidBrush myBrush = new SolidBrush(Color.Red))
{
for (int i = 0; i < _points.Count; i++)
{
Point pt = _points;
e.Graphics.FillEllipse(myBrush, new RectangleF(pt.X - _circleWidth / 2F, pt.Y - _circleWidth / 2F, _circleWidth, _circleWidth));

if (_connect && i > 0)
e.Graphics.DrawLine(Pens.Blue, _points[i - 1], _points);
}
}
}*/

<span style="color:Blue; if (_points != <span style="color:Blue; null)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
<span style="color:Blue; using (SolidBrush myBrush = <span style="color:Blue; new SolidBrush(Color.Red))
{
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < _points.Count; i++)
{
<span style="color:Blue; try
{
Point pt = _points.Point;
e.Graphics.FillEllipse(myBrush, <span style="color:Blue; new RectangleF(pt.X - _circleWidth / 2F, pt.Y - _circleWidth / 2F, _circleWidth, _circleWidth));

<span style="color:Blue; if (i > 0)
{
<span style="color:Blue; if (_points.Connected)
e.Graphics.DrawLine(Pens.Blue, _points[i - 1].Point, _points.Point);
}
}
<span style="color:Blue; catch ( Exception err)
{
textBox1.Text = err.ToString();
}
}
}
}


}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseDown(<span style="color:Blue; object sender, MouseEventArgs e)
{
<span style="color:Green; /*if (e.Button == MouseButtons.Left)
{
if (_points == null)
_points = new List<Point>();
_points.Add(new Point(e.X, e.Y));
pictureBox1.Invalidate();
}*/
<span style="color:Blue; if (e.Button == MouseButtons.Left)
{
<span style="color:Blue; if (_points == <span style="color:Blue; null)
_points = <span style="color:Blue; new List<LinePoint>();
_points.Add(<span style="color:Blue; new LinePoint() { Point = <span style="color:Blue; new Point(e.X, e.Y), Connected = _connect });
pictureBox1.Invalidate();
}

<span style="color:Blue; if (e.Button == MouseButtons.Right)
{
_connect = !_connect;
_rightButton = <span style="color:Blue; true;
}
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_DoubleClick(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Green; /* _connect = !_connect;
pictureBox1.Invalidate();*/
_points.Clear();
<span style="color:Blue; if (_rightButton && _points != <span style="color:Blue; null)
_points.Clear();

pictureBox1.Invalidate();
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseUp(<span style="color:Blue; object sender, MouseEventArgs e)
{
_rightButton = <span style="color:Blue; false;
}

<span style="color:Blue; public <span style="color:Blue; class LinePoint
{
<span style="color:Blue; public Point Point { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; bool Connected { <span style="color:Blue; get; <span style="color:Blue; set; }
}

<span style="color:Blue; private <span style="color:Blue; void pictureBox1_MouseMove(<span style="color:Blue; object sender, MouseEventArgs e)
{
<span style="color:Blue; if (e.Button == MouseButtons.Left)
{


}
}

<span style="color:Blue; private <span style="color:Blue; void loadVideoToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
openFileDialog1.Title = <span style="color:#A31515; "Select a video file";
openFileDialog1.InitialDirectory = "c:\";
openFileDialog1.FileName = <span style="color:Blue; null;
openFileDialog1.Filter = <span style="color:#A31515; "Video Files|*.wmv;*.3gp;*.avi";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = <span style="color:Blue; true;
DialogResult result1 = openFileDialog1.ShowDialog();
<span style="color:Blue; string file1 = openFileDialog1.FileName;
<span style="color:Blue; if (result1 == DialogResult.OK)
{
strVideoFile = file1;
mdd.Filename = strVideoFile;
streamlength = mdd.StreamLength;
<span style="color:Blue; double dt = (<span style="color:Blue; double)1 / (<span style="color:Blue; double)20; <span style="color:Green; //mdd.FrameRate;
<span style="color:Blue; int i = 0;
<span style="color:Blue; for (<span style="color:Blue; double x = 0; x < streamlength; x += dt)
{
streamDouble[0] = x;
SaveFramesFromVideo(strVideoFile, streamDouble, sf + i.ToString(<span style="color:#A31515; "D6") + <span style="color:#A31515; ".bmp");
i++;
}
fi = dir1.GetFiles(<span style="color:#A31515; "*.bmp");
<span style="color:Blue; if (fi.Length == 0)
{
trackBar1.Enabled = <span style="color:Blue; false;
}
<span style="color:Blue; else
{
trackBar1.Enabled = <span style="color:Blue; true;
trackBar1.Minimum = 0;
trackBar1.Maximum = fi.Length - 1;
trackBar1.Value = 0;<span style="color:Green; //fi.Length - 1;
Bitmap newImage;
newImage = <span style="color:Blue; new Bitmap(fi[0].FullName);
pictureBox1.Image = newImage;
}
}
<span style="color:Blue; if (result1 == DialogResult.Cancel)
{
<span style="color:Blue; if (file1 == <span style="color:#A31515; "")
{
}
}
}

<span style="color:Blue; private <span style="color:Blue; void changeExtractedFramesDirectoryToolStripMenuItem_Click(<span style="color:Blue; object sender, EventArgs e)
{
fb.Description = <span style="color:#A31515; "Directory To Extract Frames";
DialogResult result = fb.ShowDialog();
<span style="color:Blue; string file1 = fb.SelectedPath;
}
}
}

[/code]

In the refereces i have added: Interop.qedit.dll and JockerSoft.Media.dll

Thanks for the help.
<hr class="sig danieli

View the full article
 
Back
Top