zoukankan      html  css  js  c++  java
  • WPF中Image控件的Source属性

    imgBook 是一个Image控件,在后台代码中我想给它指定Source的属性。我先如下方式进行:

    Uri uri = new Uri(strImagePath, UriKind.RelativeOrAbsolute);
    
    
    imgBook.Source = new BitmapImage(uri);

    strImagePath是图片的绝对路径。

    在另一处代码中我想把strImagePath指定的图片删掉,操作如下:

    if (System.IO.File.Exists(strImagePath))
                    {
                        System.IO.File.Delete(strImagePath);
                    }

    但是删除操作报错,原因是程序在使用图片文件。

    解决方法如下:

    Stream m_ImageStream;
    private BitmapImage GetImageSource(string strImagePath)
            {
    //m_ImageStream.Close();
                //m_ImageStream.Dispose();
                //m_ImageStream = null;
    if (m_ImageStream != null)
                {
                    m_ImageStream.Close();
                    m_ImageStream.Dispose();
                }
    
    
    BitmapImage image = new BitmapImage();
                m_ImageStream = new FileStream(strImagePath, FileMode.Open);
                image.BeginInit();
                image.StreamSource = m_ImageStream;
                image.EndInit();
    
    
    return image;
            }

    其中m_ImageStream是一个全局的Stream变量,在删除操作前关闭这个变量就不会再出错了。

    if (m_ImageStream != null)
                    {
                        m_ImageStream.Close();
                        m_ImageStream.Dispose();
                    }
    
    
    if (System.IO.File.Exists(strImagePath))
                    {
                        System.IO.File.Delete(strImagePath);
                    }
  • 相关阅读:
    mysql 内联接、左联接、右联接、完全联接、交叉联接 区别
    JS 时间字符串与时间戳之间的转换
    MySQL性能优化的最佳20条经验
    ++i 与 i++ 的区别
    === 与 == 区别
    SC命令创建和删除windows服务
    杂记
    linux 文件编程
    u-boot 启动过程
    简单冒泡法
  • 原文地址:https://www.cnblogs.com/z_lb/p/2936611.html
Copyright © 2011-2022 走看看