在定位Win8.1 + Metro时使用SharpDX保存Texture2D吗? [英] Saving a Texture2D using SharpDX when targeting Win8.1 + Metro?

查看:78
本文介绍了在定位Win8.1 + Metro时使用SharpDX保存Texture2D吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户生成的Texture2D保存到磁盘,但是在将DirectX11.1 / Win8.1 / Metro定位为目标时,似乎没有实现此目的的标准方法。缺少ToStream / FromStream,也缺少用于将纹理写入磁盘的DX11方法。

I'm trying to save a user-generated Texture2D to disk, but it looks like the standard ways of accomplishing this aren't available when targeting DirectX11.1 / Win8.1 / Metro. ToStream/FromStream are absent, and the DX11 methods for writing a texture to disk are absent as well.

有什么想法或建议吗?

Any ideas or suggestions?

推荐答案

这是我最终得到的一般解决方案。通过映射纹理资源来获取DataStream就是正确的方法。

Here's the general solution I ended up with. Getting a DataStream through mapping a texture resource is what got me on the right track.

一般流程是使用CpuAccessFlags.Read创建纹理,然后从源纹理复制到新纹理,然后将数据从新纹理读入WIC位图。

General flow is create a texture with CpuAccessFlags.Read, then CopyResource from the source texture to the new texture, then read data from the new texture into a WIC Bitmap.

public static class Texture2DExtensions
{
    public static void Save(this Texture2D texture, IRandomAccessStream stream, DeviceManager deviceManager)
    {
        var textureCopy = new Texture2D(deviceManager.DeviceDirect3D, new Texture2DDescription
        {
            Width = (int)texture.Description.Width,
            Height = (int)texture.Description.Height,
            MipLevels = 1,
            ArraySize = 1,
            Format = texture.Description.Format,
            Usage = ResourceUsage.Staging,
            SampleDescription = new SampleDescription(1, 0),
            BindFlags = BindFlags.None,
            CpuAccessFlags = CpuAccessFlags.Read,
            OptionFlags = ResourceOptionFlags.None
        });
        deviceManager.ContextDirect3D.CopyResource(texture, textureCopy);

        DataStream dataStream;
        var dataBox = deviceManager.ContextDirect3D.MapSubresource(
            textureCopy,
            0,
            0,
            MapMode.Read,
            SharpDX.Direct3D11.MapFlags.None,
            out dataStream);

        var dataRectangle = new DataRectangle
        {
            DataPointer = dataStream.DataPointer,
            Pitch = dataBox.RowPitch
        };

        var bitmap = new Bitmap(
            deviceManager.WICFactory,
            textureCopy.Description.Width,
            textureCopy.Description.Height,
            PixelFormat.Format32bppBGRA,
            dataRectangle);

        using (var s = stream.AsStream())
        {
            s.Position = 0;
            using (var bitmapEncoder = new PngBitmapEncoder(deviceManager.WICFactory, s))
            {
                using (var bitmapFrameEncode = new BitmapFrameEncode(bitmapEncoder))
                {
                    bitmapFrameEncode.Initialize();
                    bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height);
                    var pixelFormat = PixelFormat.FormatDontCare;
                    bitmapFrameEncode.SetPixelFormat(ref pixelFormat);
                    bitmapFrameEncode.WriteSource(bitmap);
                    bitmapFrameEncode.Commit();
                    bitmapEncoder.Commit();
                }
            }
        }

        deviceManager.ContextDirect3D.UnmapSubresource(textureCopy, 0);
        textureCopy.Dispose();
        bitmap.Dispose();
    }
}

这篇关于在定位Win8.1 + Metro时使用SharpDX保存Texture2D吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆