2012-05-29 22 views
0

我正在使用WPF 4.0。這是我的代碼:爲什麼我的WPF圖像不顯示?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media.Imaging; 
using System.IO; 

namespace ResourcesLearning 
{ 
    class Program 
    { 
     [STAThread] 
     static void Main() 
     { 
      Application app = new Application(); 
      Window win = new Window(); 
      Image img = new Image(); 
      String fileName = @"C:\Users\dev\Pictures\1.png"; 

      //It work fine (I see image) 
      BitmapImage bim = new BitmapImage(new Uri(fileName)); 

      //It not work (I do not see image) 
      //BitmapImage bim = new BitmapImage(); 
      //bim.UriSource = new Uri(fileName); 

      //It not work (I do not see image) 
      //FileStream fs = new FileStream(fileName, FileMode.Open); 
      //BitmapImage bim = new BitmapImage(); 
      //bim.StreamSource = fs; 

      img.Source = bim; 
      win.Content = img; 
      app.Run(win); 
     } 
    } 
} 

爲什麼圖像不顯示用於我的已評論變體?只有第一個變體成功運行。

+0

你運行調試器來查看文件流是否在BitmapImage bim = new BitmapImage(new Uri(fileName))之後加載? – Xcalibur37

回答

0

我相信你需要用BeginInit()/ EndInit()調用括起這些屬性更改。