图像控件在wpf中显示已删除的图像 [英] Image Control Showing Deleted Image in wpf

查看:86
本文介绍了图像控件在wpf中显示已删除的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建wpf应用程序。我在图像控件中显示预览图像(由cam拍摄)。默认情况下会保存图像。如果用户想要重新拍摄照片我将删除当前图像。



 int imageCount = Directory.GetFiles(imgDir,*, SearchOption.TopDirectoryOnly)。长度; 

ImageFormat imgFormat =(ImageFormat)GetValue(SnapshotFormatProperty);

string filePath = Path.Combine(imgDir,IMAGE_+ ++ imageCount +.Jpeg);

使用(Bitmap bmp = new Bitmap(panelWidth,panelHeight))
{
using(Graphics graphics = Graphics.FromImage(bmp))
{
// image
}
bmp.Save(filePath,imgFormat);
}
globalFilePath = filePath;





在图像控制中显示图像

< pre lang =cs> string strUri = String .Format(WebCamControl.globalFilePath);
previewImage.Source = BitmapFromUri( new Uri(strUri));





  public   static  ImageSource BitmapFromUri(Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
返回位图;
}







 previewImage.Source = null; 



  if (System.IO.File.Exists(WebCamControl.globalFilePath)) 
{
System.IO.File.Delete(WebCamControl.globalFilePath);
}
else
{
MessageBox.Show( 文件不存在);
}







完成此过程后,当我拍摄新图像时,图像控件显示删除的上一张图片。图像控件是否缓存了普通图像?请给我解决方案。

解决方案

我想, BitmapCacheOption.OnLoad 它:在加载时将整个图像缓存到内存中。所有图像数据请求都从内存存储器中填充。

尝试而不是


I am creating wpf application. I am showing the preview image(which is taken by cam) in Image control. The image get saved by default. If the user want to retake the photo i am deleting the current image.

int imageCount = Directory.GetFiles(imgDir, "*",SearchOption.TopDirectoryOnly).Length;

  ImageFormat imgFormat = (ImageFormat)GetValue(SnapshotFormatProperty);

   string filePath = Path.Combine(imgDir, "IMAGE_" + ++imageCount + ".Jpeg");

                using (Bitmap bmp = new Bitmap(panelWidth, panelHeight))
                {
                    using (Graphics graphics = Graphics.FromImage(bmp))
                    {
                        // image
                    }
                    bmp.Save(filePath, imgFormat);
                }
                globalFilePath = filePath;



showing image in Image Control

string strUri = String.Format(WebCamControl.globalFilePath);
           previewImage.Source = BitmapFromUri(new Uri(strUri));



public static ImageSource BitmapFromUri(Uri source)
        {
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = source;
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.EndInit();
            return bitmap;
        }




previewImage.Source = null;


if (System.IO.File.Exists(WebCamControl.globalFilePath))
                {
                    System.IO.File.Delete(WebCamControl.globalFilePath);
                }
                else
                {
                    MessageBox.Show("File Not Exists");
                }




After Completing this process when i take the new image the Image control shows the previous image which was deleted. Is the Image Control caching the previus Image??Kindly give me the solution.

解决方案

I suppose, BitmapCacheOption.OnLoad does it: "Caches the entire image into memory at load time. All requests for image data are filled from the memory store."
Try None instead


这篇关于图像控件在wpf中显示已删除的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆