在代码中加载WPF图像的问题,我哪里出错了?

时间:2009-10-26 10:08:21

标签: c# wpf image

我的xaml中有一张图片,如下所示:

<Image Name="TotalFloors" Width="98" Source="../Images/FloorOne.png" Margin="0 0 0 10" VerticalAlignment="Bottom" />

这是正常加载,但是我试图在代码中更改此图像的所有内容都导致无法显示。

我尝试了一个MessageBox.Show(TotalFloors.Source.ToString());它又回来了:

包://应用:,,, / MyClient;组件/图像/ FloorOne.png

然后提示我使用此代码:

        private void GetFloorImg()
    {
        MessageBox.Show(TotalFloors.Source.ToString());
        BitmapImage floorImage = new BitmapImage();
        Uri uriSource;
        switch (App.selectedBuilding.Floors)
        {
            case 1:
                uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorOne.png", UriKind.Absolute);
                break;
            case 2:
                uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorTwo.png", UriKind.Absolute);
                break;
            case 3:
                uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorThree.png", UriKind.Absolute);
                break;
            default :
                throw new NotImplementedException();
        }
        floorImage.UriSource = uriSource;
        TotalFloors.Source = floorImage;

    }

然而,这也不起作用。 图像都设置为文件夹中的资源。断点显示我的代码被击中了。

任何想法? TIA,Kohan。

3 个答案:

答案 0 :(得分:2)

对您的代码稍作修改:

private void GetFloorImg()
{
    MessageBox.Show(TotalFloors.Source.ToString());
    BitmapImage floorImage = new BitmapImage();
    Uri uriSource;
    switch (App.selectedBuilding.Floors)
    {
        case 1:
            uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorOne.png", UriKind.Absolute);
            break;
        case 2:
            uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorTwo.png", UriKind.Absolute);
            break;
        case 3:
            uriSource = new Uri("pack://application:,,,/MyClient;component/Images/FloorThree.png", UriKind.Absolute);
            break;
        default :
            throw new NotImplementedException();
    }
    floorImage.BeginInit();
    floorImage.UriSource = uriSource;
    floorImage.EndInit();
    TotalFloors.Source = floorImage;

}

答案 1 :(得分:1)

试试这个:

private void GetFloorImg()
{
    string logoFileLocation = "pack://application:,,,/MyClient;component/Images/";

    switch (App.selectedBuilding.Floors)
    {
        case 1:
            logoFileLocation += "FloorOne.png";
            break;
        case 2:
            logoFileLocation += "FloorTwo.png";
            break;
        case 3:
            logoFileLocation += "FloorThree.png";
            break;
        default :
            throw new NotImplementedException();
    }

    TotalFloors.Source = BitmapFrame.Create(new Uri(logoFileLocation, UriKind.RelativeOrAbsolute));
}

PS:图像构建操作应为“资源”,添加或修改资源后,必须重建解决方案。你知道第一部分(你在问题中写的),我想你也知道第二部分,但以防万一。

答案 2 :(得分:1)

这是罪魁祸首:

floorImage.UriSource = uriSource;
TotalFloors.Source = floorImage;

改为使用:

TotalFloors.Source = new BitmapImage(uriSource);