WPF bind baidu Image

Posted wgscd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF bind baidu Image相关的知识,希望对你有一定的参考价值。

 as there baidu image has protect refer from other site to use.

 need download i use request header add referer:http://www.baidu.com

<Window x:Class="WpfApplication1.WindMsg"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindMsg" Height="300" Width="300" Closing="Window_Closing" Loaded="Window_Loaded">
    <Grid>
        <Image Name="img" Stretch="Uniform" Source="{Binding BitmapImg}"/>
        <Button Content="{Binding Text}" Height="65" HorizontalAlignment="Left" Margin="58,48,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
    </Grid>
</Window>

 

 

 

 

 public WindMsg()
        {
            InitializeComponent();
            d = new ImageData() { Url = "http://img0.imgtn.bdimg.com/it/u=3028557787,2951839071&fm=15&gp=0.jpg", Text = "test button text" };
            this.DataContext = d;
        }
        ImageData d;
        public class ImageData
        {
            public string Url { get; set; }
            public string Text { get; set; }
            public ImageSource BitmapImg
            {
                get { return GetImageData(); }
                set { }
            }
            BitmapImage GetImageData()
            {
                using (WebClient wc = new WebClient())
                {
                    wc.Headers.Add("Referer", "http://www.baidu.com");
                    Stream stream = wc.OpenRead(Url);
                    BitmapImage b = new BitmapImage();
                    b.BeginInit();
                    b.StreamSource = stream;
                    b.EndInit();
                    return b;
                }
            }
        }

以上是关于WPF bind baidu Image的主要内容,如果未能解决你的问题,请参考以下文章

WPF自学入门WPF 初识Binding

WPF BINDING

理解WPF Binding CommandParameter =“{Binding}”

wpf程序,binding后界面啥都不显示,下面附上相关代码

wpf 中images控件binding显示图片后,图片文件会一直被占用吗?如果是该怎么解

WPF中Binding的技巧