Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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中发生NullReference异常_C#_Wpf_Image - Fatal编程技术网

C# 图像源wpf中发生NullReference异常

C# 图像源wpf中发生NullReference异常,c#,wpf,image,C#,Wpf,Image,我使用FileSystemWatcher从我的资产文件夹中获取最新图像,我有一个网络摄像头来捕获图像并将其保存在资产文件夹中。保存映像后,我从FileSystemWatcher事件获取最新映像。 这是我的密码: //FileWatcher private void FileWatcher() { path = @"..\..\Assets\WebCamImage\"; System.IO.FileSystemWatcher watcher = new

我使用FileSystemWatcher从我的资产文件夹中获取最新图像,我有一个网络摄像头来捕获图像并将其保存在资产文件夹中。保存映像后,我从FileSystemWatcher事件获取最新映像。 这是我的密码:

 //FileWatcher
 private void FileWatcher()
    {
        path = @"..\..\Assets\WebCamImage\";
        System.IO.FileSystemWatcher watcher = new FileSystemWatcher(path);
        watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;

        watcher.Changed += watcher_Changed;
        watcher.EnableRaisingEvents = true;
    }


 //Event
 void watcher_Changed(object sender, FileSystemEventArgs e)
    {                 
      CustomerImage.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
    new Action(
        delegate()
        {                     
       CustomerImage.Source = (ImageSource)isc.ConvertFromString(e.FullPath);
        }
        ));
    }    
在页面加载事件中,CustomerImage控件的源被设置为默认图片nopictureavail.jpeg,当在该特定目录中进行更改时,图像应填充到CustomerImage中,filewatcher事件触发,然后错误抛出

CustomerImage.Source = (ImageSource)isc.ConvertFromString(e.FullPath);
presentationCore.dll中发生NullReferenceException,请尝试添加以下内容:

bitmap.CreateOption = BitmapCreateOptions.IgnoreImageCache
还有一些


您会遇到此错误,因为WPF会从其图像中保留句柄。请尝试改用以下代码:

BitmapImage image = new BitmapImage();
try
{
    using (FileStream stream = File.OpenRead(filePath))
    {
        image.BeginInit();
        image.StreamSource = stream;
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.EndInit();
    }
}
catch { return DependencyProperty.UnsetValue; }

我将此代码保存在
IValueConverter
中,以避免类似问题。

我已更改了我的代码,前一个代码不起作用,请检查编辑的代码并给我您的建议。您提出了一个问题,我回答了。在问题被回答后,你真的不应该改变你的问题去问另一个问题。如果你还有其他问题,那么你应该问一个新问题,如果你想提供一些上下文,还可以提供一个到原始问题的链接。