Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                    Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(@"C:\Temp\printscreen.jpg", ImageFormat.Jpeg);

这是我的打印屏幕按钮代码。问题是我按了几次按钮,它只是覆盖了旧的图像文件(printscreen.jpg),而不会创建另一个新的图像文件,例如printscreen1.jpg。

最佳答案

试试这个。每次都会生成一个唯一的文件。

Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                    Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(@"C:\Temp\printscreen"+Guid.NewGuid()+".jpg", ImageFormat.Jpeg);

或者
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                    Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
var date = DateTime.Now.ToString("MMddyyHmmss");
bitmap.Save(@"C:\Temp\printscreen"+date+".jpg", ImageFormat.Jpeg);

关于c# - 在C#中保存打印屏幕,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18046083/

10-13 07:08