分享

WPF中Image控件的Source属性

 迷之雾 2011-04-16
imageEditImage是一个Image控件,在后台代码中我想给它指定Source的属性。我先如下方式进行:
BitmapImage image = new BitmapImage(new Uri(strImagePath, UriKind.Absolute));
imageEditImage.Source = image;
strImagePath是图片的绝对路径。

在另一处代码中我想把strImagePath指定的图片删掉,操作如下:
if (System.IO.File.Exists(strImagePath))
 {
    System.IO.File.Delete(strImagePath);
 }
但是删除操作报错,愿意是程序在使用图片文件。

解决方法如下:
   BitmapImage image = new BitmapImage();
   m_ImageStream = new FileStream(strImagePath, FileMode.Open);
   image.BeginInit();
   image.StreamSource = m_ImageStream;
   image.EndInit();
   imageEditImage.Source = 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);
 }


将byte保存的图片绑定到wpf中的image中:

BitmapImage bitImg = new BitmapImage();
System.IO.MemoryStream ms = new System.IO.MemoryStream(pictureBytes);
bitImg.BeginInit();
bitImg.StreamSource = ms;
bitImg.EndInit();
ImgProofPicture.Source = bitImg;

其中pictureBytes是byte[]类型。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多