C#:捕获多个图像并保存在同一个文件夹中?
C#: Capture multiple image and Save in same folder?
目前我的代码可以捕捉图像并保存在定义的位置,但如果我在第二次尝试相同的图像被覆盖,所以如果该文件夹中存在相同的文件名,我们必须动态更改文件名。
我该怎么做?
目前的截屏代码为:
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);
}
}
您可以使用 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);
目前我的代码可以捕捉图像并保存在定义的位置,但如果我在第二次尝试相同的图像被覆盖,所以如果该文件夹中存在相同的文件名,我们必须动态更改文件名。 我该怎么做?
目前的截屏代码为:
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);
}
}
您可以使用 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);