WPF在picturebox中显示来自http请求的图像

时间:2013-12-16 07:03:34

标签: c# wpf xaml

我想显示来自网址的图片,下面是我的代码。通过这种方式,我可以在本地显示任何图像,但是从远程服务器它不能正常工作。 我的代码在mainWindow xaml中:

    <ScrollViewer Name="contentScrolViewer" Grid.Row="1">
        <ItemsControl Name="ImageList" SnapsToDevicePixels="True">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}" >
                    </WrapPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Height="311" Width="171" HorizontalAlignment="Left" Margin="10">
                        <Grid Height="225" Margin="0.5,0">
                            <Rectangle Fill="#FF0B0B1F" Stroke="Black" StrokeThickness="0"/>
                            <Image Source="{Binding image}" Width="171" Height="311" Stretch="Fill"/>
                        </Grid>
                        <Rectangle Fill="#FF211F1A" Stroke="Black" Height="85" StrokeThickness="0" Margin="0,0,0.5,0"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

我的代码在mainWindow.xaml.cs

  public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            List<ImageItems> items = new List<ImageItems>();
            items.Add(new ImageItems() { image = "http://diyaotheos.files.wordpress.com/2010/03/avatar_wallpaper_by_nyah86.png" });
            ImageList.ItemsSource = items;
        }
    }
    public class ImageItems
    {
        public String image { get; set; }

    }

1 个答案:

答案 0 :(得分:0)

怎么样......

<Image Source="http://www.wpclipart.com/recreation/games/chess/chess_set_1/chess_piece_white_knight_T.png" />