2013-12-16 57 views
0

我想从URL显示图像,下面是我的代码。通过这种方式,我可以在本地显示任何图像,但从远程服务器不工作。 我的主窗口中的XAML代码: WPF在picturebox中显示来自http请求的图像

<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; } 

    } 
+0

上面的代码工作(我使用WPF 4.5)。是否有可能存在网络问题?尝试连接到'Image.ImageFailed'事件。 –

+0

什么破解:)我有我的互联网速度问题。感谢您的提示兄弟。 –

回答

0

如何....

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

谢谢,我解决了它。问题只是在我的互联网下载速度。而且,我正在使用可绑定的源代码。无论如何谢谢。 –

相关问题