【发布时间】:2015-02-25 11:49:40
【问题描述】:
我有一个显示预览图像的图像控件。如果用户删除图像(位于文件夹中),它应该显示新拍摄的图像。 但是图像控件显示的是删除的图像,而不是显示新的图像。
// clear the image source before deleting the image.
// save image in the directory
public string globalFilePath;
int imageCount = Directory.GetFiles(imgDir, "*", SearchOption.TopDirectoryOnly).Length;
string filePath = Path.Combine(imgDir, "IMAGE_" + ++imageCount + ".jpeg");
globalFilePath = filePath;
// setting image control source
var strUri = new Uri(WebCamControl.globalFilePath, UriKind.Relative);
previewImage.Source = BitmapFromUri(strUri);
//Method
public static ImageSource BitmapFromUri(Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
return bitmap;
}
// delete image
previewImage.Source = null;
if (System.IO.File.Exists(WebCamControl.globalFilePath))
{
System.IO.File.Delete(WebCamControl.globalFilePath);
}
else
{
MessageBox.Show("File Not Exists");
}
删除目录文件中的图像后,图像图像控件应显示新图像,但我的图像控件显示已删除的图像。请给我解决方案。
【问题讨论】:
-
成功加载后会显示新的图片。此代码未显示如何/何时加载 next 图像。
-
删除文件系统中的图片后,会重新拍摄新的图片并默认保存。我得到该路径并将其分配给图像控制源。 @Henk
-
我发现我保存新图像的名称与已删除图像的名称相同的问题,我改变了为新图像创建名称的方式。感谢您的帮助:)