Direct show doesn't change webcam resolution

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<div style="font:normal normal normal 13px/normal Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif; color:#222222; padding:8px; margin:0px
Hi,<br/>
<br/>
I have two Microsoft LifeCam Studio webcams that need to have a still image taken when required. I have managed to put this code together that works for all except changing the resolution. The weird thing that I dont understand is that the equivalent code
for the form works in C# but not in vb.<br/>
<br/>
The code is built up of 3 parts, a form with a picturebox and a button, the class written in C# that uses directshow and another class that acts between them (the form is just temporary until it works).<br/>
<br/>
My form written in vb (it does need the dispose event to close the camera adding but Ive left this out)<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports SnapShot

<span style="color:Blue; Public <span style="color:Blue; Class Form1


<span style="color:Blue; Private cam <span style="color:Blue; As Class1


<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
cam.takeImage(<span style="color:#A31515; "C:UsersProgrammingDesktopme.jpg")
<span style="color:Blue; End <span style="color:Blue; Sub


<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; Me.Load
cam = <span style="color:Blue; New Class1(0, PictureBox1)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
<br/>
<br/>
The "middle" class, based upon the directshow example.<br/>
<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.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Drawing.Imaging;


<span style="color:Blue; namespace SnapShot
{
<span style="color:Blue; public <span style="color:Blue; class Class1
{
<span style="color:Blue; private Capture cam;
IntPtr m_ip = IntPtr.Zero;


<span style="color:Blue; public Class1(<span style="color:Blue; int VIDEODEVICE, Control pictureBox2)
{
cam = <span style="color:Blue; new Capture(VIDEODEVICE, 1024, 768, 24, pictureBox2);
}


<span style="color:Blue; public <span style="color:Blue; void dispose()
{
<span style="color:Blue; if (m_ip != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(m_ip);
m_ip = IntPtr.Zero;
}
}


<span style="color:Blue; public <span style="color:Blue; void takeImage(String name)
{
<span style="color:Green; // Release any previous buffer
<span style="color:Blue; if (m_ip != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(m_ip);
m_ip = IntPtr.Zero;
}


<span style="color:Green; // capture image
m_ip = cam.Click();
Bitmap b = <span style="color:Blue; new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);


<span style="color:Green; // If the image is upsidedown
b.RotateFlip(RotateFlipType.RotateNoneFlipY);
b.Save(name, ImageFormat.Jpeg);
}
}
}
[/code]
<br/>
<br/>
<br/>
<br/>
The class that uses direct show, from the samples:
<div style="font:normal normal normal 13px/normal Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif; color:#222222; padding:8px; margin:0px
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; /****************************************************************************While the underlying libraries are covered by LGPL, this sample is released
as public domain. It is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
*****************************************************************************/


<span style="color:Blue; using System;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Drawing.Imaging;
<span style="color:Blue; using System.Collections;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using System.Threading;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; using System.Windows.Forms;


<span style="color:Blue; using DirectShowLib;




<span style="color:Blue; namespace SnapShot
{
<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; Summary description for MainForm. </summary>
<span style="color:Blue; internal <span style="color:Blue; class Capture : ISampleGrabberCB, IDisposable
{
<span style="color:Blue; #region Member variables


<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; graph builder interface. </summary>
<span style="color:Blue; private IFilterGraph2 m_FilterGraph = <span style="color:Blue; null;


<span style="color:Green; // Used to snap picture on Still pin
<span style="color:Blue; private IAMVideoControl m_VidControl = <span style="color:Blue; null;
<span style="color:Blue; private IPin m_pinStill = <span style="color:Blue; null;


<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; so we can wait for the async job to finish </summary>
<span style="color:Blue; private ManualResetEvent m_PictureReady = <span style="color:Blue; null;


<span style="color:Blue; private <span style="color:Blue; bool m_WantOne = <span style="color:Blue; false;


<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; Dimensions of the image, calculated once in constructor for perf. </summary>
<span style="color:Blue; private <span style="color:Blue; int m_videoWidth;
<span style="color:Blue; private <span style="color:Blue; int m_videoHeight;
<span style="color:Blue; private <span style="color:Blue; int m_stride;


<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; buffer for bitmap data. Always release by caller</summary>
<span style="color:Blue; private IntPtr m_ipBuffer = IntPtr.Zero;


<span style="color:Blue; #if DEBUG
<span style="color:Green; // Allow you to "Connect to remote graph" from GraphEdit
DsROTEntry m_rot = <span style="color:Blue; null;
<span style="color:Blue; #endif
<span style="color:Blue; #endregion


<span style="color:Blue; #region APIs
[DllImport(<span style="color:#A31515; "Kernel32.dll", EntryPoint=<span style="color:#A31515; "RtlMoveMemory")]
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; void CopyMemory(IntPtr Destination, IntPtr Source, [MarshalAs(UnmanagedType.U4)] <span style="color:Blue; int Length);
<span style="color:Blue; #endregion


<span style="color:Green; // Zero based device index and device params and output window
<span style="color:Blue; public Capture(<span style="color:Blue; int iDeviceNum, <span style="color:Blue; int iWidth, <span style="color:Blue; int iHeight, <span style="color:Blue; short iBPP, Control hControl)
{
DsDevice [] capDevices;


<span style="color:Green; // Get the collection of video devices
capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);


<span style="color:Blue; if (iDeviceNum + 1 > capDevices.Length)
{
<span style="color:Blue; throw <span style="color:Blue; new Exception(<span style="color:#A31515; "No video capture devices found at that index!");
}


<span style="color:Blue; try
{
<span style="color:Green; // Set up the capture graph
SetupGraph( capDevices[iDeviceNum], iWidth, iHeight, iBPP, hControl);


<span style="color:Green; // tell the callback to ignore new images
m_PictureReady = <span style="color:Blue; new ManualResetEvent(<span style="color:Blue; false);
}
<span style="color:Blue; catch
{
Dispose();
<span style="color:Blue; throw;
}
}


<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; release everything. </summary>
<span style="color:Blue; public <span style="color:Blue; void Dispose()
{
<span style="color:Blue; #if DEBUG
<span style="color:Blue; if (m_rot != <span style="color:Blue; null)
{
m_rot.Dispose();
}
<span style="color:Blue; #endif
CloseInterfaces();
<span style="color:Blue; if (m_PictureReady != <span style="color:Blue; null)
{
m_PictureReady.Close();
}
}
<span style="color:Green; // Destructor
~Capture()
{
Dispose();
}


<span style="color:Gray; /// <span style="color:Gray; <summary>
<span style="color:Gray; ///<span style="color:Green; Get the image from the Still pin. The returned image can turned into a bitmap with
<span style="color:Gray; ///<span style="color:Green; Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);
<span style="color:Gray; ///<span style="color:Green; If the image is upside down, you can fix it with
<span style="color:Gray; ///<span style="color:Green; b.RotateFlip(RotateFlipType.RotateNoneFlipY);
<span style="color:Gray; /// <span style="color:Gray; </summary>
<span style="color:Gray; /// <span style="color:Gray; <returns><span style="color:Green; Returned pointer to be freed by caller with Marshal.FreeCoTaskMem</returns>
<span style="color:Blue; public IntPtr Click()
{
<span style="color:Blue; int hr;


<span style="color:Green; // get ready to wait for new image
m_PictureReady.Reset();
m_ipBuffer = Marshal.AllocCoTaskMem(Math.Abs(m_stride) * m_videoHeight);


<span style="color:Blue; try
{
m_WantOne = <span style="color:Blue; true;


<span style="color:Green; // If we are using a still pin, ask for a picture
<span style="color:Blue; if (m_VidControl != <span style="color:Blue; null)
{
<span style="color:Green; // Tell the camera to send an image
hr = m_VidControl.SetMode(m_pinStill, VideoControlFlags.Trigger);
DsError.ThrowExceptionForHR( hr );
}


<span style="color:Green; // Start waiting
<span style="color:Blue; if ( ! m_PictureReady.WaitOne(9000, <span style="color:Blue; false) )
{
<span style="color:Blue; throw <span style="color:Blue; new Exception(<span style="color:#A31515; "Timeout waiting to get picture");
}
}
<span style="color:Blue; catch
{
Marshal.FreeCoTaskMem(m_ipBuffer);
m_ipBuffer = IntPtr.Zero;
<span style="color:Blue; throw;
}

<span style="color:Green; // Got one
<span style="color:Blue; return m_ipBuffer;
}


<span style="color:Blue; public <span style="color:Blue; int Width
{
<span style="color:Blue; get
{
<span style="color:Blue; return m_videoWidth;
}
}
<span style="color:Blue; public <span style="color:Blue; int Height
{
<span style="color:Blue; get
{
<span style="color:Blue; return m_videoHeight;
}
}
<span style="color:Blue; public <span style="color:Blue; int Stride
{
<span style="color:Blue; get
{
<span style="color:Blue; return m_stride;
}
}




<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(DsDevice dev, <span style="color:Blue; int iWidth, <span style="color:Blue; int iHeight, <span style="color:Blue; short iBPP, Control hControl)
{
<span style="color:Blue; int hr;


ISampleGrabber sampGrabber = <span style="color:Blue; null;
IBaseFilter capFilter = <span style="color:Blue; null;
IPin pCaptureOut = <span style="color:Blue; null;
IPin pSampleIn = <span style="color:Blue; null;
IPin pRenderIn = <span style="color:Blue; null;


<span style="color:Green; // Get the graphbuilder object
m_FilterGraph = <span style="color:Blue; new FilterGraph() <span style="color:Blue; as IFilterGraph2;


<span style="color:Blue; try
{
<span style="color:Blue; #if DEBUG
m_rot = <span style="color:Blue; new DsROTEntry(m_FilterGraph);
<span style="color:Blue; #endif
<span style="color:Green; // add the video input device
hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, <span style="color:Blue; null, dev.Name, <span style="color:Blue; out capFilter);
DsError.ThrowExceptionForHR( hr );


<span style="color:Green; // Find the still pin
m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Still, 0);


<span style="color:Green; // Didnt find one. Is there a preview pin?
<span style="color:Blue; if (m_pinStill == <span style="color:Blue; null)
{
m_pinStill = DsFindPin.ByCategory(capFilter, PinCategory.Preview, 0);
}


<span style="color:Green; // Still havent found one. Need to put a splitter in so we have
<span style="color:Green; // one stream to capture the bitmap from, and one to display. Ok, we
<span style="color:Green; // dont *have* to do it that way, but we are going to anyway.
<span style="color:Blue; if (m_pinStill == <span style="color:Blue; null)
{
IPin pRaw = <span style="color:Blue; null;
IPin pSmart = <span style="color:Blue; null;


<span style="color:Green; // There is no still pin
m_VidControl = <span style="color:Blue; null;


<span style="color:Green; // Add a splitter
IBaseFilter iSmartTee = (IBaseFilter)<span style="color:Blue; new SmartTee();


<span style="color:Blue; try
{
hr = m_FilterGraph.AddFilter(iSmartTee, <span style="color:#A31515; "SmartTee");
DsError.ThrowExceptionForHR( hr );


<span style="color:Green; // Find the find the capture pin from the video device and the
<span style="color:Green; // input pin for the splitter, and connnect them
pRaw = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);
pSmart = DsFindPin.ByDirection(iSmartTee, PinDirection.Input, 0);


hr = m_FilterGraph.Connect(pRaw, pSmart);
DsError.ThrowExceptionForHR( hr );


<span style="color:Green; // Now set the capture and still pins (from the splitter)
m_pinStill = DsFindPin.ByName(iSmartTee, <span style="color:#A31515; "Preview");
pCaptureOut = DsFindPin.ByName(iSmartTee, <span style="color:#A31515; "Capture");


<span style="color:Green; // If any of the default config items are set, perform the config
<span style="color:Green; // on the actual video device (rather than the splitter)
<span style="color:Blue; if (iHeight + iWidth + iBPP > 0)
{
SetConfigParms(pRaw, iWidth, iHeight, iBPP);
}
}
<span style="color:Blue; finally
{
<span style="color:Blue; if (pRaw != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(pRaw);
}
<span style="color:Blue; if (pRaw != pSmart)
{
Marshal.ReleaseComObject(pSmart);
}
<span style="color:Blue; if (pRaw != iSmartTee)
{
Marshal.ReleaseComObject(iSmartTee);
}
}
}
<span style="color:Blue; else
{
<span style="color:Green; // Get a control pointer (used in Click())
m_VidControl = capFilter <span style="color:Blue; as IAMVideoControl;


pCaptureOut = DsFindPin.ByCategory(capFilter, PinCategory.Capture, 0);


<span style="color:Green; // If any of the default config items are set
<span style="color:Blue; if (iHeight + iWidth + iBPP > 0)
{
SetConfigParms(m_pinStill, iWidth, iHeight, iBPP);
}
}


<span style="color:Green; // Get the SampleGrabber interface
sampGrabber = <span style="color:Blue; new SampleGrabber() <span style="color:Blue; as ISampleGrabber;


<span style="color:Green; // Configure the sample grabber
IBaseFilter baseGrabFlt = sampGrabber <span style="color:Blue; as IBaseFilter;
ConfigureSampleGrabber(sampGrabber);
pSampleIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);


<span style="color:Green; // Get the default video renderer
IBaseFilter pRenderer = <span style="color:Blue; new VideoRendererDefault() <span style="color:Blue; as IBaseFilter;
hr = m_FilterGraph.AddFilter(pRenderer, <span style="color:#A31515; "Renderer");
DsError.ThrowExceptionForHR( hr );


pRenderIn = DsFindPin.ByDirection(pRenderer, PinDirection.Input, 0);


<span style="color:Green; // Add the sample grabber to the graph
hr = m_FilterGraph.AddFilter( baseGrabFlt, <span style="color:#A31515; "Ds.NET Grabber" );
DsError.ThrowExceptionForHR( hr );


<span style="color:Blue; if (m_VidControl == <span style="color:Blue; null)
{
<span style="color:Green; // Connect the Still pin to the sample grabber
hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
DsError.ThrowExceptionForHR( hr );


<span style="color:Green; // Connect the capture pin to the renderer
hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
DsError.ThrowExceptionForHR( hr );
}
<span style="color:Blue; else
{
<span style="color:Green; // Connect the capture pin to the renderer
hr = m_FilterGraph.Connect(pCaptureOut, pRenderIn);
DsError.ThrowExceptionForHR( hr );


<span style="color:Green; // Connect the Still pin to the sample grabber
hr = m_FilterGraph.Connect(m_pinStill, pSampleIn);
DsError.ThrowExceptionForHR( hr );
}


<span style="color:Green; // Learn the video properties
SaveSizeInfo(sampGrabber);
ConfigVideoWindow(hControl);


<span style="color:Green; // Start the graph
IMediaControl mediaCtrl = m_FilterGraph <span style="color:Blue; as IMediaControl;
hr = mediaCtrl.Run();
DsError.ThrowExceptionForHR( hr );
}
<span style="color:Blue; finally
{
<span style="color:Blue; if (sampGrabber != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(sampGrabber);
sampGrabber = <span style="color:Blue; null;
}
<span style="color:Blue; if (pCaptureOut != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(pCaptureOut);
pCaptureOut = <span style="color:Blue; null;
}
<span style="color:Blue; if (pRenderIn != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(pRenderIn);
pRenderIn = <span style="color:Blue; null;
}
<span style="color:Blue; if (pSampleIn != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(pSampleIn);
pSampleIn = <span style="color:Blue; null;
}
}
}


<span style="color:Blue; private <span style="color:Blue; void SaveSizeInfo(ISampleGrabber sampGrabber)
{
<span style="color:Blue; int hr;


<span style="color:Green; // Get the media type from the SampleGrabber
AMMediaType media = <span style="color:Blue; new AMMediaType();


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
VideoInfoHeader videoInfoHeader = (VideoInfoHeader) Marshal.PtrToStructure( media.formatPtr, <span style="color:Blue; typeof(VideoInfoHeader) );
m_videoWidth = videoInfoHeader.BmiHeader.Width;
m_videoHeight = videoInfoHeader.BmiHeader.Height;
m_stride = m_videoWidth * (videoInfoHeader.BmiHeader.BitCount / 8);


DsUtils.FreeAMMediaType(media);
media = <span style="color:Blue; null;
}


<span style="color:Green; // Set the video window within the control specified by hControl
<span style="color:Blue; private <span style="color:Blue; void ConfigVideoWindow(Control hControl)
{
<span style="color:Blue; int hr;


IVideoWindow ivw = m_FilterGraph <span style="color:Blue; as IVideoWindow;


<span style="color:Green; // Set the parent
hr = ivw.put_Owner(hControl.Handle);
DsError.ThrowExceptionForHR( hr );


<span style="color:Green; // Turn off captions, etc
hr = ivw.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings);
DsError.ThrowExceptionForHR( hr );


<span style="color:Green; // Yes, make it visible
hr = ivw.put_Visible( OABool.True );
DsError.ThrowExceptionForHR( hr );


<span style="color:Green; // Move to upper left corner
Rectangle rc = hControl.ClientRectangle;
hr = ivw.SetWindowPosition( 0, 0, rc.Right, rc.Bottom );
DsError.ThrowExceptionForHR( hr );
}


<span style="color:Blue; private <span style="color:Blue; void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
{
<span style="color:Blue; int hr;
AMMediaType media = <span style="color:Blue; new AMMediaType();


<span style="color:Green; // Set the media type to Video/RBG24
media.majorType = MediaType.Video;
media.subType = MediaSubType.RGB24;
media.formatType = FormatType.VideoInfo;
hr = sampGrabber.SetMediaType( media );
DsError.ThrowExceptionForHR( hr );


DsUtils.FreeAMMediaType(media);
media = <span style="color:Blue; null;


<span style="color:Green; // Configure the samplegrabber
hr = sampGrabber.SetCallback( <span style="color:Blue; this, 1 );
DsError.ThrowExceptionForHR( hr );
}


<span style="color:Green; // Set the Framerate, and video size
<span style="color:Blue; private <span style="color:Blue; void SetConfigParms(IPin pStill, <span style="color:Blue; int iWidth, <span style="color:Blue; int iHeight, <span style="color:Blue; short iBPP)
{
<span style="color:Blue; int hr;
AMMediaType media;
VideoInfoHeader v;


IAMStreamConfig videoStreamConfig = pStill <span style="color:Blue; as IAMStreamConfig;


<span style="color:Green; // Get the existing format block
hr = videoStreamConfig.GetFormat(<span style="color:Blue; out media);
DsError.ThrowExceptionForHR(hr);


<span style="color:Blue; try
{
<span style="color:Green; // copy out the videoinfoheader
v = <span style="color:Blue; new VideoInfoHeader();
Marshal.PtrToStructure( media.formatPtr, v );


<span style="color:Green; // if overriding the width, set the width
<span style="color:Blue; if (iWidth > 0)
{
v.BmiHeader.Width = iWidth;
}


<span style="color:Green; // if overriding the Height, set the Height
<span style="color:Blue; if (iHeight > 0)
{
v.BmiHeader.Height = iHeight;
}


<span style="color:Green; // if overriding the bits per pixel
<span style="color:Blue; if (iBPP > 0)
{
v.BmiHeader.BitCount = iBPP;
}


<span style="color:Green; // Copy the media structure back
Marshal.StructureToPtr( v, media.formatPtr, <span style="color:Blue; false );


<span style="color:Green; // Set the new format
hr = videoStreamConfig.SetFormat( media );
DsError.ThrowExceptionForHR( hr );
}
<span style="color:Blue; finally
{
DsUtils.FreeAMMediaType(media);
media = <span style="color:Blue; null;
}
}


<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; Shut down capture </summary>
<span style="color:Blue; private <span style="color:Blue; void CloseInterfaces()
{
<span style="color:Blue; int hr;


<span style="color:Blue; try
{
<span style="color:Blue; if( m_FilterGraph != <span style="color:Blue; null )
{
IMediaControl mediaCtrl = m_FilterGraph <span style="color:Blue; as IMediaControl;


<span style="color:Green; // Stop the graph
hr = mediaCtrl.Stop();
}
}
<span style="color:Blue; catch (Exception ex)
{
Debug.WriteLine(ex);
}


<span style="color:Blue; if (m_FilterGraph != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(m_FilterGraph);
m_FilterGraph = <span style="color:Blue; null;
}


<span style="color:Blue; if (m_VidControl != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(m_VidControl);
m_VidControl = <span style="color:Blue; null;
}


<span style="color:Blue; if (m_pinStill != <span style="color:Blue; null)
{
Marshal.ReleaseComObject(m_pinStill);
m_pinStill = <span style="color:Blue; null;
}
}


<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; sample callback, NOT USED. </summary>
<span style="color:Blue; int ISampleGrabberCB.SampleCB( <span style="color:Blue; double SampleTime, IMediaSample pSample )
{
Marshal.ReleaseComObject(pSample);
<span style="color:Blue; return 0;
}


<span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green; buffer callback, COULD BE FROM FOREIGN THREAD. </summary>
<span style="color:Blue; int ISampleGrabberCB.BufferCB( <span style="color:Blue; double SampleTime, IntPtr pBuffer, <span style="color:Blue; int BufferLen )
{
<span style="color:Green; // Note that we depend on only being called once per call to Click. Otherwise
<span style="color:Green; // a second call can overwrite the previous image.
Debug.Assert(BufferLen == Math.Abs(m_stride) * m_videoHeight, <span style="color:#A31515; "Incorrect buffer length");


<span style="color:Blue; if (m_WantOne)
{
m_WantOne = <span style="color:Blue; false;
Debug.Assert(m_ipBuffer != IntPtr.Zero, <span style="color:#A31515; "Unitialized buffer");


<span style="color:Green; // Save the buffer
CopyMemory(m_ipBuffer, pBuffer, BufferLen);


<span style="color:Green; // Picture is ready.
m_PictureReady.Set();
}


<span style="color:Blue; return 0;
}
}
}
[/code]
<br/>
<br/>
<br/>
<br/>
Many thanks,<br/>
<br/>
Chris

View the full article
 
Back
Top