使用数据库中的URL链接在WPF中显示图像

use*_*998 6 c# url wpf image path

我像这样在数据库中保存了URL

〜/图片/问题/ drink.png

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

            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);
Run Code Online (Sandbox Code Playgroud)

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

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

当我使用

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

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

Tim*_*hyP 16

您正在使用UrlKind.Absolute指定UriKind.Relative
因为您可能正在从数据库加载完整的URL,例如

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

而UriKind.Relative将用于类似的东西

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

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

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);
Run Code Online (Sandbox Code Playgroud)

没有必要将image.Width和Image.Height设置为Double.Nan

边注.虽然你可以在运行时像这样加载图像,但最好使用WPF数据绑定(最好使用像MVVM这样的东西)

基本上你有一个带有WrapPanel的ListBox作为ItemsPanelTemplate然后将ItemsSource设置为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>
Run Code Online (Sandbox Code Playgroud)

您将图像绑定到表示Path的任何属性,并使用ValueConverter来规范化路径.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

代码只是一种让你知道要进入哪个方向的方法.关键是你可能想要查找WPF数据绑定而不是使用代码.