2014-05-12 46 views
0

我想加載一個DirectDraw表面(DDS)文件並將其顯示在WPF應用程序中。 這是我從ZIP檔案得到流:從流中加載DDS文件並顯示在WPF應用程序中?

using (ZipArchive clientArchive = ZipFile.OpenRead(levelsPath + mapName + @"\client.zip")) 
{ 
    var entry = clientArchive.GetEntry("hud/minimap/ingamemap.dds"); 
    var stream = entry.Open(); 
} 

現在,我該如何顯示在我的WPF應用程序的DDS圖像(只是第一個,最優質的紋理貼圖)?

回答

0
using (ZipArchive clientArchive = ZipFile.OpenRead(levelsPath + mapName + @"\client.zip")) 
{ 
    var entry = clientArchive.GetEntry("hud/minimap/ingamemap.dds"); 
    var stream = entry.Open(); 
    ImageObject.Source = DDSConverter.Convert(stream,null,null,null); 
} 

here.

public class DDSConverter : IValueConverter 
{ 
    private static readonly DDSConverter defaultInstace = new DDSConverter(); 

    public static DDSConverter Default 
    { 
     get 
     { 
      return DDSConverter.defaultInstace; 
     } 
    } 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value == null) 
     { 
      throw new ArgumentNullException("value"); 
     } 
     else if (value is Stream) 
     { 
      return DDSConverter.Convert((Stream)value); 
     } 
     else if (value is string) 
     { 
      return DDSConverter.Convert((string)value); 
     } 
     else if (value is byte[]) 
     { 
      return DDSConverter.Convert((byte[])value); 
     } 
     else 
     { 
      throw new NotSupportedException(string.Format("{0} cannot convert from {1}.", this.GetType().FullName, value.GetType().FullName)); 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(string.Format("{0} does not support converting back.", this.GetType().FullName)); 
    } 

    public static ImageSource Convert(string filePath) 
    { 
     using (var fileStream = File.OpenRead(filePath)) 
     { 
      return DDSConverter.Convert(fileStream); 
     } 
    } 

    public static ImageSource Convert(byte[] imageData) 
    { 
     using (var memoryStream = new MemoryStream(imageData)) 
     { 
      return DDSConverter.Convert(memoryStream); 
     } 
    } 

    public static ImageSource Convert(Stream stream) 
    { 
     ... 
    } 
} 

採取下面是一個簡單的使用例子:

<Image x:Name="ImageObject" Source="{Binding Source=Test.dds, Converter={x:Static local:DDSConverter.Default}}" /> 
+0

這個解決方案要求我安裝XNA 3.1(4.0,因爲一些方法將無法正常工作/類是不存在了),這又需要我安裝Visual C#Studio速成版2008(這是我不希望因爲我有VS2013)。這並不能解決我的問題。 – aPerfectMisterMan

2

我最近使用DDSImage類從kprojects。它可以加載DXT1和DXT5壓縮DDS文件。

只需創建一個字節數組的新實例並通過Bitmap[]類型的屬性images訪問所有的貼圖:

​​

進出口你有你的紋理貼圖爲位圖,你可以用一個Image - 控制顯示。要創建一個BitmapSource,基於內存位圖this answer指出我正確的方法。

相關問題