C#:捕获多个图像并保存在同一个文件夹中?

时间:2017-06-27 06:37:41

标签: c# winforms screen-capture savefiledialog

目前我的代码能够捕获图像并保存在定义的位置,但如果我在第二次尝试相同的图像被覆盖,那么如果该文件夹中存在相同的文件名,我们必须动态更改文件名。 我怎么能这样做?

目前的截屏代码是:

private void CaptureMyScreen()
{
    try
    {
        //Creating a new Bitmap object
        Bitmap captureBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);

        //Creating a Rectangle object which will capture our Current Screen
        Rectangle captureRectangle = Screen.AllScreens[0].Bounds;

        //Creating a New Graphics Object
        Graphics captureGraphics = Graphics.FromImage(captureBitmap);

        //Copying Image from The Screen
        captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0, captureRectangle.Size);

        //Saving the Image File (I am here Saving it in My D drive).
        captureBitmap.Save(@"D:\Capture.jpg", ImageFormat.Jpeg);

        //Displaying the Successfull Result

        MessageBox.Show("Screen Captured");
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

1 个答案:

答案 0 :(得分:4)

您可以使用GUID获取每个捕获文件的唯一名称。像

这样的东西
string guid = Guid.NewGuid().ToString();
captureBitmap.Save(@"D:\Capture-" + guid + ".jpg",ImageFormat.Jpeg);

或者,使用当前日期和时间,如下所示:

string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
captureBitmap.Save(@"D:\Capture-" + timestamp + ".jpg",ImageFormat.Jpeg);
相关问题