0

I've looked for answers to this but can't figure out what's going wrong, basically the screencapture function below which creates and saves a bitmap to a file works fine the first time but throws this error when call a second time?? "A generic error occurred in GDI+"

    public void Capture(int size, int x, int y, string fname)
    {
        using (Bitmap bmpScreenCapture = new Bitmap(size,size))
        {
            using (Graphics g = Graphics.FromImage(bmpScreenCapture))
            {
                g.CopyFromScreen(x,
                     y,
                     0, 0,
                     bmpScreenCapture.Size,
                     CopyPixelOperation.SourceCopy);

                bmpScreenCapture.Save(fname, ImageFormat.Bmp);
            }
        }
    }
tshepang
  • 12,111
  • 21
  • 91
  • 136
chillydk147
  • 385
  • 1
  • 10
  • Try deleting the file first. – SLaks Jan 10 '14 at 14:06
  • 1
    I can't reproduce it with that simple code. Do you maybe load the image from the filename? Loading a Bitmap from a file locks the file and can cause that error. – Dirk Jan 10 '14 at 14:07
  • Yes, I did this in another method "Image.FromFile(fname)", how do I ensure the image is not locked? – chillydk147 Jan 10 '14 at 14:21
  • [This question](http://stackoverflow.com/questions/6576341/open-image-from-file-then-release-lock) shows how to load an image without locking it. – Dirk Jan 10 '14 at 15:57

0 Answers0