EDN Admin
Well-known member
This is the class code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Drawing.Imaging;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using DirectShowLib;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using Extracting_Frames;
<span style="color:Blue; internal <span style="color:Blue; class WmvAdapter : ISampleGrabberCB, IDisposable
{
<span style="color:Blue; #region Fields
<span style="color:Blue; public List<<span style="color:Blue; double> results { <span style="color:Blue; get; <span style="color:Blue; set; }
StreamWriter w = <span style="color:Blue; new StreamWriter(<span style="color:#A31515; @"d:results.txt");
<span style="color:Blue; private IFilterGraph2 _filterGraph;
<span style="color:Blue; private IMediaControl _mediaCtrl;
<span style="color:Blue; private IMediaEvent _mediaEvent;
<span style="color:Blue; private <span style="color:Blue; int _width;
<span style="color:Blue; private <span style="color:Blue; int _height;
<span style="color:Blue; private <span style="color:Blue; readonly <span style="color:Blue; string _outFolder;
<span style="color:Blue; private <span style="color:Blue; int _frameId;
<span style="color:Green; //better use a custom EventHandler that passes the results of the action to the subscriber.
<span style="color:Blue; public <span style="color:Blue; delegate <span style="color:Blue; void EventHandler(<span style="color:Blue; object sender, EventArgs e);
<span style="color:Blue; public <span style="color:Blue; event EventHandler StatusChanged;
<span style="color:Blue; public <span style="color:Blue; delegate <span style="color:Blue; void FrameCountEventHandler(<span style="color:Blue; object sender, FrameCountEventArgs e);
<span style="color:Blue; public <span style="color:Blue; event FrameCountEventHandler FrameCountAvailable;
<span style="color:Blue; public <span style="color:Blue; delegate <span style="color:Blue; void ProgressEventHandler(<span style="color:Blue; object sender, ProgressEventArgs e);
<span style="color:Blue; public <span style="color:Blue; event ProgressEventHandler ProgressChanged;
<span style="color:Blue; private IMediaSeeking _mSeek;
<span style="color:Blue; private <span style="color:Blue; long _duration = 0;
<span style="color:Blue; private <span style="color:Blue; long _avgFrameTime = 0;
<span style="color:Blue; #endregion
<span style="color:Blue; #region Constructors and Destructors
<span style="color:Blue; public WmvAdapter(<span style="color:Blue; string file, <span style="color:Blue; string outFolder)
{
_outFolder = outFolder;
<span style="color:Blue; try
{
SetupGraph(file);
}
<span style="color:Blue; catch
{
Dispose();
<span style="color:Blue; throw;
}
}
~WmvAdapter()
{
CloseInterfaces();
}
<span style="color:Blue; #endregion
<span style="color:Blue; public <span style="color:Blue; void Dispose()
{
CloseInterfaces();
}
<span style="color:Blue; public <span style="color:Blue; void Start()
{
<span style="color:Green; //Edit: get the duration
_mSeek.GetDuration(<span style="color:Blue; out _duration);
EstimateFrameCount();
<span style="color:Blue; int hr = _mediaCtrl.Run();
WaitUntilDone();
DsError.ThrowExceptionForHR(hr);
}
<span style="color:Blue; public <span style="color:Blue; void WaitUntilDone()
{
<span style="color:Blue; int hr;
<span style="color:Blue; const <span style="color:Blue; int eAbort = <span style="color:Blue; unchecked((<span style="color:Blue; int)0x80004004);
<span style="color:Blue; do
{
System.Windows.Forms.Application.DoEvents();
EventCode evCode;
hr = _mediaEvent.WaitForCompletion(100, <span style="color:Blue; out evCode);
} <span style="color:Blue; while (hr == eAbort);
w.Close();
<span style="color:Green; //here you can close the streamwriter, if its on class level
<span style="color:Green; //but dont forget to re-instanciate it when doing the job more than once from
OnStatusChanged();
DsError.ThrowExceptionForHR(hr);
}
<span style="color:Green; /*int hr;
const int eAbort = unchecked((int)0x80004004);
do
{
System.Windows.Forms.Application.DoEvents();
EventCode evCode;
hr = _mediaEvent.WaitForCompletion(100, out evCode);
} while (hr == eAbort);
OnStatusChanged();
DsError.ThrowExceptionForHR(hr);*/
<span style="color:Green; //Edit: added events
<span style="color:Blue; protected <span style="color:Blue; virtual <span style="color:Blue; void OnStatusChanged()
{
<span style="color:Blue; if (StatusChanged != <span style="color:Blue; null)
StatusChanged(<span style="color:Blue; this, <span style="color:Blue; new EventArgs());
}
<span style="color:Blue; protected <span style="color:Blue; virtual <span style="color:Blue; void OnFrameCountAvailable(<span style="color:Blue; long frameCount)
{
<span style="color:Blue; if (FrameCountAvailable != <span style="color:Blue; null)
FrameCountAvailable(<span style="color:Blue; this, <span style="color:Blue; new FrameCountEventArgs() { FrameCount = frameCount });
}
<span style="color:Blue; protected <span style="color:Blue; virtual <span style="color:Blue; void OnProgressChanged(<span style="color:Blue; int frameID)
{
<span style="color:Blue; if (ProgressChanged != <span style="color:Blue; null)
ProgressChanged(<span style="color:Blue; this, <span style="color:Blue; new ProgressEventArgs() { FrameID = frameID });
}
<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; build the capture graph for grabber. </summary>
<span style="color:Blue; private <span style="color:Blue; void SetupGraph(<span style="color:Blue; string file)
{
ISampleGrabber sampGrabber = <span style="color:Blue; null;
IBaseFilter capFilter = <span style="color:Blue; null;
IBaseFilter nullrenderer = <span style="color:Blue; null;
_filterGraph = (IFilterGraph2)<span style="color:Blue; new FilterGraph();
_mediaCtrl = (IMediaControl)_filterGraph;
_mediaEvent = (IMediaEvent)_filterGraph;
_mSeek = (IMediaSeeking)_filterGraph;
<span style="color:Blue; var mediaFilt = (IMediaFilter)_filterGraph;
<span style="color:Blue; try
{
<span style="color:Green; // Add the video source
<span style="color:Blue; int hr = _filterGraph.AddSourceFilter(file, <span style="color:#A31515; "Ds.NET FileFilter", <span style="color:Blue; out capFilter);
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // Get the SampleGrabber interface
sampGrabber = <span style="color:Blue; new SampleGrabber() <span style="color:Blue; as ISampleGrabber;
<span style="color:Blue; var baseGrabFlt = sampGrabber <span style="color:Blue; as IBaseFilter;
ConfigureSampleGrabber(sampGrabber);
<span style="color:Green; // Add the frame grabber to the graph
hr = _filterGraph.AddFilter(baseGrabFlt, <span style="color:#A31515; "Ds.NET Grabber");
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // ---------------------------------
<span style="color:Green; // Connect the file filter to the sample grabber
<span style="color:Green; // Hopefully this will be the video pin, we could check by reading its mediatype
IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0);
<span style="color:Green; // Get the input pin from the sample grabber
IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);
hr = _filterGraph.Connect(iPinOut, iPinIn);
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // Add the null renderer to the graph
nullrenderer = <span style="color:Blue; new NullRenderer() <span style="color:Blue; as IBaseFilter;
hr = _filterGraph.AddFilter(nullrenderer, <span style="color:#A31515; "Null renderer");
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // ---------------------------------
<span style="color:Green; // Connect the sample grabber to the null renderer
iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);
iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0);
hr = _filterGraph.Connect(iPinOut, iPinIn);
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // Turn off the clock. This causes the frames to be sent
<span style="color:Green; // thru the graph as fast as possible
hr = mediaFilt.SetSyncSource(<span style="color:Blue; null);
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // Read and cache the image sizes
SaveSizeInfo(sampGrabber);
}
<span style="color:Blue; finally
{
<span style="color:Blue; if (capFilter != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(capFilter);
}
<span style="color:Blue; if (sampGrabber != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(sampGrabber);
}
<span style="color:Blue; if (nullrenderer != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(nullrenderer);
}
GC.Collect();
}
}
<span style="color:Blue; private <span style="color:Blue; void EstimateFrameCount()
{
<span style="color:Green; //1sec / averageFrameTime
<span style="color:Blue; long fr = 10000000 / _avgFrameTime;
<span style="color:Blue; double frameCount = fr * (_duration / 10000000.0);
OnFrameCountAvailable((<span style="color:Blue; long)frameCount);
}
<span style="color:Blue; private <span style="color:Blue; void SaveSizeInfo(ISampleGrabber sampGrabber)
{
<span style="color:Green; // Get the media type from the SampleGrabber
<span style="color:Blue; var media = <span style="color:Blue; new AMMediaType();
<span style="color:Blue; int hr = sampGrabber.GetConnectedMediaType(media);
DsError.ThrowExceptionForHR(hr);
<span style="color:Blue; if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
{
<span style="color:Blue; throw <span style="color:Blue; new NotSupportedException(<span style="color:#A31515; "Unknown Grabber Media Format");
}
<span style="color:Green; // Grab the size info
<span style="color:Blue; var videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, <span style="color:Blue; typeof(VideoInfoHeader));
_width = videoInfoHeader.BmiHeader.Width;
_height = videoInfoHeader.BmiHeader.Height;
<span style="color:Green; //Edit: get framerate
_avgFrameTime = videoInfoHeader.AvgTimePerFrame;
DsUtils.FreeAMMediaType(media);
GC.Collect();
}
<span style="color:Blue; private <span style="color:Blue; void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
{
<span style="color:Blue; var media = <span style="color:Blue; new AMMediaType
{
majorType = MediaType.Video,
subType = MediaSubType.RGB24,
formatType = FormatType.VideoInfo
};
<span style="color:Blue; int hr = sampGrabber.SetMediaType(media);
DsError.ThrowExceptionForHR(hr);
DsUtils.FreeAMMediaType(media);
GC.Collect();
hr = sampGrabber.SetCallback(<span style="color:Blue; this, 1);
DsError.ThrowExceptionForHR(hr);
}
<span style="color:Blue; private <span style="color:Blue; void CloseInterfaces()
{
<span style="color:Blue; try
{
<span style="color:Blue; if (_mediaCtrl != <span style="color:Blue; null)
{
_mediaCtrl.Stop();
_mediaCtrl = <span style="color:Blue; null;
}
}
<span style="color:Blue; catch (Exception ex)
{
Debug.WriteLine(ex);
}
<span style="color:Blue; if (_filterGraph != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(_filterGraph);
_filterGraph = <span style="color:Blue; null;
}
GC.Collect();
}
<span style="color:Blue; int ISampleGrabberCB.SampleCB(<span style="color:Blue; double sampleTime, IMediaSample pSample)
{
Marshal.ReleaseComObject(pSample);
<span style="color:Blue; return 0;
}
Bitmap bitmap;
<span style="color:Green; //add a boolean property to indicate the save-mode
<span style="color:Blue; public <span style="color:Blue; bool SaveToDisc { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Green; //the list for the bitmaps
<span style="color:Blue; public List<Bitmap> Images { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; int ISampleGrabberCB.BufferCB(<span style="color:Blue; double sampleTime, IntPtr pBuffer, <span style="color:Blue; int bufferLen)
{
<span style="color:Blue; using (bitmap = <span style="color:Blue; new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
<span style="color:Green; //Bitmap bitmaps = PixelsGray(bitmap);
<span style="color:Blue; double average = GetAveragePixelValue(bitmap);
<span style="color:Green; //Bitmap bitmaps = converttoBlackAndWhite(bitmap);
<span style="color:Blue; if (SaveToDisc)
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString(<span style="color:#A31515; "D6") + <span style="color:#A31515; ".bmp"));
_frameId++;
}
<span style="color:Blue; else
{
<span style="color:Blue; if (average > 10)
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString(<span style="color:#A31515; "D6") + <span style="color:#A31515; ".bmp"));
}
w.WriteLine(average);
<span style="color:Blue; if (results == <span style="color:Blue; null)
results = <span style="color:Blue; new List<<span style="color:Blue; double>();
results.Add(average);
<span style="color:Green; //w.WriteLine(" " + _frameId);
_frameId++;
<span style="color:Green; /*if (_frameId == 9227 || _frameId == 9228)
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));
}*/
<span style="color:Blue; if (_frameId % 100 == 0)
OnProgressChanged(_frameId);
<span style="color:Green; /*if (_frameId >= 1800)
{
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));
}*/
<span style="color:Green; //let only report each 100 frames for performance
}
}
<span style="color:Blue; return 0;
}
<span style="color:Blue; private Bitmap PixelsGray(Bitmap originals)
{
<span style="color:Blue; double numberpixels = 0;
<span style="color:Blue; double big_Sum = 0;
<span style="color:Blue; double sum;
Bitmap newGray;
newGray = <span style="color:Blue; new Bitmap(originals.Width, originals.Height);
<span style="color:Blue; int x, y;
<span style="color:Blue; for (x = 0; x < newGray.Width; x++)
{
<span style="color:Blue; for (y = 0; y < originals.Height; y++)
{
Color originalColor = originals.GetPixel(x, y);
<span style="color:Blue; double WhiteBlack = (<span style="color:Blue; int)((originalColor.R) + (originalColor.G)
+ (originalColor.B));
sum = WhiteBlack / 3;
big_Sum = big_Sum + sum;
<span style="color:Green; //newColor = Color.FromArgb(255,(int)sum,(int)sum,(int)sum);
<span style="color:Green; //newGray.SetPixel(x, y, newColor);
numberpixels = originals.Width * originals.Height;
}
}
<span style="color:Blue; double result = big_Sum / numberpixels;
<span style="color:Blue; if (results == <span style="color:Blue; null)
results = <span style="color:Blue; new List<<span style="color:Blue; double>();
results.Add(result);
<span style="color:Blue; return newGray;
}
<span style="color:Blue; private Bitmap converttoBlackAndWhite(Bitmap original)
{
Bitmap newBitmap = <span style="color:Blue; new Bitmap(original.Width, original.Height);
<span style="color:Green; //get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
<span style="color:Green; //create the grayscale ColorMatrix
ColorMatrix colorMatrix = <span style="color:Blue; new ColorMatrix(
<span style="color:Blue; new <span style="color:Blue; float[][]
{
<span style="color:Blue; new <span style="color:Blue; float[] {.3f, .3f, .3f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.59f, .59f, .59f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.11f, .11f, .11f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 1, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 0, 1}
});
<span style="color:Green; //create some image attributes
ImageAttributes attributes = <span style="color:Blue; new ImageAttributes();
<span style="color:Green; //set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
<span style="color:Green; //draw the original image on the new image
<span style="color:Green; //using the grayscale color matrix
g.DrawImage(original, <span style="color:Blue; new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
<span style="color:Green; //dispose the Graphics object
g.Dispose();
<span style="color:Blue; return newBitmap;
}
<span style="color:Blue; private <span style="color:Blue; unsafe <span style="color:Blue; double GetAveragePixelValue(Bitmap bmp)
{
BitmapData bmData = <span style="color:Blue; null;
<span style="color:Blue; try
{
bmData = bmp.LockBits(<span style="color:Blue; new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
<span style="color:Blue; int stride = bmData.Stride;
IntPtr scan0 = bmData.Scan0;
<span style="color:Blue; int w = bmData.Width;
<span style="color:Blue; int h = bmData.Height;
<span style="color:Blue; long sum = 0;
<span style="color:Blue; long pixels = bmp.Width * bmp.Height;
<span style="color:Blue; byte* p = (<span style="color:Blue; byte*)scan0.ToPointer();
<span style="color:Blue; for (<span style="color:Blue; int y = 0; y < h; y++)
{
p = (<span style="color:Blue; byte*)scan0.ToPointer();
p += y * stride;
<span style="color:Blue; for (<span style="color:Blue; int x = 0; x < w; x++)
{
<span style="color:Blue; double i = (p[0] + p[1] + p[2]) / 3.0;
sum += (<span style="color:Blue; long)i;
p += 3;
}
<span style="color:Green; //no offset incrementation needed when getting
<span style="color:Green; //the pointer at the start of each row
}
bmp.UnlockBits(bmData);
<span style="color:Blue; long result = sum / pixels;
<span style="color:Blue; return (<span style="color:Blue; int)result;
}
<span style="color:Blue; catch
{
<span style="color:Blue; try
{
bmp.UnlockBits(bmData);
}
<span style="color:Blue; catch
{
}
}
<span style="color:Blue; return -1;
}
}
<span style="color:Blue; public <span style="color:Blue; class FrameCountEventArgs
{
<span style="color:Blue; public <span style="color:Blue; long FrameCount { <span style="color:Blue; get; <span style="color:Blue; set; }
}
<span style="color:Blue; public <span style="color:Blue; class ProgressEventArgs
{
<span style="color:Blue; public <span style="color:Blue; int FrameID { <span style="color:Blue; get; <span style="color:Blue; set; }
}
[/code]
<br/>
And this is Form1 the code the overall frames is shown up when i click on button1:
<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 DirectShowLib;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using System.Drawing.Imaging;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; namespace Extracting_Frames
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
OpenFileDialog fd;
WmvAdapter f;
<span style="color:Green; //outputDir
<span style="color:Blue; private <span style="color:Blue; string _outputDir = <span style="color:#A31515; @"d:Frames";<span style="color:Green; //Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "ffff");
<span style="color:Green; //moviefile
<span style="color:Blue; private <span style="color:Blue; string _videoFile = <span style="color:#A31515; @"d:Lightnings Israel 03042011 Youtube4Down.com.mp4";<span style="color:Green; //@"d:VIDEO0007.avi";//@"d:bf3 2011-10-28 00-09-17-70.avi";//@"d:VIDEO0007.3gp";//Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "DSCF0910.AVI");
FileInfo[] _fi = <span style="color:Blue; null;
<span style="color:Blue; delegate <span style="color:Blue; void del1(<span style="color:Blue; object sender, ProgressEventArgs e);
<span style="color:Blue; delegate <span style="color:Blue; void del2(<span style="color:Blue; object sender, EventArgs e);
<span style="color:Blue; delegate <span style="color:Blue; void del3(<span style="color:Blue; object sender, FrameCountEventArgs e);
<span style="color:Green; //Controls
<span style="color:Blue; private System.Windows.Forms.Button button1 = <span style="color:Blue; new System.Windows.Forms.Button();
<span style="color:Blue; private System.Windows.Forms.PictureBox pictureBox1 = <span style="color:Blue; new System.Windows.Forms.PictureBox();
<span style="color:Blue; private System.Windows.Forms.TrackBar trackBar1 = <span style="color:Blue; new System.Windows.Forms.TrackBar();
<span style="color:Blue; private System.Windows.Forms.Label label1 = <span style="color:Blue; new System.Windows.Forms.Label();
<span style="color:Blue; private System.Windows.Forms.ProgressBar progressBar1 = <span style="color:Blue; new System.Windows.Forms.ProgressBar();
<span style="color:Blue; private System.Windows.Forms.Label label2 = <span style="color:Blue; new System.Windows.Forms.Label();
<span style="color:Blue; public Form1()
{
InitializeComponent();
label2.Visible = <span style="color:Blue; false;
label3.Visible = <span style="color:Blue; false;
label4.Visible = <span style="color:Blue; false;
fd = <span style="color:Blue; new OpenFileDialog();
f = <span style="color:Blue; new WmvAdapter(_videoFile,
Path.Combine(_outputDir));
}
<span style="color:Green; /*private void Init()
{
//
// button1
//
this.button1.Location = new System.Drawing.Point(725, 382);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(204, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(406, 278);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(12, 307);
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(788, 45);
this.trackBar1.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 392);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(15, 346);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(785, 23);
this.progressBar1.TabIndex = 4;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(398, 392);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 3;
this.label2.Text = "label2";
this.ClientSize = new System.Drawing.Size(829, 430);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
}*/
<span style="color:Green; //go
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
Directory.CreateDirectory(_outputDir);
f.SaveToDisc = <span style="color:Blue; false;
f.FrameCountAvailable += <span style="color:Blue; new WmvAdapter.FrameCountEventHandler(f_FrameCountAvailable);
f.StatusChanged += <span style="color:Blue; new WmvAdapter.EventHandler(f_StatusChanged);
f.ProgressChanged += <span style="color:Blue; new WmvAdapter.ProgressEventHandler(f_ProgressChanged);
<span style="color:Blue; this.Text = <span style="color:#A31515; "Processing Frames...";
<span style="color:Green; //this.Enabled = false;
f.Start();
}
<span style="color:Blue; void f_ProgressChanged(<span style="color:Blue; object sender, ProgressEventArgs e)
{
<span style="color:Blue; if (InvokeRequired)
{
<span style="color:Blue; this.Invoke(<span style="color:Blue; new del1(f_ProgressChanged), <span style="color:Blue; new <span style="color:Blue; object[] { <span style="color:Blue; null, e });
<span style="color:Blue; return;
}
<span style="color:Blue; else
{
<span style="color:Blue; if (e.FrameID < <span style="color:Blue; this.progressBar1.Maximum)
<span style="color:Blue; this.progressBar1.Value = e.FrameID;
<span style="color:Blue; this.label4.Visible = <span style="color:Blue; true;
<span style="color:Blue; this.label2.Visible = <span style="color:Blue; true;
<span style="color:Blue; this.label2.Text = e.FrameID.ToString();
}
}
<span style="color:Blue; void f_FrameCountAvailable(<span style="color:Blue; object sender, FrameCountEventArgs e)
{
<span style="color:Blue; if (InvokeRequired)
{
<span style="color:Blue; this.Invoke(<span style="color:Blue; new del3(f_FrameCountAvailable), <span style="color:Blue; new <span style="color:Blue; object[] { <span style="color:Blue; null, e });
<span style="color:Blue; return;
}
<span style="color:Blue; else
{
<span style="color:Blue; this.progressBar1.Maximum = (<span style="color:Blue; int)e.FrameCount;
<span style="color:Blue; this.label3.Visible = <span style="color:Blue; true;
<span style="color:Blue; this.label3.Text = e.FrameCount.ToString();
<span style="color:Blue; this.progressBar1.Enabled = <span style="color:Blue; true;
}
}
<span style="color:Blue; void f_StatusChanged(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (InvokeRequired)
{
<span style="color:Blue; this.Invoke(<span style="color:Blue; new del2(f_StatusChanged), <span style="color:Blue; new <span style="color:Blue; object[] { <span style="color:Blue; null, e });
<span style="color:Blue; return;
}
<span style="color:Blue; else
{
<span style="color:Blue; this.Text = <span style="color:#A31515; "Procession Done";
<span style="color:Blue; this.Enabled = <span style="color:Blue; true;
<span style="color:Blue; this.button1.Enabled = <span style="color:Blue; false;
<span style="color:Blue; this.progressBar1.Value = 0;
<span style="color:Blue; this.progressBar1.Enabled = <span style="color:Blue; false;
<span style="color:Blue; if (Directory.Exists(_outputDir))
{
_fi = <span style="color:Blue; new DirectoryInfo(_outputDir).GetFiles();
<span style="color:Blue; this.trackBar1.Maximum = _fi.Length - 1;
<span style="color:Blue; if (<span style="color:Blue; this.trackBar1.Maximum > 0)
{
SetPicture(0);
<span style="color:Blue; this.trackBar1.Scroll += <span style="color:Blue; new EventHandler(trackBar1_Scroll);
}
}
}
}
<span style="color:Blue; void trackBar1_Scroll(<span style="color:Blue; object sender, EventArgs e)
{
SetPicture(<span style="color:Blue; this.trackBar1.Value);
}
<span style="color:Blue; private <span style="color:Blue; void SetPicture(<span style="color:Blue; int indx)
{
<span style="color:Blue; if (indx >= 0 && indx <= trackBar1.Maximum && _fi.Length > indx)
{
<span style="color:Blue; this.label1.Text = _fi[indx].FullName;
Image img = Image.FromFile(_fi[indx].FullName);
Bitmap bmp = <span style="color:Blue; new Bitmap(img);
Image imgOLd = <span style="color:Blue; this.pictureBox1.Image;
<span style="color:Blue; try
{
img = Image.FromFile(_fi[indx].FullName);
bmp = <span style="color:Blue; new Bitmap(img);
imgOLd = <span style="color:Blue; this.pictureBox1.Image;
<span style="color:Blue; this.pictureBox1.Image = bmp;
<span style="color:Blue; if (imgOLd != <span style="color:Blue; null)
imgOLd.Dispose();
}
<span style="color:Blue; catch
{
<span style="color:Blue; if (img != <span style="color:Blue; null)
img.Dispose();
<span style="color:Blue; if (bmp != <span style="color:Blue; null)
bmp.Dispose();
<span style="color:Blue; if (imgOLd != <span style="color:Blue; null)
imgOLd.Dispose();
}
img.Dispose();
}
}
}
}
[/code]
<br/>
The file is .MP4 and its 7.02MB <hr class="sig danieli
View the full article
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Drawing.Imaging;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using DirectShowLib;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using Extracting_Frames;
<span style="color:Blue; internal <span style="color:Blue; class WmvAdapter : ISampleGrabberCB, IDisposable
{
<span style="color:Blue; #region Fields
<span style="color:Blue; public List<<span style="color:Blue; double> results { <span style="color:Blue; get; <span style="color:Blue; set; }
StreamWriter w = <span style="color:Blue; new StreamWriter(<span style="color:#A31515; @"d:results.txt");
<span style="color:Blue; private IFilterGraph2 _filterGraph;
<span style="color:Blue; private IMediaControl _mediaCtrl;
<span style="color:Blue; private IMediaEvent _mediaEvent;
<span style="color:Blue; private <span style="color:Blue; int _width;
<span style="color:Blue; private <span style="color:Blue; int _height;
<span style="color:Blue; private <span style="color:Blue; readonly <span style="color:Blue; string _outFolder;
<span style="color:Blue; private <span style="color:Blue; int _frameId;
<span style="color:Green; //better use a custom EventHandler that passes the results of the action to the subscriber.
<span style="color:Blue; public <span style="color:Blue; delegate <span style="color:Blue; void EventHandler(<span style="color:Blue; object sender, EventArgs e);
<span style="color:Blue; public <span style="color:Blue; event EventHandler StatusChanged;
<span style="color:Blue; public <span style="color:Blue; delegate <span style="color:Blue; void FrameCountEventHandler(<span style="color:Blue; object sender, FrameCountEventArgs e);
<span style="color:Blue; public <span style="color:Blue; event FrameCountEventHandler FrameCountAvailable;
<span style="color:Blue; public <span style="color:Blue; delegate <span style="color:Blue; void ProgressEventHandler(<span style="color:Blue; object sender, ProgressEventArgs e);
<span style="color:Blue; public <span style="color:Blue; event ProgressEventHandler ProgressChanged;
<span style="color:Blue; private IMediaSeeking _mSeek;
<span style="color:Blue; private <span style="color:Blue; long _duration = 0;
<span style="color:Blue; private <span style="color:Blue; long _avgFrameTime = 0;
<span style="color:Blue; #endregion
<span style="color:Blue; #region Constructors and Destructors
<span style="color:Blue; public WmvAdapter(<span style="color:Blue; string file, <span style="color:Blue; string outFolder)
{
_outFolder = outFolder;
<span style="color:Blue; try
{
SetupGraph(file);
}
<span style="color:Blue; catch
{
Dispose();
<span style="color:Blue; throw;
}
}
~WmvAdapter()
{
CloseInterfaces();
}
<span style="color:Blue; #endregion
<span style="color:Blue; public <span style="color:Blue; void Dispose()
{
CloseInterfaces();
}
<span style="color:Blue; public <span style="color:Blue; void Start()
{
<span style="color:Green; //Edit: get the duration
_mSeek.GetDuration(<span style="color:Blue; out _duration);
EstimateFrameCount();
<span style="color:Blue; int hr = _mediaCtrl.Run();
WaitUntilDone();
DsError.ThrowExceptionForHR(hr);
}
<span style="color:Blue; public <span style="color:Blue; void WaitUntilDone()
{
<span style="color:Blue; int hr;
<span style="color:Blue; const <span style="color:Blue; int eAbort = <span style="color:Blue; unchecked((<span style="color:Blue; int)0x80004004);
<span style="color:Blue; do
{
System.Windows.Forms.Application.DoEvents();
EventCode evCode;
hr = _mediaEvent.WaitForCompletion(100, <span style="color:Blue; out evCode);
} <span style="color:Blue; while (hr == eAbort);
w.Close();
<span style="color:Green; //here you can close the streamwriter, if its on class level
<span style="color:Green; //but dont forget to re-instanciate it when doing the job more than once from
OnStatusChanged();
DsError.ThrowExceptionForHR(hr);
}
<span style="color:Green; /*int hr;
const int eAbort = unchecked((int)0x80004004);
do
{
System.Windows.Forms.Application.DoEvents();
EventCode evCode;
hr = _mediaEvent.WaitForCompletion(100, out evCode);
} while (hr == eAbort);
OnStatusChanged();
DsError.ThrowExceptionForHR(hr);*/
<span style="color:Green; //Edit: added events
<span style="color:Blue; protected <span style="color:Blue; virtual <span style="color:Blue; void OnStatusChanged()
{
<span style="color:Blue; if (StatusChanged != <span style="color:Blue; null)
StatusChanged(<span style="color:Blue; this, <span style="color:Blue; new EventArgs());
}
<span style="color:Blue; protected <span style="color:Blue; virtual <span style="color:Blue; void OnFrameCountAvailable(<span style="color:Blue; long frameCount)
{
<span style="color:Blue; if (FrameCountAvailable != <span style="color:Blue; null)
FrameCountAvailable(<span style="color:Blue; this, <span style="color:Blue; new FrameCountEventArgs() { FrameCount = frameCount });
}
<span style="color:Blue; protected <span style="color:Blue; virtual <span style="color:Blue; void OnProgressChanged(<span style="color:Blue; int frameID)
{
<span style="color:Blue; if (ProgressChanged != <span style="color:Blue; null)
ProgressChanged(<span style="color:Blue; this, <span style="color:Blue; new ProgressEventArgs() { FrameID = frameID });
}
<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; build the capture graph for grabber. </summary>
<span style="color:Blue; private <span style="color:Blue; void SetupGraph(<span style="color:Blue; string file)
{
ISampleGrabber sampGrabber = <span style="color:Blue; null;
IBaseFilter capFilter = <span style="color:Blue; null;
IBaseFilter nullrenderer = <span style="color:Blue; null;
_filterGraph = (IFilterGraph2)<span style="color:Blue; new FilterGraph();
_mediaCtrl = (IMediaControl)_filterGraph;
_mediaEvent = (IMediaEvent)_filterGraph;
_mSeek = (IMediaSeeking)_filterGraph;
<span style="color:Blue; var mediaFilt = (IMediaFilter)_filterGraph;
<span style="color:Blue; try
{
<span style="color:Green; // Add the video source
<span style="color:Blue; int hr = _filterGraph.AddSourceFilter(file, <span style="color:#A31515; "Ds.NET FileFilter", <span style="color:Blue; out capFilter);
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // Get the SampleGrabber interface
sampGrabber = <span style="color:Blue; new SampleGrabber() <span style="color:Blue; as ISampleGrabber;
<span style="color:Blue; var baseGrabFlt = sampGrabber <span style="color:Blue; as IBaseFilter;
ConfigureSampleGrabber(sampGrabber);
<span style="color:Green; // Add the frame grabber to the graph
hr = _filterGraph.AddFilter(baseGrabFlt, <span style="color:#A31515; "Ds.NET Grabber");
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // ---------------------------------
<span style="color:Green; // Connect the file filter to the sample grabber
<span style="color:Green; // Hopefully this will be the video pin, we could check by reading its mediatype
IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0);
<span style="color:Green; // Get the input pin from the sample grabber
IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);
hr = _filterGraph.Connect(iPinOut, iPinIn);
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // Add the null renderer to the graph
nullrenderer = <span style="color:Blue; new NullRenderer() <span style="color:Blue; as IBaseFilter;
hr = _filterGraph.AddFilter(nullrenderer, <span style="color:#A31515; "Null renderer");
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // ---------------------------------
<span style="color:Green; // Connect the sample grabber to the null renderer
iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);
iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0);
hr = _filterGraph.Connect(iPinOut, iPinIn);
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // Turn off the clock. This causes the frames to be sent
<span style="color:Green; // thru the graph as fast as possible
hr = mediaFilt.SetSyncSource(<span style="color:Blue; null);
DsError.ThrowExceptionForHR(hr);
<span style="color:Green; // Read and cache the image sizes
SaveSizeInfo(sampGrabber);
}
<span style="color:Blue; finally
{
<span style="color:Blue; if (capFilter != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(capFilter);
}
<span style="color:Blue; if (sampGrabber != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(sampGrabber);
}
<span style="color:Blue; if (nullrenderer != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(nullrenderer);
}
GC.Collect();
}
}
<span style="color:Blue; private <span style="color:Blue; void EstimateFrameCount()
{
<span style="color:Green; //1sec / averageFrameTime
<span style="color:Blue; long fr = 10000000 / _avgFrameTime;
<span style="color:Blue; double frameCount = fr * (_duration / 10000000.0);
OnFrameCountAvailable((<span style="color:Blue; long)frameCount);
}
<span style="color:Blue; private <span style="color:Blue; void SaveSizeInfo(ISampleGrabber sampGrabber)
{
<span style="color:Green; // Get the media type from the SampleGrabber
<span style="color:Blue; var media = <span style="color:Blue; new AMMediaType();
<span style="color:Blue; int hr = sampGrabber.GetConnectedMediaType(media);
DsError.ThrowExceptionForHR(hr);
<span style="color:Blue; if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
{
<span style="color:Blue; throw <span style="color:Blue; new NotSupportedException(<span style="color:#A31515; "Unknown Grabber Media Format");
}
<span style="color:Green; // Grab the size info
<span style="color:Blue; var videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, <span style="color:Blue; typeof(VideoInfoHeader));
_width = videoInfoHeader.BmiHeader.Width;
_height = videoInfoHeader.BmiHeader.Height;
<span style="color:Green; //Edit: get framerate
_avgFrameTime = videoInfoHeader.AvgTimePerFrame;
DsUtils.FreeAMMediaType(media);
GC.Collect();
}
<span style="color:Blue; private <span style="color:Blue; void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
{
<span style="color:Blue; var media = <span style="color:Blue; new AMMediaType
{
majorType = MediaType.Video,
subType = MediaSubType.RGB24,
formatType = FormatType.VideoInfo
};
<span style="color:Blue; int hr = sampGrabber.SetMediaType(media);
DsError.ThrowExceptionForHR(hr);
DsUtils.FreeAMMediaType(media);
GC.Collect();
hr = sampGrabber.SetCallback(<span style="color:Blue; this, 1);
DsError.ThrowExceptionForHR(hr);
}
<span style="color:Blue; private <span style="color:Blue; void CloseInterfaces()
{
<span style="color:Blue; try
{
<span style="color:Blue; if (_mediaCtrl != <span style="color:Blue; null)
{
_mediaCtrl.Stop();
_mediaCtrl = <span style="color:Blue; null;
}
}
<span style="color:Blue; catch (Exception ex)
{
Debug.WriteLine(ex);
}
<span style="color:Blue; if (_filterGraph != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(_filterGraph);
_filterGraph = <span style="color:Blue; null;
}
GC.Collect();
}
<span style="color:Blue; int ISampleGrabberCB.SampleCB(<span style="color:Blue; double sampleTime, IMediaSample pSample)
{
Marshal.ReleaseComObject(pSample);
<span style="color:Blue; return 0;
}
Bitmap bitmap;
<span style="color:Green; //add a boolean property to indicate the save-mode
<span style="color:Blue; public <span style="color:Blue; bool SaveToDisc { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Green; //the list for the bitmaps
<span style="color:Blue; public List<Bitmap> Images { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; int ISampleGrabberCB.BufferCB(<span style="color:Blue; double sampleTime, IntPtr pBuffer, <span style="color:Blue; int bufferLen)
{
<span style="color:Blue; using (bitmap = <span style="color:Blue; new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
<span style="color:Green; //Bitmap bitmaps = PixelsGray(bitmap);
<span style="color:Blue; double average = GetAveragePixelValue(bitmap);
<span style="color:Green; //Bitmap bitmaps = converttoBlackAndWhite(bitmap);
<span style="color:Blue; if (SaveToDisc)
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString(<span style="color:#A31515; "D6") + <span style="color:#A31515; ".bmp"));
_frameId++;
}
<span style="color:Blue; else
{
<span style="color:Blue; if (average > 10)
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString(<span style="color:#A31515; "D6") + <span style="color:#A31515; ".bmp"));
}
w.WriteLine(average);
<span style="color:Blue; if (results == <span style="color:Blue; null)
results = <span style="color:Blue; new List<<span style="color:Blue; double>();
results.Add(average);
<span style="color:Green; //w.WriteLine(" " + _frameId);
_frameId++;
<span style="color:Green; /*if (_frameId == 9227 || _frameId == 9228)
{
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));
}*/
<span style="color:Blue; if (_frameId % 100 == 0)
OnProgressChanged(_frameId);
<span style="color:Green; /*if (_frameId >= 1800)
{
bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));
}*/
<span style="color:Green; //let only report each 100 frames for performance
}
}
<span style="color:Blue; return 0;
}
<span style="color:Blue; private Bitmap PixelsGray(Bitmap originals)
{
<span style="color:Blue; double numberpixels = 0;
<span style="color:Blue; double big_Sum = 0;
<span style="color:Blue; double sum;
Bitmap newGray;
newGray = <span style="color:Blue; new Bitmap(originals.Width, originals.Height);
<span style="color:Blue; int x, y;
<span style="color:Blue; for (x = 0; x < newGray.Width; x++)
{
<span style="color:Blue; for (y = 0; y < originals.Height; y++)
{
Color originalColor = originals.GetPixel(x, y);
<span style="color:Blue; double WhiteBlack = (<span style="color:Blue; int)((originalColor.R) + (originalColor.G)
+ (originalColor.B));
sum = WhiteBlack / 3;
big_Sum = big_Sum + sum;
<span style="color:Green; //newColor = Color.FromArgb(255,(int)sum,(int)sum,(int)sum);
<span style="color:Green; //newGray.SetPixel(x, y, newColor);
numberpixels = originals.Width * originals.Height;
}
}
<span style="color:Blue; double result = big_Sum / numberpixels;
<span style="color:Blue; if (results == <span style="color:Blue; null)
results = <span style="color:Blue; new List<<span style="color:Blue; double>();
results.Add(result);
<span style="color:Blue; return newGray;
}
<span style="color:Blue; private Bitmap converttoBlackAndWhite(Bitmap original)
{
Bitmap newBitmap = <span style="color:Blue; new Bitmap(original.Width, original.Height);
<span style="color:Green; //get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
<span style="color:Green; //create the grayscale ColorMatrix
ColorMatrix colorMatrix = <span style="color:Blue; new ColorMatrix(
<span style="color:Blue; new <span style="color:Blue; float[][]
{
<span style="color:Blue; new <span style="color:Blue; float[] {.3f, .3f, .3f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.59f, .59f, .59f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.11f, .11f, .11f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 1, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 0, 1}
});
<span style="color:Green; //create some image attributes
ImageAttributes attributes = <span style="color:Blue; new ImageAttributes();
<span style="color:Green; //set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
<span style="color:Green; //draw the original image on the new image
<span style="color:Green; //using the grayscale color matrix
g.DrawImage(original, <span style="color:Blue; new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
<span style="color:Green; //dispose the Graphics object
g.Dispose();
<span style="color:Blue; return newBitmap;
}
<span style="color:Blue; private <span style="color:Blue; unsafe <span style="color:Blue; double GetAveragePixelValue(Bitmap bmp)
{
BitmapData bmData = <span style="color:Blue; null;
<span style="color:Blue; try
{
bmData = bmp.LockBits(<span style="color:Blue; new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
<span style="color:Blue; int stride = bmData.Stride;
IntPtr scan0 = bmData.Scan0;
<span style="color:Blue; int w = bmData.Width;
<span style="color:Blue; int h = bmData.Height;
<span style="color:Blue; long sum = 0;
<span style="color:Blue; long pixels = bmp.Width * bmp.Height;
<span style="color:Blue; byte* p = (<span style="color:Blue; byte*)scan0.ToPointer();
<span style="color:Blue; for (<span style="color:Blue; int y = 0; y < h; y++)
{
p = (<span style="color:Blue; byte*)scan0.ToPointer();
p += y * stride;
<span style="color:Blue; for (<span style="color:Blue; int x = 0; x < w; x++)
{
<span style="color:Blue; double i = (p[0] + p[1] + p[2]) / 3.0;
sum += (<span style="color:Blue; long)i;
p += 3;
}
<span style="color:Green; //no offset incrementation needed when getting
<span style="color:Green; //the pointer at the start of each row
}
bmp.UnlockBits(bmData);
<span style="color:Blue; long result = sum / pixels;
<span style="color:Blue; return (<span style="color:Blue; int)result;
}
<span style="color:Blue; catch
{
<span style="color:Blue; try
{
bmp.UnlockBits(bmData);
}
<span style="color:Blue; catch
{
}
}
<span style="color:Blue; return -1;
}
}
<span style="color:Blue; public <span style="color:Blue; class FrameCountEventArgs
{
<span style="color:Blue; public <span style="color:Blue; long FrameCount { <span style="color:Blue; get; <span style="color:Blue; set; }
}
<span style="color:Blue; public <span style="color:Blue; class ProgressEventArgs
{
<span style="color:Blue; public <span style="color:Blue; int FrameID { <span style="color:Blue; get; <span style="color:Blue; set; }
}
[/code]
<br/>
And this is Form1 the code the overall frames is shown up when i click on button1:
<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 DirectShowLib;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using System.Drawing.Imaging;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; namespace Extracting_Frames
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
OpenFileDialog fd;
WmvAdapter f;
<span style="color:Green; //outputDir
<span style="color:Blue; private <span style="color:Blue; string _outputDir = <span style="color:#A31515; @"d:Frames";<span style="color:Green; //Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "ffff");
<span style="color:Green; //moviefile
<span style="color:Blue; private <span style="color:Blue; string _videoFile = <span style="color:#A31515; @"d:Lightnings Israel 03042011 Youtube4Down.com.mp4";<span style="color:Green; //@"d:VIDEO0007.avi";//@"d:bf3 2011-10-28 00-09-17-70.avi";//@"d:VIDEO0007.3gp";//Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "DSCF0910.AVI");
FileInfo[] _fi = <span style="color:Blue; null;
<span style="color:Blue; delegate <span style="color:Blue; void del1(<span style="color:Blue; object sender, ProgressEventArgs e);
<span style="color:Blue; delegate <span style="color:Blue; void del2(<span style="color:Blue; object sender, EventArgs e);
<span style="color:Blue; delegate <span style="color:Blue; void del3(<span style="color:Blue; object sender, FrameCountEventArgs e);
<span style="color:Green; //Controls
<span style="color:Blue; private System.Windows.Forms.Button button1 = <span style="color:Blue; new System.Windows.Forms.Button();
<span style="color:Blue; private System.Windows.Forms.PictureBox pictureBox1 = <span style="color:Blue; new System.Windows.Forms.PictureBox();
<span style="color:Blue; private System.Windows.Forms.TrackBar trackBar1 = <span style="color:Blue; new System.Windows.Forms.TrackBar();
<span style="color:Blue; private System.Windows.Forms.Label label1 = <span style="color:Blue; new System.Windows.Forms.Label();
<span style="color:Blue; private System.Windows.Forms.ProgressBar progressBar1 = <span style="color:Blue; new System.Windows.Forms.ProgressBar();
<span style="color:Blue; private System.Windows.Forms.Label label2 = <span style="color:Blue; new System.Windows.Forms.Label();
<span style="color:Blue; public Form1()
{
InitializeComponent();
label2.Visible = <span style="color:Blue; false;
label3.Visible = <span style="color:Blue; false;
label4.Visible = <span style="color:Blue; false;
fd = <span style="color:Blue; new OpenFileDialog();
f = <span style="color:Blue; new WmvAdapter(_videoFile,
Path.Combine(_outputDir));
}
<span style="color:Green; /*private void Init()
{
//
// button1
//
this.button1.Location = new System.Drawing.Point(725, 382);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(204, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(406, 278);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(12, 307);
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(788, 45);
this.trackBar1.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 392);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 3;
this.label1.Text = "label1";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(15, 346);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(785, 23);
this.progressBar1.TabIndex = 4;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(398, 392);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 3;
this.label2.Text = "label2";
this.ClientSize = new System.Drawing.Size(829, 430);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
}*/
<span style="color:Green; //go
<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
Directory.CreateDirectory(_outputDir);
f.SaveToDisc = <span style="color:Blue; false;
f.FrameCountAvailable += <span style="color:Blue; new WmvAdapter.FrameCountEventHandler(f_FrameCountAvailable);
f.StatusChanged += <span style="color:Blue; new WmvAdapter.EventHandler(f_StatusChanged);
f.ProgressChanged += <span style="color:Blue; new WmvAdapter.ProgressEventHandler(f_ProgressChanged);
<span style="color:Blue; this.Text = <span style="color:#A31515; "Processing Frames...";
<span style="color:Green; //this.Enabled = false;
f.Start();
}
<span style="color:Blue; void f_ProgressChanged(<span style="color:Blue; object sender, ProgressEventArgs e)
{
<span style="color:Blue; if (InvokeRequired)
{
<span style="color:Blue; this.Invoke(<span style="color:Blue; new del1(f_ProgressChanged), <span style="color:Blue; new <span style="color:Blue; object[] { <span style="color:Blue; null, e });
<span style="color:Blue; return;
}
<span style="color:Blue; else
{
<span style="color:Blue; if (e.FrameID < <span style="color:Blue; this.progressBar1.Maximum)
<span style="color:Blue; this.progressBar1.Value = e.FrameID;
<span style="color:Blue; this.label4.Visible = <span style="color:Blue; true;
<span style="color:Blue; this.label2.Visible = <span style="color:Blue; true;
<span style="color:Blue; this.label2.Text = e.FrameID.ToString();
}
}
<span style="color:Blue; void f_FrameCountAvailable(<span style="color:Blue; object sender, FrameCountEventArgs e)
{
<span style="color:Blue; if (InvokeRequired)
{
<span style="color:Blue; this.Invoke(<span style="color:Blue; new del3(f_FrameCountAvailable), <span style="color:Blue; new <span style="color:Blue; object[] { <span style="color:Blue; null, e });
<span style="color:Blue; return;
}
<span style="color:Blue; else
{
<span style="color:Blue; this.progressBar1.Maximum = (<span style="color:Blue; int)e.FrameCount;
<span style="color:Blue; this.label3.Visible = <span style="color:Blue; true;
<span style="color:Blue; this.label3.Text = e.FrameCount.ToString();
<span style="color:Blue; this.progressBar1.Enabled = <span style="color:Blue; true;
}
}
<span style="color:Blue; void f_StatusChanged(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (InvokeRequired)
{
<span style="color:Blue; this.Invoke(<span style="color:Blue; new del2(f_StatusChanged), <span style="color:Blue; new <span style="color:Blue; object[] { <span style="color:Blue; null, e });
<span style="color:Blue; return;
}
<span style="color:Blue; else
{
<span style="color:Blue; this.Text = <span style="color:#A31515; "Procession Done";
<span style="color:Blue; this.Enabled = <span style="color:Blue; true;
<span style="color:Blue; this.button1.Enabled = <span style="color:Blue; false;
<span style="color:Blue; this.progressBar1.Value = 0;
<span style="color:Blue; this.progressBar1.Enabled = <span style="color:Blue; false;
<span style="color:Blue; if (Directory.Exists(_outputDir))
{
_fi = <span style="color:Blue; new DirectoryInfo(_outputDir).GetFiles();
<span style="color:Blue; this.trackBar1.Maximum = _fi.Length - 1;
<span style="color:Blue; if (<span style="color:Blue; this.trackBar1.Maximum > 0)
{
SetPicture(0);
<span style="color:Blue; this.trackBar1.Scroll += <span style="color:Blue; new EventHandler(trackBar1_Scroll);
}
}
}
}
<span style="color:Blue; void trackBar1_Scroll(<span style="color:Blue; object sender, EventArgs e)
{
SetPicture(<span style="color:Blue; this.trackBar1.Value);
}
<span style="color:Blue; private <span style="color:Blue; void SetPicture(<span style="color:Blue; int indx)
{
<span style="color:Blue; if (indx >= 0 && indx <= trackBar1.Maximum && _fi.Length > indx)
{
<span style="color:Blue; this.label1.Text = _fi[indx].FullName;
Image img = Image.FromFile(_fi[indx].FullName);
Bitmap bmp = <span style="color:Blue; new Bitmap(img);
Image imgOLd = <span style="color:Blue; this.pictureBox1.Image;
<span style="color:Blue; try
{
img = Image.FromFile(_fi[indx].FullName);
bmp = <span style="color:Blue; new Bitmap(img);
imgOLd = <span style="color:Blue; this.pictureBox1.Image;
<span style="color:Blue; this.pictureBox1.Image = bmp;
<span style="color:Blue; if (imgOLd != <span style="color:Blue; null)
imgOLd.Dispose();
}
<span style="color:Blue; catch
{
<span style="color:Blue; if (img != <span style="color:Blue; null)
img.Dispose();
<span style="color:Blue; if (bmp != <span style="color:Blue; null)
bmp.Dispose();
<span style="color:Blue; if (imgOLd != <span style="color:Blue; null)
imgOLd.Dispose();
}
img.Dispose();
}
}
}
}
[/code]
<br/>
The file is .MP4 and its 7.02MB <hr class="sig danieli
View the full article