使用SharpDX在WinRT中将图像生成为byte []

时间:2012-11-09 18:57:40

标签: xaml windows-runtime sharpdx

我试图生成一个简单的图像并在WinRT / XAML中显示它。 为此我想要用SharpDX绘制的图像的byte []。 到目前为止,我的方法似乎很好,但结果缓冲区是空的。

我也尝试过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;
    }

1 个答案:

答案 0 :(得分:0)

我遇到同样的问题,我需要访问物理原始纹理数据

DataStream dataStream = new DataStream(source.Size.Height * source.Size.Width, true, true);
bitmapSource.CopyPixels( source.Size.Width * sizeof(uint), dataStream);
var data = dataStream.ReadRange<uint>(source.Size.Width * source.Size.Height);

在论坛SharpDX forumsformore details