WPF 图像 UriSource 和使用 http:\ URL 的数据绑定 [英] WPF Image UriSource and Data Binding using http:\ URL

查看:66
本文介绍了WPF 图像 UriSource 和使用 http:\ URL 的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 用户控件中显示带有 Web URL 的图像时遇到问题.我已经完成了 2008 年 8 月在本网站上提出的类似问题的所有建议(ImageUriSource 和数据绑定),但这些建议都没有奏效.

I am having a problem displaying an image with a web URL in a WPF user control. I have worked through all the suggestions for a similar problem asked on this site in Aug 2008 (Image UriSource and Data Binding) but none of those suggestions have worked.

我想做的是:

<Image Width="50" Name="MemberImage">
    <Image.Source>
        <BitmapImage DecodePixelWidth="50" UriSource="{Binding Member.ImageFilePathUri}" />
    </Image.Source>
</Image>

ImageFilePathUri 是从字符串路径创建的 Uri:

ImageFilePathUri is a Uri created from the string path through:

public Uri ImageFilePathUri
    {
        get
        {
            return new Uri(this.ImageFilePath);
        }
    }
}

这给出了必须设置属性 'UriSource' 或属性 'StreamSource'."错误如预期.

This gives the "Property 'UriSource' or property 'StreamSource' must be set." error as expected.

我也尝试过使用值转换器:

I have also tried using a value converter:

public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var image = new BitmapImage();
        image.BeginInit();
        if (value != null)
        {
            image.UriSource = new Uri((string)value);
        }
        image.DecodePixelWidth = 50;
        image.EndInit();
        return image;
    }
}

但是,绑定到它使用:

<Image Name="TestImage" Width="50" Source="{Binding Path=Member.ImageFilePath, Converter=Parliament.HansardApplicationSuite.Logging.Helpers.ImageConverter}"></Image>

不显示图像.

进一步尝试以编程方式加载图像,在控件构造函数和/或控件 Loaded 事件中也没有奏效:

A further attempt to load the image programmatically, in the control constructor and/or the control Loaded event have also not worked:

if (this.MemberRollItemViewModel.Member != null)
{
    var image = new BitmapImage();
    image.BeginInit();
    image.UriSource = new Uri(this.MemberRollItemViewModel.Member.ImageFilePath);
    image.DecodePixelWidth = 50;
    image.EndInit();

    this.MemberImage.Source = image;
}

有效的一件事是将图像保存到本地文件路径并显示:

The one thing that has worked is saving the image to a local file path and displaying that:

<Image Width="50" Name="MemberImage">
    <Image.Source>
        <BitmapImage DecodePixelWidth="50" UriSource="C:Data6bc64e7b-2df5-40d5-b6c4-eaf732318222.jpg" />
    </Image.Source>
</Image>

这显然只在调试问题时有用,而不是解决方案.相同的代码,但用 http 地址代替本地文件路径不起作用.

This is obviously only useful in debugging the problem and is not a solution. The same code but substituting the http address for the local file path doesn't work.

<Image.Source>
    <BitmapImage DecodePixelWidth="50" UriSource="http://member.org/6bc64e7b-2df5-40d5-b6c4-eaf732318222.jpg" />
</Image.Source>

更新:

这是 MemberImage 属性的实现.

This is the MemberImage property implementation.

public BitmapImage MemberImage
{
    get
    {
        var image = new BitmapImage();

        if (this.Member != null)
        {
            WebRequest request = WebRequest.Create(new Uri(this.Member.ImageFilePath, UriKind.Absolute));
            request.Timeout = -1;
            WebResponse response = request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            BinaryReader reader = new BinaryReader(responseStream);
            MemoryStream memoryStream = new MemoryStream();

            byte[] bytebuffer = new byte[BytesToRead];
            int bytesRead = reader.Read(bytebuffer, 0, BytesToRead);

            while (bytesRead > 0)
            {
                memoryStream.Write(bytebuffer, 0, bytesRead);
                bytesRead = reader.Read(bytebuffer, 0, BytesToRead);
            }

            image.BeginInit();
            memoryStream.Seek(0, SeekOrigin.Begin);

            image.StreamSource = memoryStream;
            image.EndInit();
        }

        return image;
    }
}

更新:

这就是我在视图中绑定到控件的方式:

This is how I am binding to the control in my view:

<Image Width="50" Source="{Binding MemberImage}" />

MemberImage 是我在上面给出的属性.我的数据上下文设置正确,因为该属性正在运行,只是没有返回图像.

MemberImage is the property I have given above. My data context is being set correctly because that property is being run, it's just not returning an image.

推荐答案

WebURL 不能作为 BitmapImage 的 UriSource 属性的源提供.如果是 weburl,则需要在本地下载该图像并将该路径绑定到 UriSource.见下文

WebURL can't be provided as a source to UriSource property of BitmapImage. If it is weburl you need to download that image locally and bind that path to UriSource. See the below

http://blogs.windowsclient.net/cennest/archive/2010/03/26/code-for-keeps-wpf-silverlight-retrieve-images-from-db-url.aspx

更新:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var image = new BitmapImage();
        int BytesToRead=100;

        WebRequest request = WebRequest.Create(new Uri("http://www.interweb.in/attachments/pc-wallpapers/16187d1222942178-nature-wallpaper-nature-summer-wallpaper.jpg", UriKind.Absolute));
        request.Timeout = -1;
        WebResponse response = request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        BinaryReader reader = new BinaryReader(responseStream);
        MemoryStream memoryStream = new MemoryStream();

        byte[] bytebuffer = new byte[BytesToRead];
        int bytesRead = reader.Read(bytebuffer, 0, BytesToRead);

        while (bytesRead > 0)
        {
            memoryStream.Write(bytebuffer, 0, bytesRead);
            bytesRead = reader.Read(bytebuffer, 0, BytesToRead);
        }

        image.BeginInit();
        memoryStream.Seek(0, SeekOrigin.Begin);

        image.StreamSource = memoryStream;
        image.EndInit();

        myImage.Source = image;
    }

这篇关于WPF 图像 UriSource 和使用 http:\ URL 的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆