c# - 从Texture2D获取SharpDX.Direct2D1.Bitmap

标签 c# directx sharpdx

我正在尝试创建一个离屏位图来在其上绘制,然后使用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);

最佳答案

  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;
        }
    }

关于c# - 从Texture2D获取SharpDX.Direct2D1.Bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107494/

相关文章:

c# - 从 SlimDX 迁移到 SharpDX - 效果

c# - SinkWriter.WriteSample() 失败并返回 E_INVALIDARG

c# - 禁用 ASP.NET Core 输出路径中的 'RuntimeIdentifier' 子文件夹

wpf - D3DImage 和 SharpDX 在慢速硬件上闪烁

c# - 如何在 C# 中确定当前关注的进程名称和版本

directx - Direct3D 中的矩阵多阶

2d - Skia vs Cairo vs Direct2D,哪个功能最丰富?

c++ - 将纹理数组发送到 DirectX 11 中的着色器

c# - VSTO 中的加载项 - 如何使用带按钮的功能区从 Word 文档中获取文本

c# - 我如何在托管 C++ 中执行 typeof(int)?