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

     缓冲图片,延迟加载

    // Create source
    BitmapImage myBitmapImage = new BitmapImage();
    // BitmapImage.UriSource must be in a BeginInit/EndInit block
    myBitmapImage.BeginInit();
    myBitmapImage.UriSource = new Uri(str, UriKind.Absolute);
    // To save significant application memory, set the DecodePixelWidth or  
    // DecodePixelHeight of the BitmapImage value of the image source to the desired 
    // height or width of the rendered image. If you don't do this, the application will 
    // cache the image as though it were rendered as its normal size rather then just 
    // the size that is displayed.
    // Note: In order to preserve aspect ratio, set DecodePixelWidth
    // or DecodePixelHeight but not both.
    myBitmapImage.DecodePixelWidth = 2048;
    myBitmapImage.EndInit();
    //set image source
  • 相关阅读:
    linux之awk命令
    HDU 2097 Sky数 进制转换
    HDU 2077 汉诺塔IV
    HDU 2094 产生冠军 dfs加map容器
    HDU 2073 叠框
    HDU 2083 简易版之最短距离
    HDU 2063 过山车 二分匹配
    天梯 1014 装箱问题
    天梯 1214 线段覆盖
    天梯 1098 均分纸牌
  • 原文地址:https://www.cnblogs.com/wgscd/p/8652147.html
Copyright © 2020-2023  润新知