C# 从Texture2D获取SharpDX.Direct2D1.Bitmap

C# 从Texture2D获取SharpDX.Direct2D1.Bitmap,c#,directx,sharpdx,C#,Directx,Sharpdx,我正在尝试创建一个屏幕外位图来绘制它,然后使用Direct2D1.RenderTarget.DrawBitmap来绘制它。因此,我创建了Texture2D,并从中获取位图。但是我收到了错误 [D2DERR_UNSUPPORTED_PIXEL_FORMAT/UnsupportedPixelFormat] 在代码的最后一个字符串中。请帮助我理解,我做错了什么 m_texture = new Texture2D( context.Device,

我正在尝试创建一个屏幕外位图来绘制它,然后使用
Direct2D1.RenderTarget.DrawBitmap
来绘制它。因此,我创建了
Texture2D
,并从中获取位图。但是我收到了错误

[D2DERR_UNSUPPORTED_PIXEL_FORMAT/UnsupportedPixelFormat] 
在代码的最后一个字符串中。请帮助我理解,我做错了什么

    m_texture = new Texture2D(
            context.Device,
            new Texture2DDescription() {
                ArraySize = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.B8G8R8A8_UNorm,
                Height = bitmapSize.Height,
                Width = bitmapSize.Width,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription() {
                    Count = 1,
                    Quality = 0
                },
                Usage = ResourceUsage.Default
            }
        );

        m_surface = m_texture.QueryInterface<Surface>();

        using (SharpDX.Direct2D1.Factory factory = new SharpDX.Direct2D1.Factory()) {
            m_renderTarget = new RenderTarget(
                factory,
                m_surface,
                new RenderTargetProperties() {
                    DpiX = 0.0f, // default dpi
                    DpiY = 0.0f, // default dpi
                    MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
                    Type = RenderTargetType.Hardware,
                    Usage = RenderTargetUsage.None,
                    PixelFormat = new SharpDX.Direct2D1.PixelFormat(
                        Format.Unknown,
                        AlphaMode.Premultiplied
                    )
                }
            );
        }

        m_bitmap = new SharpDX.Direct2D1.Bitmap(m_renderTarget, m_surface);
m_纹理=新纹理2d(
上下文。设备,
新的Texture2DDescription(){
ArraySize=1,
BindFlags=BindFlags.RenderTarget | BindFlags.ShaderResource,
CpuAccessFlags=CpuAccessFlags.None,
格式=Format.B8G8R8A8_UNorm,
高度=位图大小。高度,
宽度=位图大小。宽度,
MIP级别=1,
OptionFlags=ResourceOptionFlags.None,
SampleDescription=新的SampleDescription(){
计数=1,
质量=0
},
用法=ResourceUsage.Default
}
);
m_surface=m_texture.QueryInterface();
使用(SharpDX.Direct2D1.Factory Factory=new SharpDX.Direct2D1.Factory()){
m_renderTarget=新的renderTarget(
工厂,
m_表面,
新的RenderTargetProperties(){
DpiX=0.0f,//默认dpi
DpiY=0.0f,//默认dpi
MinLevel=SharpDX.Direct2D1.FeatureLevel.Level\u默认值,
类型=RenderTargetType.Hardware,
用法=RenderTargetUsage.None,
PixelFormat=新的SharpDX.Direct2D1.PixelFormat(
格式。未知,
字母模式。预乘
)
}
);
}
m_bitmap=new SharpDX.Direct2D1.bitmap(m_renderTarget,m_surface);
公共静态SharpDX.Direct2D1.Bitmap GetBitmapFromSRV(SharpDX.Direct3D11.ShaderResourceView srv,RenderTarget RenderManager)
{
使用(var texture=srv.ResourceAs())
使用(var surface=texture.QueryInterface())
{
var bitmap=new SharpDX.Direct2D1.bitmap(渲染器、曲面、new SharpDX.Direct2D1.BitmapProperties(new SharpDX.Direct2D1.PixelFormat(
Format.R8G8B8A8_UNorm,
SharpDX.Direct2D1.AlphaMode.Premultiplied));
返回位图;
}
}

您不应该指定像素格式吗?如果我应该这样做,我在哪里可以这样做?事实上,我已经找到了解决办法。我需要从这个RenderTarget创建BitmapRenderTarget,然后从中获取Bitamp。它是这样工作的,但是如果你使用未分类的像素格式,并且你将你的像素格式声明为format.Unknown,我无法确切地理解我之前做错了什么。您可以尝试以下格式之一:……乍一看,这就是我的假设。但是如果你有一个有效的解决方案,那就太棒了!我一直在尝试m_纹理使用的相同像素格式,但结果是一样的。所以我选择了Unknown,因为系统选择的格式与m_surface使用的格式相同。实际上,问题是:为什么我要创建BitmapRenderTarget而不是简单地使用RenderTarget?BitmapRenderTarget和RenderTarget在公共和较低级别实现中的区别是什么?位图构造函数真正做什么?它们应该如何使用?
  public static SharpDX.Direct2D1.Bitmap GetBitmapFromSRV(SharpDX.Direct3D11.ShaderResourceView srv, RenderTarget renderTarger)
    {
        using (var texture = srv.ResourceAs<Texture2D>())
        using (var surface = texture.QueryInterface<Surface>())
        {
            var bitmap = new SharpDX.Direct2D1.Bitmap(renderTarger, surface, new SharpDX.Direct2D1.BitmapProperties(new SharpDX.Direct2D1.PixelFormat(
                                                      Format.R8G8B8A8_UNorm,
                                                      SharpDX.Direct2D1.AlphaMode.Premultiplied)));
            return bitmap;
        }
    }