Search code examples
c#screenshot

Creating and Deleting Pictures/Screenshots


Well, happens that im writing a program to take some screenshots and having some difficulty dealing with files being already in use by another process, hopefully someone can help me find a way to "close" this process or enlight me how to proceed.

//Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                               Screen.PrimaryScreen.Bounds.Height,
                               PixelFormat.Format32bppArgb);

// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);

// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                            Screen.PrimaryScreen.Bounds.Y,
                            0,
                            0,
                            Screen.PrimaryScreen.Bounds.Size,
                            CopyPixelOperation.SourceCopy);

// Save the screenshot to the specified path that the user has chosen.
//bmpScreenshot.Save(nomeScreen + ".jpeg", ImageFormat.Jpeg);
bmpScreenshot.Save(@"c:\temp\"+nomeScreen+".jpeg", ImageFormat.Jpeg);
count++;
nomeScreen = "S"+Convert.ToString(count);

This is what I have (i know, poor programing skills) to take several screenshots, however later on I want them to be deleted from the place where I'm storing them

http://gyazo.com/4b50945b0d157d082f7897e34a705560

This is a screenshot of what it occurs. How do I "close" the bitmap? The only actions that the program does so far is taking screenshots and delete them.

string pathString = "C:\\temp";
DirectoryInfo d = new DirectoryInfo(pathString);
foreach (var k in d.GetFiles("*.jpeg"))
{
    File.Delete(pathString+"\\" + k);
}

Solution

  • I believe you need to call bmpScreenshot.Close() after bmpScreenshot.Save().

    Sorry I was on phone and couldn't check if it had close.

    Try this:

    //Create a new bitmap.
    using(var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                   Screen.PrimaryScreen.Bounds.Height,
                                   PixelFormat.Format32bppArgb))
    {
    
    // Create a graphics object from the bitmap.
    var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
    
    // Take the screenshot from the upper left corner to the right bottom corner.
    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                Screen.PrimaryScreen.Bounds.Y,
                                0,
                                0,
                                Screen.PrimaryScreen.Bounds.Size,
                                CopyPixelOperation.SourceCopy);
    
    // Save the screenshot to the specified path that the user has chosen.
    //bmpScreenshot.Save(nomeScreen + ".jpeg", ImageFormat.Jpeg);
    bmpScreenshot.Save(@"c:\temp\"+nomeScreen+".jpeg", ImageFormat.Jpeg);
    }
    count++;
    nomeScreen = "S"+Convert.ToString(count);
    

    So I tested the code and I got some interesting results.

    I have this:

    private void button1_Click(object sender, EventArgs e)
        {
            string nomeScreen = "screenshot"+new Random().Next();
            screenshotPath = Application.StartupPath+"\\" + nomeScreen + ".jpeg";
            //Create a new bitmap.
            /*using (*/
            var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                Screen.PrimaryScreen.Bounds.Height,
                                PixelFormat.Format32bppArgb);//)
           // {
    
                // Create a graphics object from the bitmap.
            /*using (*/
            var gfxScreenshot = Graphics.FromImage(bmpScreenshot);//)
                //{
    
                    // Take the screenshot from the upper left corner to the right bottom corner.
                    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                                Screen.PrimaryScreen.Bounds.Y,
                                                0,
                                                0,
                                                Screen.PrimaryScreen.Bounds.Size,
                                                CopyPixelOperation.SourceCopy);
    
                //}
    
                // Save the screenshot to the specified path that the user has chosen.
                //bmpScreenshot.Save(nomeScreen + ".jpeg", ImageFormat.Jpeg);
                bmpScreenshot.Save(screenshotPath, ImageFormat.Jpeg);
           // }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            File.Delete(screenshotPath);
        }
    

    It works fine without any using statements, though in my opinion it is good to have both of the commented out ones. Maybe your problem lies elsewhere. Have you checked your folder's permissions. Maybe you could try saving to the path of the exe of the application? That might fix it. If it does, then you need to check the folder permissions for your "temp" folder or wherever you are trying to save.