Wednesday, May 23, 2007

C# Webcam Capture

Well I've tried to grab the stream from the IVideoWindow object in my application but to no avail. The only thing left is to grab a snapshot of the window control itself.

(If anyone can acutally manage to grab the stream please let me know!)

So I am left with this approach:



public virtual Bitmap CaptureControl( Control window )
{
Rectangle rect = window.RectangleToScreen( window.DisplayRectangle );
return capture(window, rect);
}

private Bitmap capture(Control window, Rectangle rect)
{
Bitmap memoryImage = null;

try
{
using (Graphics graphics = window.CreateGraphics())
{
memoryImage = new Bitmap(rect.Width, rect.Height, graphics);
using (Graphics memoryGraphics = Graphics.FromImage(memoryImage))
{
memoryGraphics .CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Capture failed");
}

return memoryImage;
}


No comments:

Post a Comment