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,事情就是这样。

     

  • 相关阅读:
    Django REST framework
    django写入csv并发送邮件
    GC收集器ParNew&CMS
    编写高质量的JavaScript代码
    Vue3.0 declare it using the "emits" option警告
    vue 3.0 router 跳转动画
    vue3.0 element-plus 表格合并行
    element-plus 时间日期选择器 el-date-picker value-format 无效等
    vue3.0中使用,一个元素中是否包含某一个元素。
    vue axios ajax 获取后端流文件下载
  • 原文地址:https://www.cnblogs.com/shit/p/2244816.html
Copyright © 2011-2022 走看看