我正在尝试使用SharpDX.Direct2D和WicBitmap作为渲染目标来绘制位图。
我的代码如下所示:
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);我想要创造的是一个背景透明的图像。如果AlphaMode中的PixelFormat未知或忽略,Clear方法将忽略透明颜色的alpha通道。如果我试图将AlphaMode设置为直或预乘,那么WicRenderTarget构造函数就会失败,出现COM错误:参数不正确。
什么是正确的参数组合,使alpha通道在Clear方法中不被忽略?
发布于 2016-09-22 11:30:10
您已经使用不支持alpha通道的WicBitmap SharpDX.WIC.PixelFormat.Format32bppBGR创建了您的SharpDX.WIC.PixelFormat.Format32bppBGR。请使用Format32bppPBGRA或类似的,并将alpha模式设置为Pre相乘,它应该可以工作。
https://stackoverflow.com/questions/38484552
复制相似问题