2

I am currently making a 2D application using SharpDX.Direct2D1.

Here is my setup for my 2D deviceContext that works correctly.

m_swapchaindesc = New SwapChainDescription()
    With m_swapchaindesc
        .BufferCount = 2
        .ModeDescription =
            New ModeDescription(control.Width, control.Height,
                                New Rational(60, 1), Format.R8G8B8A8_UNorm)
        .IsWindowed = True
        .OutputHandle = control.Handle
        .SampleDescription = New SampleDescription(1, 0)
        .SwapEffect = SwapEffect.Discard
        .Usage = Usage.RenderTargetOutput
    End With

    SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport Or DeviceCreationFlags.Debug, m_swapchaindesc, m_device, m_swapChain)

    Dim dxgiDevice As SharpDX.DXGI.Device = m_device.QueryInterface(Of SharpDX.DXGI.Device)()

    m_2dDevice = New SharpDX.Direct2D1.Device(dxgiDevice)
    m_d2dContext = New SharpDX.Direct2D1.DeviceContext(m_2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None)

    m_properties = New BitmapProperties(New SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), 96, 96)

    m_backBuffer = m_swapChain.GetBackBuffer(Of Surface)(0)

    m_2dTarget = New SharpDX.Direct2D1.Bitmap(m_d2dContext, m_backBuffer, m_properties)
    m_d2dContext.Target = m_2dTarget

My question : I want to draw 8bpp bitmap on a grayscale to my deviceContext, which means I don't need it to be 32bits per pixel which takes a loooot of memory. I would like for it to be only 8 bits for memory purpose. The problem when I do so, which means setting up my BitmapProperties, there is only a Format.R8_Unorm Or Format.A8_Unorm that seems interesting since I guess they only have 8bpp. However, when I put this format and I am trying to instantiate my SharpDX.Direct2D1.Bitmap, it returns an error message that says WRONGPIXELFORMAT.

I realised that only Format.R8G8B8A8_Unorm seems to work when creating bitmap, which I don't need.

Have any of you worked with 8bpp grayscale bitmap on SharpDX ?

petithomme
  • 509
  • 1
  • 8
  • 29

1 Answers1

0

You cannot use 8BPP bitmaps with SharpDX (DirectX). This is not a supported format by DirectX. To be able to display a grayscale image, You have to set all values of yours rgb to the same one, or apply a shaders to your image that converts all colors to gray.