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

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

问题描述

我有一个WPF用户控件的Web URL显示图像的问题。我已经完成了所有工作的建议在2008年8月在本网站问过类似的问题(http://stackoverflow.com/questions/20586/wpf-image-urisource-and-data-binding),但没有这些建议已经工作

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 (http://stackoverflow.com/questions/20586/wpf-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;
    }
}



不过,使用绑定到它:

However, binding to it using:

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



不显示图像。

doesn't display the image.

进一步企图以编程方式加载图像,在控制构造函数和/或控件加载事件也没有工作:

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:\Data\6bc64e7b-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>



更​​新:

Update:

这是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;
    }
}



更​​新:

Update:

这是我如何绑定到我的视图控件:

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属性来提供。如果你需要在本地下载图像并绑定该路径来UriSource WEBURL。请参见下面的

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-从-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绑定:\\网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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