当前位置:首页 > Windows程序 > 正文

WPF bind baidu Image

2024-03-31 Windows程序

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

need download i use request header add referer:

<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" Closing="Window_Closing" Loaded="Window_Loaded">

    <Grid>

        <Image Stretch="Uniform" Source="{Binding BitmapImg}"/>

        <Button Content="{Binding Text}" HorizontalAlignment="Left" Margin="58,48,0,0" VerticalAlignment="Top" />

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

                }

            }

        }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/34035.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494587访问次数
  • 建站天数
  • 友情链接