C#Image - System.NullReferenceException

时间:2016-01-08 16:58:35

标签: c# image nullreferenceexception bitmapimage

我目前正在尝试通过一个按钮(nextImg)来更新程序,该按钮在单击时调用一个创建两个位图的函数,从0开始生成一个数字,并在每次单击nextImg按钮时递增。然后程序获取该数字(num)并从List [num]返回一个字符串,该字符串包含我照片的所有Uri源。但是,当我点击下一张图片时,我得到了一个

System.NullReferenceException。

private void Image_Loaded(object sender, RoutedEventArgs e)
    {
        // Create a new BitmapImage.
        BitmapImage b = new BitmapImage();
        BitmapImage b1 = new BitmapImage();
        Image img = new Image();

        string sSrc = numGen();            
        b.BeginInit();
        b.UriSource = new Uri(sSrc);
        b.EndInit();

        b1.BeginInit();
        b1.UriSource = b.UriSource;
        b1.EndInit();

        img = sender as Image;
        img.Source = b;         // Error on this line

        Canvas1.Height = Picturebox1.Height;
        Canvas1.Width = Picturebox1.Width;
        Canvas1.Margin = Picturebox1.Margin;
    }

0 个答案:

没有答案