Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF保存屏幕截图_C#_Wpf_Screenshot - Fatal编程技术网

C#WPF保存屏幕截图

C#WPF保存屏幕截图,c#,wpf,screenshot,C#,Wpf,Screenshot,当我调用SaveImageCommandExecute时,它会给我一个错误,我不理解它。有人能解释一下吗?非常感谢:) 错误: 代码: private void SaveImageCommandExecute() { string pathToFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments); ApplicationViewModel Applicati

当我调用SaveImageCommandExecute时,它会给我一个错误,我不理解它。有人能解释一下吗?非常感谢:)

错误:

代码:

private void SaveImageCommandExecute()
    {
        string pathToFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
        ApplicationViewModel ApplicationVM = System.Windows.Application.Current.Resources["MainViewModel"] as ApplicationViewModel;

        string filePath;
        string fullPath;
        using (FolderBrowserDialog dlg = new FolderBrowserDialog())
        {
            dlg.Description = "Select folder";
            dlg.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
            dlg.ShowNewFolderButton = true;
            DialogResult result = dlg.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                filePath = dlg.SelectedPath;
                fullPath = filePath + "\\" + DateTime.Now.ToString("HH-mm-ss_dd-MM-yyyy") + ".jpeg";
                camera.SaveThermalImage(@"C:\programs\file.jpeg");
            }
        }

    }

顺便说一句,我使用的是Workswell WIC SDK。

看起来像是生成了
完整路径,但没有使用它。您可以使用硬编码的
C:\programs\file.jpeg
。该文件夹可能不存在。这里是编辑的那一行

private void SaveImageCommandExecute()
{
    string pathToFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
    ApplicationViewModel ApplicationVM = System.Windows.Application.Current.Resources["MainViewModel"] as ApplicationViewModel;

    string filePath;
    string fullPath;
    using (FolderBrowserDialog dlg = new FolderBrowserDialog())
    {
        dlg.Description = "Select folder";
        dlg.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
        dlg.ShowNewFolderButton = true;
        DialogResult result = dlg.ShowDialog();
        if (result == System.Windows.Forms.DialogResult.OK)
        {
            filePath = dlg.SelectedPath;
            fullPath = filePath + "\\" + DateTime.Now.ToString("HH-mm-ss_dd-MM-yyyy") + ".jpeg";
            camera.SaveThermalImage(fullPath);
        }
    }
}

注意:我不知道如何使用
camera.SaveThermalImage
工作,也可能存在问题。

C:\Progams将不存在

您可能想到的是c:\程序文件

用户几乎肯定无法写入c:\program files下的文件夹,因此即使您“更正”了该行,也会因此失败:

camera.SaveThermalImage(@"C:\program files\file.jpeg");
事实证明,允许人们在你的程序所在的地方写东西会打开一个安全漏洞。 在用户的上下文中,恶意软件经常试图进行恶意操作

这个问题在几年前就解决了,自从win 7 iirc之后,您就无法将文件保存到此位置

这是公认的违反直觉的,但很容易证明

运行记事本

文件>保存

尝试将blaa.txt保存到c:\程序文件中,然后查看发生了什么

相反,您应该在文件选择器使用或appdata时向commondocuments之类的地方写入

对于文件选择器,还有另一个需要注意的问题

我忘了winforms对话框是如何工作的。 我想这可能允许用户选择一个不同于您预期的文件夹。
因此,为这种目的编写自定义用户界面是很常见的,这不允许更改文件夹。

放置一个try-catch块,并检查
InnerException
属性以了解更多信息。