SharpDX: Render to bitmap using Direct2D 1.1

Posted by mwhouser on Game Development See other posts from Game Development or by mwhouser
Published on 2013-11-08T19:57:29Z Indexed on 2013/11/08 22:18 UTC
Read the original article Hit count: 3487

Filed under:
|

I have a command line application that I am currently using SharpDX (Direct2D 1.0) to render to PNG files. This is a window-less application.

It's currently creating a SharpDX.WIC.WicBitmap, a WicRenderTarget, then rendering to that. I then save the WicBitmap to the PNG file.

For various reasons, I need to migrate to Direct2D 1.1 to take advantage of some of the effects available in 1.1.

I'm trying to get a SharpDX.Direct2D1.Bitmap that I can save as PNG.

I cannot use FromWicBitmap because that copies the bitmap, it does not share it.

I see CreateSharedBitmap in the Direct2D1 API that takes a IWICBitmapLock. However, I do not see this implemented as a constructor of SharpDX.Direct2D.Bitmap.

This is what I'm trying to do:

// Bunch of setup
var d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice);

var d2dDeviceContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice,
    SharpDX.Direct2D1.DeviceContextOptions.None);

using (var wicFactory = new SharpDX.WIC.ImagingFactory())
{
  using (SharpDX.WIC.Bitmap wicBitmap = 
    new SharpDX.WIC.Bitmap(wicFactory, 500, 500, 
      SharpDX.WIC.PixelFormat.Format32bppPBGRA, 
      SharpDX.WIC.BitmapCreateCacheOption.CacheOnDemand))
    {
    var wicLock = wicBitmap.Lock(SharpDX.WIC.BitmapLockFlags.Write);

    var props = new SharpDX.Direct2D1.BitmapProperties1();
    props.BitmapOptions = SharpDX.Direct2D1.BitmapOptions.Target;

    var bitmap = new SharpDX.Direct2D1.Bitmap1(d2dDeviceContext, wicLock, props); // This is not available

    d2dDeviceContext.Target = bitmap;

    // Do the drawing
    // Save the PNG
  }
}

Is there a way to do what I'm trying to accomplish?

© Game Development or respective owner

Related posts about c#

Related posts about sharpdx