我正在尝试生成一个简单的图像并将其显示在 WinRT/XAML 中。为此,我想要使用 SharpDX 绘制的图像的字节 []。到目前为止,我的方法似乎很好,但结果缓冲区是空的。
我也尝试了 CopyPixels,但它也只产生零。
有人可以指出我正确的方向吗?
private static byte[] Draw(TexRenderer trnder, int width, int height)
{
var wicFactory = new ImagingFactory();
var dddFactory = new SharpDX.Direct2D1.Factory();
var dwFactory = new SharpDX.DirectWrite.Factory();
var wicBitmap = new Bitmap(
wicFactory,
width,
height,
SharpDX.WIC.PixelFormat.Format32bppBGR,
BitmapCreateCacheOption.CacheOnLoad);
var renderTargetProperties = new RenderTargetProperties(
RenderTargetType.Default,
new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Unknown),
dddFactory.DesktopDpi.Width,
dddFactory.DesktopDpi.Height,
RenderTargetUsage.None,
FeatureLevel.Level_DEFAULT);
var renderTarget = new WicRenderTarget(
dddFactory,
wicBitmap,
renderTargetProperties)
{
TextAntialiasMode = TextAntialiasMode.Cleartype
};
renderTarget.BeginDraw();
var textFormat = new TextFormat(dwFactory, "Consolas", 48)
{
TextAlignment = TextAlignment.Center,
ParagraphAlignment = ParagraphAlignment.Center
};
var textBrush = new SolidColorBrush(
renderTarget,
Color.Blue);
renderTarget.Clear(Color.White);
renderTarget.DrawText(
"Hi, mom!",
textFormat,
new RectangleF(0, 0, width, height),
textBrush);
var bitmapRenderTarget = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.None);
trnder.Render(bitmapRenderTarget, 0, 0);
renderTarget.EndDraw();
var bitmaplock = wicBitmap.Lock(null, BitmapLockFlags.Read);
var dStream = new DataStream(bitmaplock.Data.DataPointer, bitmaplock.Stride * bitmaplock.Size.Height, true, true);
var buffer = new byte[bitmaplock.Stride * bitmaplock.Size.Height];
dStream.Write(buffer, 0, buffer.Length);
return buffer;
}