0

The code below works here and there. Sometimes it save the image just fine, and other times it doesn't. The image is being save locally and the folder has the correct permissions.

 using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }
                bitmap.Save("test.png", ImageFormat.Png);
            }

            Rectangle newbounds = Bounds;
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
                bitmap.Save(picLocation, ImageFormat.Png);
            }

Here is the error that I am getting:

enter image description here

Can someone please tell me what I am doing wrong?

nate
  • 1,418
  • 5
  • 34
  • 73
  • is there any other program holding a lock on that image that you are trying to overwrite? – Steve Jul 20 '16 at 18:40
  • @Steve not to my knowledge, there is not any other programs on the system that deal images – nate Jul 20 '16 at 18:49
  • I would check if the image is saved twice. Or if the button is clicked twice which caused the same function being called twice and result in file being locked. And also I would avoid using relative path since the image might end up saving in system directory (results in no permission) depending on how your program is opened. – Steve Jul 20 '16 at 18:56
  • 1
    Put File.WriteAllText(picLocation, "test") before the bitmap.Save() call and you'll get a much better exception. – Hans Passant Jul 20 '16 at 20:33
  • @Steve I have checked to see if the button is being clicked more then once, and it is not. I will keep digging with the rest of your suggestions – nate Jul 20 '16 at 20:59
  • @HansPassant I have impended that line of code that you suggested. We are running test now to see if it made a difference. – nate Jul 20 '16 at 21:00
  • you can use visual winapi/GDI calls for visual components (which has window type handles) only in the main thread of your app otherwise it will cause various problems including random exception even in unrelated code ... So my bet is you are saving/rendering/updating something you should not in a separate thread (as interopservices implies). But I may be wrong as I do not use GDI+ or do not have any knowledge about your code architecture – Spektre Jul 21 '16 at 05:01
  • @HansPassant There was much more info given into the exception. We fund the error stemmed from another line in the code before that block. We were able to fix this issue based on that. Would you like to answer the question? – nate Jul 21 '16 at 15:03
  • I don't know what error you found, just post the answer yourself. – Hans Passant Jul 21 '16 at 15:45

0 Answers0