读取Datastream sharpDX错误所有值均为0

时间:2017-07-04 14:43:58

标签: c# sharpdx

我为我的项目遵循了这个解决方案:How to create bitmap from Surface (SharpDX)

我没有足够的声誉发表评论所以我在这里开了一个新问题。

我的项目基本上是Direct 2D,我有一个Surface缓冲区,一个交换链。我想将我的缓冲区放入数据流并读取它的值以将其放入位图并将其保存在磁盘上(如屏幕截图),但由于所有字节值均为0(黑色),我的代码将无法工作这是没有意义的,因为我的图像是完全白色的,有点蓝色。

这是我的代码:

SwapChainDescription description = new SwapChainDescription()
     {
        ModeDescription = new ModeDescription(this.Width, this.Height, new Rational(60, 1), Format.B8G8R8A8_UNorm),
        SampleDescription = new SampleDescription(1, 0),
        Usage = Usage.RenderTargetOutput,
        BufferCount = 1,
        SwapEffect = SwapEffect.Discard,
        IsWindowed = true,
        OutputHandle = this.Handle
     };

     Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, description, out device, out swapChain);

     SharpDX.DXGI.Device dxgiDevice = device.QueryInterface<SharpDX.DXGI.Device>();
     SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice.Adapter;

     SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice);
     d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);
     SharpDX.Direct3D11.DeviceContext d3DeviceContext = new SharpDX.Direct3D11.DeviceContext(device);




     properties = new BitmapProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
          96, 96);

     Surface backBuffer = swapChain.GetBackBuffer<Surface>(0);
     d2dTarget = new SharpDX.Direct2D1.Bitmap(d2dContext, backBuffer, properties);

     d2dContext.Target = d2dTarget;

     playerBitmap = this.LoadBitmapFromContentFile(@"C:\Users\ndesjardins\Desktop\wave.png");

     //System.Drawing.Bitmap bitmapCanva = new System.Drawing.Bitmap(1254, 735);

     d2dContext.BeginDraw();
     d2dContext.Clear(SharpDX.Color.White);
     d2dContext.DrawBitmap(playerBitmap, new SharpDX.RectangleF(0, 0, playerBitmap.Size.Width, playerBitmap.Size.Height), 1f, SharpDX.Direct2D1.BitmapInterpolationMode.NearestNeighbor);
     SharpDX.Direct2D1.SolidColorBrush brush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, SharpDX.Color.Green);
     d2dContext.DrawRectangle(new SharpDX.RectangleF(200, 200, 100, 100), brush);
     d2dContext.EndDraw();

     swapChain.Present(1, PresentFlags.None);


     Texture2D backBuffer3D = backBuffer.QueryInterface<SharpDX.Direct3D11.Texture2D>();

     Texture2DDescription desc = backBuffer3D.Description;
     desc.CpuAccessFlags = CpuAccessFlags.Read;
     desc.Usage = ResourceUsage.Staging;
     desc.OptionFlags = ResourceOptionFlags.None;
     desc.BindFlags = BindFlags.None;

     var texture = new Texture2D(device, desc);

     d3DeviceContext.CopyResource(backBuffer3D, texture);

     byte[] data = null;
     using (Surface surface = texture.QueryInterface<Surface>())
     {
        DataStream dataStream;
        var map = surface.Map(SharpDX.DXGI.MapFlags.Read, out dataStream);
        int lines = (int)(dataStream.Length / map.Pitch);
        data = new byte[surface.Description.Width * surface.Description.Height * 4];

        dataStream.Position = 0;
        int dataCounter = 0;
        // width of the surface - 4 bytes per pixel.
        int actualWidth = surface.Description.Width * 4;
        for (int y = 0; y < lines; y++)
        {
           for (int x = 0; x < map.Pitch; x++)
           {
              if (x < actualWidth)
              {
                 data[dataCounter++] = dataStream.Read<byte>();
              }
              else
              {
                 dataStream.Read<byte>();
              }
           }
        }
        dataStream.Dispose();
        surface.Unmap();

        int width = surface.Description.Width;
        int height = surface.Description.Height;
        byte[] bytewidth = BitConverter.GetBytes(width);
        byte[] byteheight = BitConverter.GetBytes(height);

        Array.Copy(bytewidth, 0, data, 0, 4);
        Array.Copy(byteheight, 0, data, 4, 4);

     }

你们有没有想过为什么最后返回的字节数组满为0,因为它应该大部分为255?我在后台填充的所有内容都是绘制位图图像和矩形图形。 Array.Copy是将宽度和高度标头添加到字节数组中,因此我可以创建一个位图。

1 个答案:

答案 0 :(得分:0)

我在评论中回答,但格式化很糟糕,所以道歉!

https://gamedev.stackexchange.com/a/112978/29920这看起来很有希望,但正如你在回答我的时候所说的那样,这是在不久前我已经凭空捏捏这个,如果它不适合有更多的人工作当前的知识将不得不回答,或者我必须抓住一些源代码并尝试自己。

SharpDX.Direct2D1.Bitmap dxbmp = new SharpDX.Direct2D1.Bitmap(renderTarget, 
new SharpDX.Size2(bmpWidth, bmpHeight), new 
BitmapProperties(renderTarget.PixelFormat));
dxbmp.CopyFromMemory(bmpBits, bmpWidth * 4);

这看起来有点像你需要的。我假设这里的bmpBits是一个字节数组或一个内存流,其中任何一个都可以保存下来或至少给你一些东西来看你是否真的得到像素数据