zoukankan      html  css  js  c++  java
  • image绑定bitmap( Binding Image.Source from download memory)

    首先 xaml前台image的source是用string表示的

    如:<image source="1.jpg"/>

    想当然地以为source="{Binding imagesource}",imagesource也是必须是string,结果闹了我一个下午。

    给后来人留点脚印,想想前者探索的艰辛啊。。

    首先看看这段代码

    <UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ListBoxSilde.UserControl1">
    <Grid x:Name="LayoutRoot">
    <Image Source="{Binding Image}" Stretch="None" x:Name="img"></Image>
    </Grid>
    </UserControl>

    cs部分:

    using System.Windows.Controls;

    namespace ListBoxSilde
    {
    public partial class UserControl1 : UserControl
    {
    Test t;

    public UserControl1()
    {
    InitializeComponent();
    t = new Test() { Image = "1.jpg" };
    img.DataContext = t;
    }
    }

    public class Test { public string Image { set; get; } }
    }

    再来看看另一种情况,要绑定的image是下载下来的byte[],没有路径,这时候

     <Image Stretch="None" Source="{Binding imageSource}" x:Name="img"></Image>

    cs:

     public class book//定义一个book类,需要绑定imagesource用ImageSource类型
    {
    public string bookname { get; set; }
    public ImageSource imagesource { get; set; }
    }
     void GetFirstImageCompleted(object sender,GetFirstImageCompletedEventArgs e)
    {
    ms = new MemoryStream(e.Result);//byte[]转stream
    BitmapImage image = new BitmapImage();
    image.SetSource(ms);
    book b = new book();
    b.imagesource =image;
    img.DataContext=b;//绑定对象
    }


    很显然,image.source绑定对象可以是ImageSource和string,事情就是这样。

     

  • 相关阅读:
    webpack
    react 原理
    jest
    input 自动获取焦点
    taro
    html5标签
    webpack
    每日日报
    每日日报
    每日日报
  • 原文地址:https://www.cnblogs.com/shit/p/2244816.html
Copyright © 2011-2022 走看看