WPF Image控件
1.只是显示图片
string path = 相对路径
path =绝对路径
BitmapImage image = new BitmapImage(new Uri(path, UriKind.Absolute));
img.Source = image;
2.在另一段代码中,要删除这个图片
就需要先把图片转换成文件流,在当前读取流完时,关闭流,当要删除图片时释放资源,修改代码如下:
System.IO.FileStream m_ImageStream;
string path;
string path = 相对路径
path =绝对路径
BitmapImage image = new BitmapImage();
m_ImageStream = new System.IO.FileStream(path,
FileMode.Open);
image.BeginInit();
image.StreamSource = m_ImageStream;
image.EndInit();
img.Source = image;
删除代码:
if (m_ImageStream != null)
{
m_ImageStream.Close();
m_ImageStream.Dispose();
}
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
BitmapImage i = new
BitmapImage();//虽然点了删除,可是图片还显示在窗体里,这时要想重新刷新,给image控件赋值一个空的图片
img.Source = i;
}