I'm trying to generate (draw/render) a simple image and display it in WinRT/XAML. For that I want the byte[] of an image drawn with SharpDX. My Approach so far seems fine but the resulting buffer is empty.
I tried CopyPixels as well but it also only produced zeros.
Can somebody point me in the right direction?
- - -
My current Approach:
private static byte[] Approach7(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(
"Hello World!",
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;
}