WPF中顯示任意目錄的圖片或其他資源文件


最近在做一個WPF的抽獎程序,需要顯示人員照片。做的時候把照片全都加到項目里了,作為內容,在ListBox或其他控件中綁定,可以正常顯示。但人員照片需要經常添加修改的,總不能每次都重新編譯項目吧。因此看了一下WPF中資源文件的引用方式,詳見 http://msdn.microsoft.com/zh-cn/library/aa970494.aspx 

WPF 應用程序資源、內容和數據文件

根據MSDN的資料:

Microsoft Windows 應用程序通常依賴包含不可執行數據的文件,如Extensible Application Markup Language (XAML)、圖像、視頻和音頻。 Windows Presentation Foundation (WPF) 為配置、識別和使用這些類型的數據文件(稱為應用程序數據文件)提供了特殊支持。 這種支持主要針對一組特定的應用程序數據文件類型,包括:

  • 資源文件:編譯到可執行或庫 WPF 程序集中的數據文件。

  • 內容文件:與可執行 WPF 程序集具有顯式關聯的獨立數據文件。

  • 源站點文件:與可執行 WPF 程序集沒有關聯的獨立數據文件。

這三種類型的文件之間的一個重要區別是:資源文件和內容文件在生成時是已知的;程序集明確地知道它們的存在。 但是對於源站點文件,程序集可能完全不知道它們,或者通過 pack uniform resource identifier (URI) 引用知道它們的存在;在后一種情況下,不能保證被引用的源站點文件實際存在。

目前所需的就是第三種情況,照片並不需要關聯到項目里,而是根據實際情況進行更新。因此綁定方式就需要做一下改動了。

簡單說一下代碼,首先是實體的Person類

public class Person
{
public string Name { get; set; }
public string Photo { get; set; }
}

然后讀取指定目錄下的照片列表,放到List列表里:(這里最開始的時候是寫了一個xml文件,后來一想這不是多此一舉么,直接讀文件目錄不就行了。)

/// <summary>
/// Inits the data.初始化人員數據
/// </summary>
protected void InitData()
{
//以下方法是從xml文件中讀取,為了方便,可以直接從目錄中讀取圖片列表
#region 從xml文件中讀取
//XmlDocument xdoc = new XmlDocument();
//xdoc.Load("Persons.xml");
//XmlElement root = xdoc.DocumentElement;
//foreach (XmlNode temp in root.ChildNodes)
//{
// Person tempPerson = new Person();
// tempPerson.Name = temp.FirstChild.Value;
// persons.Add(tempPerson);
//}
#endregion
string folderPath = "persons";
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(folderPath);
FileInfo[] fis = di.GetFiles();
if (fis == null)
{
MessageBox.Show("沒有圖片!");
App.Current.Shutdown();
}
else
{
foreach (FileInfo fi in fis)
{
Person tempPerson = new Person();
//這里直接獲取圖片文件名,包括擴展名,方便在圖片控件中直接綁定
tempPerson.Name = fi.Name.Substring(0, fi.Name.Length - 4);
tempPerson.Photo = @"persons/" + fi.Name;
persons.Add(tempPerson);
}
}

}

抽獎的時候隨機顯示人員照片及姓名:

xaml:

<StackPanel Name="pnlRandom" Orientation="Vertical" >
<Image Name="imgPerson" Width="300" Height="400" Source="{Binding Photo,Converter={StaticResource ImgConverter}}" Stretch="UniformToFill" Margin="0,10,0,0"></Image>
<Label x:Name="lblNameShow" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="100" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#fff45b"/>
</StackPanel>

之前綁定是直接寫的 Source="{Binding Photo} ,這樣也可以顯示,但必須把圖片加入到項目里設置成“內容”才能顯示出來。現在為了要達到顯示沒加入到項目里的圖片的目的,需要加一個Converter:

    [ValueConversion(typeof(string), typeof(string))]  
public class ImgConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//throw new NotImplementedException();
//string strSource = "/LuckyLottery;component/person/" + value + ".jpg";
//return strSource;
BitmapImage img = new BitmapImage();
//若要原始文件的站點,可以調用 Application 類的 GetRemoteStream 方法,同時傳遞標識原始文件的所需站點的 pack URI。 GetRemoteStream 將返回一個 StreamResourceInfo 對象,該對象將原始文件的該站點作為 Stream 公開,並描述其內容類型。
StreamResourceInfo info = Application.GetRemoteStream(new Uri(value.ToString(), UriKind.Relative));
img.BeginInit();
//img.UriSource = new Uri(value.ToString(), UriKind.Relative);
img.StreamSource = info.Stream;
img.EndInit();
return img;
}

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

需要 using System.Windows.Resources;

這樣圖片就可以顯示出來了。而且可以隨意添加刪除更新圖片。

詳細內容可參看MSDN:http://msdn.microsoft.com/zh-cn/library/aa970494.aspx

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM