1

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);        
Cranio
  • 9,647
  • 4
  • 35
  • 55
  • Have you checked if there is enough space in c:\ drive or is writing to c:\ drive permitted(outside a folder).I Have encountered similar exceptions in such scenario – techno Jun 23 '12 at 09:06
  • @techno I am ALMOST sure that the problem is in permissions. I can write to my App data folder. Is there a way to write to a custom folder (not C: but maybe inside my application execution folder) without setting permissions from Windows? – Cranio Jun 23 '12 at 09:13
  • You can write in the application folder if it is not in c:\Program Files\ – techno Jun 23 '12 at 09:24
  • I've tried, but it seems not to work (using Application.StartupPath). – Cranio Jun 23 '12 at 09:25
  • Try saving to the Temporary folder -Path.gettemppath(); – techno Jun 23 '12 at 09:29
  • I am trying something similar but I only get a blank image. any Ideas? – Herrozerro May 03 '13 at 01:22

1 Answers1

1

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.

techno
  • 6,100
  • 16
  • 86
  • 192