0

我正在使用 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);
        }
    }
}

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

4

1 回答 1

0

我相信您需要将这些属性更改与 BeginInit()/EndInit() 调用括起来。

于 2012-05-29T22:12:22.947 回答