1

我正在尝试加载 DirectDraw Surface (DDS) 文件并将其显示在 WPF 应用程序中。这就是我从 zip 存档中获取流的方式:

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

现在,如何在我的 WPF 应用程序中显示 DDS 图像(只是第一个,最高质量的 mipmap)?

4

2 回答 2

2

我最近使用了 kprojects 中的 DDSImage。它可以加载 DXT1 和 DXT5 压缩 DDS 文件。

只需使用字节数组创建一个新实例,并通过imagestype的属性访问所有 mipmap Bitmap[]

DDSImage img = new DDSImage(File.ReadAllBytes(@"e:\myfile.dds"));

for (int i = 0; i < img.images.Length; i++)
{
    img.images[i].Save(@"e:\mipmap-" + i + ".png", ImageFormat.Png);
} 

我将您的 mipmap 作为位图,您可以使用Image -Control 显示它。要创建一个 BitmapSource,基于内存中的 Bitmap,这个答案为我指出了正确的方法。

于 2014-09-27T17:38:06.870 回答
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);
}

取自这里。

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}}" />
于 2014-05-12T20:34:30.527 回答