[英]NullReference Exception Occurs in Image Source wpf
I am using FileSystemWatcher to get the latest image from my Assets folder, i have a webcam to capture the image and save it in the Assets folder. 我正在使用FileSystemWatcher从我的Assets文件夹中获取最新图像,我有一个网络摄像头来捕获图像并将其保存在Assets文件夹中。 After saving the image i get the latest image from FileSystemWatcher event.
保存图像后,我从FileSystemWatcher事件获取最新图像。 Here is my code :
这是我的代码:
//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);
}
));
}
At page load event the source of CustomerImage control is set to a default picture which is nopictureavail.jpeg, when a changes made in that particular Directory the image should populate in CustomerImage, filewatcher event fires then the error throws at 在页面加载事件中,CustomerImage控件的源设置为默认图片nopictureavail.jpeg,当在该特定目录中进行更改时,该图片应填充到CustomerImage中,则触发filewatcher事件,然后将错误抛出
CustomerImage.Source = (ImageSource)isc.ConvertFromString(e.FullPath);
NullReferenceException Occured in presentationCore.dll NullReferenceException出现在presentationCore.dll中
Try to add this: 尝试添加以下内容:
bitmap.CreateOption = BitmapCreateOptions.IgnoreImageCache
Here's some more 还有更多
https://stackoverflow.com/a/1689808/2609288 https://stackoverflow.com/a/1689808/2609288
You're getting this error because WPF holds on to the handle from its images. 您收到此错误是因为WPF保留了其图像中的句柄。 Try using this code instead:
尝试改用以下代码:
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; }
I have this code in an IValueConverter
to avoid a similar problem. 我在
IValueConverter
有此代码以避免类似的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.