使用数据库中的URL链接在WPF中显示图像 [英] Showing image in WPF using the URL link from database

查看:490
本文介绍了使用数据库中的URL链接在WPF中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样将URL保存在数据库中

i saved the URL in the database like this

〜/Images/Questions/drink.png

~/Images/Questions/drink.png

因此,当我在WPF应用程序中检索它时,我试图这样做:

So when i retrieve it in my WPF application i tried to do this :

            Image img = new Image();
            img.Width = Double.NaN;
            img.Height = Double.NaN;

    //      string fullFilePath = Path.Combine(@"C:\Users\apr13mpsip\Documents\Visual Studio 2010\Projects\iStellarMobile\iStellarMobile\Images\Questions", lstQuestion[i].ImageURL.Substring(1));
            string fullFilePath = @"C:\Users\apr13mpsip\Documents\Visual Studio 2010\Projects\iStellarMobile\iStellarMobile\Images\Questions\drink.png";
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(fullFilePath, UriKind.Relative); 
            bi.EndInit();


            img.Source = bi;
            wrapPanel1.Children.Add(img);

lstQuestion [i] .ImageURL是我从数据库检索的URL.但是它不起作用...当我运行它时它什么也不显示,所以我尝试通过手动输入整个目录来尝试完整路径,但是它仍然不起作用,我在这里出了什么问题?

the lstQuestion[i].ImageURL is the URL that i retrieve from database. but it won't work ... it display nothing when i run it , so i tried the full path by typing in manually the whole directory but it still won't work , What have i gone wrong here?

当我调试它时,它仅显示Images \ Questions \ drink.png而不是完整路径

When i debug it , it only shows Images\Questions\drink.png instead of the full path

当我使用

Path.Combine(@"C:\ Users \ apr13mpsip \ Documents \ Visual Studio 2010 \ Projects \ iStellarMobile \ iStellarMobile", lstQuestion [i] .ImageURL.Substring(1));

Path.Combine(@"C:\Users\apr13mpsip\Documents\Visual Studio 2010\Projects\iStellarMobile\iStellarMobile", lstQuestion[i].ImageURL.Substring(1));

,它说无法确定URL,而当我调试URL时,它仅显示为Images \ Questions \ drink.png而不是完整路径.

, it says URL could not be determined and when i debug it , it only read as Images\Questions\drink.png instead of the full path.

推荐答案

您在指定UriKind.Relative的同时应使用UrlKind.Absolute
例如,由于您可能正在从数据库加载完整的URL,

You are specifying UriKind.Relative while you should be using UrlKind.Absolute
Since you are probably loading a complete url from the database, for example

http://www.americanlayout.com/wp/wp-content/uploads/2012/08/C-To-Go-300x300.png

而UriKind.Relative则用于类似

Whereas UriKind.Relative would be used for something like

/wp/wp-content/uploads/2012/08/C-To-Go-300x300.png

/wp/wp-content/uploads/2012/08/C-To-Go-300x300.png

在任何情况下,以下代码均有效:

In any case the following code works:

var image = new Image();
var fullFilePath = @"http://www.americanlayout.com/wp/wp-content/uploads/2012/08/C-To-Go-300x300.png";

BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(fullFilePath, UriKind.Absolute);
bitmap.EndInit();

image.Source = bitmap;
wrapPanel1.Children.Add(image);

无需设置图像.宽度& Image.Height到Double.Nan

There is no need to set image.Width & Image.Height to Double.Nan

旁注.您当然可以像这样在运行时加载图像, 最好使用WPF数据绑定(最好使用MVVM之类的东西)

Side note. While you can certainly load images at runtime like this, it would be better to use WPF Databinding (preferably with something like MVVM)

基本上,您将拥有一个带有WrapPanel的ListBox作为ItemsPanelTemplate 然后将ItemsSource设置为您的列表(lstQuestions).

Basically you'd have a ListBox with a WrapPanel as ItemsPanelTemplate Then set the the ItemsSource to your List (lstQuestions).

<ListBox ItemsSource={Binding lstQuestions}>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding Path, Converter={StaticResource MyPathConverter}}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您会将图像绑定到表示Path的任何属性, 使用ValueConverter标准化路径.

You'd bind the image to whatever property represents the Path and use a ValueConverter to normalize the path.

public class PathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string path = value.ToString();
        if (path.StartsWith("\\")
            path = path.Substring(1);

        return Path.Combine("whateveryourbasepathis", path);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

代码只是一种让您了解前进方向的方法. 关键是您可能希望查找WPF数据绑定,而不是使用代码.

The code is just a way to give you an idea which direction to go in. The point is you might want to look up WPF databinding as opposed to doing it with code.

这篇关于使用数据库中的URL链接在WPF中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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