码迷,mamicode.com
首页 > Windows程序 > 详细

WPF bind baidu Image

时间:2018-03-26 17:26:40      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:sch   use   ams   tle   nta   orm   pos   ini   aml   

 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

标签:sch   use   ams   tle   nta   orm   pos   ini   aml   

原文地址:https://www.cnblogs.com/wgscd/p/8652147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!