显示gif图像

时间:2010-07-27 09:01:28

标签: wpf xbap

当我开发我的WPF PC应用程序时,我使用以下代码来显示我的gif图像,因为我意识到没有简单的方法可以做到这一点: -

XAML:

            <StackPanel Height="25" Name="stkProgressBar">
                <wfi:WindowsFormsHost Background="Transparent" HorizontalAlignment="Center">
                    <winForms:PictureBox x:Name="pictureBoxLoading" Visible="False">
                    </winForms:PictureBox>
                </wfi:WindowsFormsHost>

            </StackPanel>

XAML.cs:

 private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        this.pictureBoxLoading.Image = Properties.Resources.progressbar;
    }

现在当我使用相同的方法在我的WPF浏览器(XBAP)应用程序中显示图像时,由于这个xaml代码,它没有加载页面?任何想法为什么会这样,如果有任何其他方式在XBAP页面上显示GIF?

谢谢, ABHI。

==================

于2010年7月27日更新11:AM

现在我尝试了以下代码: -

XAML:

 <Image Name="Image1"></Image> 

XAML.cs:

        Stream imageStreamSource = new FileStream(@"C:\Inetpub\ExchangeRate\ExchangeRate\Image\progressbar.gif", FileMode.Open, FileAccess.Read, FileShare.Read);
        GifBitmapDecoder decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

        Int32Animation anim = new Int32Animation(0, decoder.Frames.Count - 1, new Duration(new TimeSpan(0, 0, 0, decoder.Frames.Count / 10, (int)((decoder.Frames.Count / 10.0 - decoder.Frames.Count / 10) * 1000))));
        anim.RepeatBehavior = RepeatBehavior.Forever;

        BitmapSource bitmapSource = decoder.Frames[0];

        Image1.Source = bitmapSource;
        Image1.Stretch = Stretch.None;

它显示图像正常,但不是动画。在这方面,我将不胜感激。

ABHI。

1 个答案:

答案 0 :(得分:0)

辛苦劳作一天后,我采用了不同的方法。我没有使用gif图像,而是按如下方式创建了一个用户控件: -

<UserControl.Resources>
    <Color x:Key="FilledColor" A="255" B="112" R="147" G="160"/>
    <Color x:Key="UnfilledColor" A="0" B="112" R="147" G="160"/>

    <Storyboard x:Key="Animation0" FillBehavior="Stop" BeginTime="00:00:00.0" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="_00" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
            <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard> 

    <Storyboard x:Key="Animation1" BeginTime="00:00:00.2" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="_01" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
            <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>

    <Storyboard x:Key="Animation2" BeginTime="00:00:00.4" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="_02" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
            <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard> 

    <Storyboard x:Key="Animation3" BeginTime="00:00:00.6" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="_03" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
            <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>

    <Storyboard x:Key="Animation4" BeginTime="00:00:00.8" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="_04" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
            <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>

    <Storyboard x:Key="Animation5" BeginTime="00:00:01.0" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="_05" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
            <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>

    <Storyboard x:Key="Animation6" BeginTime="00:00:01.2" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="_06" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
            <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>

    <Storyboard x:Key="Animation7" BeginTime="00:00:01.4" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="_07" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/>
            <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/>
        </ColorAnimationUsingKeyFrames> 
    </Storyboard>
</UserControl.Resources>

<UserControl.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard Storyboard="{StaticResource Animation0}"/>
        <BeginStoryboard Storyboard="{StaticResource Animation1}"/>
        <BeginStoryboard Storyboard="{StaticResource Animation2}"/>
        <BeginStoryboard Storyboard="{StaticResource Animation3}"/>
        <BeginStoryboard Storyboard="{StaticResource Animation4}"/>
        <BeginStoryboard Storyboard="{StaticResource Animation5}"/>
        <BeginStoryboard Storyboard="{StaticResource Animation6}"/>
        <BeginStoryboard Storyboard="{StaticResource Animation7}"/>
    </EventTrigger>
</UserControl.Triggers>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto" MinWidth="150" />
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Canvas Grid.Row="1" Grid.Column="1" Height="20" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Canvas.Resources>
            <Style TargetType="Ellipse">
                <Setter Property="Width" Value="15"/>
                <Setter Property="Height" Value="15" />
                <Setter Property="Fill" Value="#FFFFFFFF" />
            </Style>

            <Style TargetType="Rectangle">
                <Setter Property="Width" Value="12"/>
                <Setter Property="Height" Value="13" />
                <Setter Property="Fill" Value="#FFFFFFFF" />
                <Setter Property="RadiusX" Value="2" />
                <Setter Property="RadiusY" Value="2" />
            </Style>

        </Canvas.Resources>

        <Rectangle x:Name="_00" Canvas.Top="6" Canvas.Left="15"></Rectangle>
        <Rectangle x:Name="_01" Canvas.Top="6" Canvas.Left="30"></Rectangle>
        <Rectangle x:Name="_02" Canvas.Top="6" Canvas.Left="45"></Rectangle>
        <Rectangle x:Name="_03" Canvas.Top="6" Canvas.Left="60"></Rectangle>
        <Rectangle x:Name="_04" Canvas.Top="6" Canvas.Left="75"></Rectangle>
        <Rectangle x:Name="_05" Canvas.Top="6" Canvas.Left="90"></Rectangle>
        <Rectangle x:Name="_06" Canvas.Top="6" Canvas.Left="105"></Rectangle>
        <Rectangle x:Name="_07" Canvas.Top="6" Canvas.Left="120"></Rectangle>
    </Canvas>
<!--  <TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Foreground="#AAA" TextAlignment="Center" FontSize="15" Text="{Binding Source={StaticResource Model}, Path=StatusMessage}"/> -->
</Grid>

在usercontrol类中,我创建了如下方法: -

  public void Show()
    {
        this.Visibility = Visibility.Visible;
    }
    public void Hide()
    {
        this.Visibility = Visibility.Hidden;
    } 

This link向我指出了这个方向。