2016-07-20 56 views
1

我正在嘗試使用SharpDX.Direct2D和WicBitmap作爲渲染目標來進行一些位圖繪製。
我的代碼如下所示:使用透明色清除RenderTarget

var wicFactory = new ImagingFactory(); 
var d2dFactory = new SharpDX.Direct2D1.Factory(); 

var wicBitmap = new SharpDX.WIC.Bitmap(wicFactory, width, height, 
    SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad); 
var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, 
    new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Unknown), 
    0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); 

var d2dRenderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties); 

PathGeometry path = new PathGeometry(d2dFactory); 
// Create the path figures here ... 

SolidColorBrush brush = new SolidColorBrush(d2dRenderTarget, new RawColor4(1, 0, 0, 1)); 
d2dRenderTarget.BeginDraw(); 
// The problem is with this line 
d2dRenderTarget.Clear(new RawColor4(1, 1, 1, 0)); 
d2dRenderTarget.DrawGeometry(path, brush, lineWidth); 
d2dRenderTarget.EndDraw(); 

brush.Dispose(); 

path.Dispose(); 

bl = wicBitmap.Lock(BitmapLockFlags.Read); 
// imageData is a byte[] 
Marshal.Copy(bl.Data.DataPointer, imageData, 0, imageData.Length); 
bl.Dispose(); 

d2dRenderTarget.Dispose(); 
wicBitmap.Dispose(); 
d2dFactory.Dispose(); 
wicFactory.Dispose(); 

File.WriteAllBytes("image.raw", imageData); 

我試圖創建是透明背景的圖像。如果PixelFormat中的AlphaMode爲「未知」或「忽略」,Clear方法會忽略清晰顏色中的Alpha通道。如果我嘗試將AlphaMode設置爲Straight或Premultiplied,則WicRenderTarget構造函數將失敗,並顯示COM錯誤:該參數不正確。

什麼是正確的參數組合,以便在Clear方法中不會忽略alpha通道?

回答

2

您已經使用SharpDX.WIC.PixelFormat.Format32bppBGR創建了不支持Alpha通道的WicBitmap。請使用Format32bppPBGRA或類似的並將alpha模式設置爲Premultiplied,它應該工作。