【问题标题】:Load DDS file from stream and display in WPF application?从流中加载 DDS 文件并在 WPF 应用程序中显示?
【发布时间】:2014-06-30 08:31:20
【问题描述】:

我正在尝试加载 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)?

【问题讨论】:

    标签: c# .net wpf .net-4.5 dds-format


    【解决方案1】:

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

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

    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 this answer 指出了正确的方法。

    【讨论】:

      【解决方案2】:
      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}}" />
      

      【讨论】:

      • 此解决方案要求我安装 XNA 3.1(4.0 将无法工作,因为某些方法/类不再存在)这反过来又要求我安装 Visual C# Studio Express Edition 2008(我不想要,因为我有 VS2013)。这并不能解决我的问题。
      猜你喜欢
      • 2016-08-30
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      相关资源
      最近更新 更多