ある私は私のプロジェクトのために、このソリューションに続く:私はので、私はここに新しい質問を開いてるコメントするのに十分な評判を持っていないHow to create bitmap from Surface (SharpDX)読書データストリームsharpDXエラーすべての値が0
を。
私のプロジェクトは基本的にはDirect 2Dです。サーフェスバッファー、スワップチェーンがあります。私はバッファをデータストリームに入れて、それをビットマップに入れてディスクに保存する(画面キャプチャのように)値を読み込みますが、すべてのバイト値が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);
}
は、あなたたちは、それはほとんどが255でなければなりませんので、最後に返されるバイト配列は0に満ちている理由を任意のアイデアを持っていますか?バックバッファーで行ったのは、ビットマップイメージと四角形を描画することでした。 Array.Copyは、幅と高さのヘッダーをバイト配列に追加するため、ビットマップを作成することができます。
ありがとう、copyFromMemoryはまさにトリック!また、System.Drawing.BitmapのビットマップをsharpDX.Direct2D1.Bitmapに5ms未満で変換することが可能かどうか疑問に思っていましたか? –
わかりません。私の前回の返信のリンクは、SharpDXビットマップを読み込んで何かをしています(私は思う)ので、あなたは創造的にする必要があります。ビットマップをすばやく読み込む必要がある場合は、非同期で行うことができますか?ファイルから読み込んで、それをメインスレッドに渡す前にバイト配列(またはSharpDXが必要とするもの)に変換しますか?もちろんそれほど時間はかかりませんが、ビジネスの過程でメインスレッドを解放することができます - これはオプションですか? 上記の回答に満足している場合は、そのようにマークすることもできますか?それは将来他の誰かを助けることができる:) –