I'm making a custom webcam user control.
I use Microsoft Expression Encoder, and set a PreviewWindow on a panel inside the control.
All's fine, except that when I try to grab the image, I get an exception:
Generic GDI+ exception
My first try was:
using (var bmp = new Bitmap(p.Width, p.Width))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(@"c:\test.png");
}
Another waS:
using (Bitmap bitmap = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
Rectangle rectanglePanelVideoPreview = panelVideoPreview.Bounds;
Point sourcePoints = panelVideoPreview.PointToScreen(new Point(panelVideoPreview.ClientRectangle.X, panelVideoPreview.ClientRectangle.Y));
g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size);
}
string strGrabFileName = String.Format("C:\\Snapshot.jpg", DateTime.Now);
bitmap.Save(strGrabFileName, ImageFormat.Jpeg);
The problem is in writing the file. There may not be enough space in c:\ drive or you do not have permission to write to c:\ drive(outside a folder). Try writing to AppData ,if you want to write to c:\ drive,you need to acquire administrative privileges by using a custom manifest.