2
\$\begingroup\$

Does anyone know a way to get from (WPF) System.Windows.Media.Imaging.BitmapSource to a DX11 Texture2D without going to System.Drawing.Bitmap? I'm trying to strip out all System.Drawing references as this code it for WPF only.

\$\endgroup\$
0
\$\begingroup\$
public Texture2D Bitmap2Texture(Device device, BitmapSource source) {
  var bitmap = new WriteableBitmap(source);
  bitmap.Lock();
  var texture = new Texture2D(device, new Texture2DDescription() {
    Width = source.PixelWidth,
    Height = source.PixelHeight,
    ArraySize = 1,
    MipLevels = 1,
    BindFlags = BindFlags.ShaderResource,
    Usage = ResourceUsage.Default,
    CpuAccessFlags = CpuAccessFlags.None,
    Format = Format.R8G8B8A8_UNorm,
    OptionFlags = ResourceOptionFlags.None,
    SampleDescription = new SampleDescription(1, 0),
  }, new DataRectangle(bitmap.BackBuffer, bitmap.BackBufferStride));
  bitmap.Unlock();
  return texture;
}

You might need to change the texture description flags depending on what you need the texture for.

\$\endgroup\$
0
\$\begingroup\$

I use this code to load a bitmap source in, I dont use the system windows media library at all but the directx (it may leverage the very same library underneath).

This works in UWP which is what I assume you are looking for.

Hope this helps.

   public static D3D11.Texture2D1 CreateTex2DFromBitmap(SharpDX.WIC.BitmapSource bsource, D3D11.Device3 a_device, D3D11.DeviceContext2 a_context, D3D11.Texture2DDescription1 a_desc, int a_mipLevels)
    {
        D3D11.Texture2D1 t2d = null;

        try
        {
            int stride = bsource.Size.Width * 4;
            var buffer = new DataStream(bsource.Size.Height * stride, true, true);

            if (a_mipLevels != 0)
            {
                // if we arent doing mip maps, then load the resource directly
                DataRectangle rect = new DataRectangle(buffer.DataPointer, stride);
                bsource.CopyPixels(stride, buffer);
                t2d = new D3D11.Texture2D1(a_device, a_desc, rect); /// this creates a texture and populates it, we need to create a blank one then populate it.                
            }
            else
            {
                t2d = new D3D11.Texture2D1(a_device, a_desc); /// this creates a texture and populates it, we need to create a blank one then populate it.
                bsource.CopyPixels(stride, buffer);
                DataBox box = new DataBox(buffer.DataPointer, stride, 1);

                a_context.UpdateSubresource(box, t2d, D3D11.Resource.CalculateSubResourceIndex(0, 0, CountMips(bsource.Size.Width, bsource.Size.Height)));
            }
            buffer.Dispose();

        }
        catch (Exception ex)
        {
            ErrorHandler.DoErrorHandling(ex, ErrorHandler.GetCurrentMethod(ex), Environment.StackTrace);

        }
        return t2d;
    }
\$\endgroup\$
1
  • \$\begingroup\$ I'm being forced to load (WPF) System.Windows.Media.Imaging.BitmapSource to a Texture2D of which is put on a quad. \$\endgroup\$ – user96556 Sep 9 '17 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.