为什么我的WPF图像没有显示?

时间:2012-05-29 21:03:25

标签: .net wpf image

我正在使用WPF 4.0。这是我的代码:

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);
        }
    }
}

为什么我的评论变体没有显示图像?只有第一个变体才能成功运作。

1 个答案:

答案 0 :(得分:0)

我认为您需要使用BeginInit()/ EndInit()调用来包含这些属性更改。