轉換的淨位圖到SlimDx的Texture2D工作非常快這樣的: http://www.rolandk.de/index.php?option=com_content&view=article&id=65:bitmap-from-texture-d3d11&catid=16:blog&Itemid=10轉換SlimDX.Direct3D11的Texture2D到.NET位圖
private Texture2D TextureFromBitmap(FastBitmapSingle fastBitmap)
{
Texture2D result = null;
DataStream dataStream = new DataStream(fastBitmap.BitmapData.Scan0, fastBitmap.BitmapData.Stride * fastBitmap.BitmapData.Height, true, false);
DataRectangle dataRectangle = new DataRectangle(fastBitmap.BitmapData.Stride, dataStream);
try
{
Texture2DDescription dt = new Texture2DDescription
{
BindFlags = BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
Format = Format.B8G8R8A8_UNorm,
OptionFlags = ResourceOptionFlags.None,
MipLevels = 1,
Usage = ResourceUsage.Immutable,
Width = fastBitmap.Size.X,
Height = fastBitmap.Size.Y,
ArraySize = 1,
SampleDescription = new SampleDescription(1, 0),
};
result = new Texture2D(device, dt, dataRectangle);
}
finally
{
dataStream.Dispose();
}
return result;
}
對於正確的格式轉換的紋理回的.Net位圖我用的,但它是非常緩慢:
private bool BitmapFromTexture(FastBitmapSingle fastBitmap, Texture2D texture)
{
using (MemoryStream ms = new MemoryStream())
{
Texture2D.ToStream(device.ImmediateContext, texture, ImageFileFormat.Bmp, ms);
ms.Position = 0;
using (Bitmap temp1 = (Bitmap)Bitmap.FromStream(ms))
{
Rectangle bounds = new Rectangle(0, 0, temp1.Width, temp1.Height);
BitmapData BitmapDataIn = temp1.LockBits(bounds, ImageLockMode.ReadWrite, temp1.PixelFormat);
using (DataStream dataStreamIn = new DataStream(BitmapDataIn.Scan0, BitmapDataIn.Stride * BitmapDataIn.Height, true, false))
using (DataStream dataStreamOut = new DataStream(fastBitmap.BitmapData.Scan0, fastBitmap.BitmapData.Stride * fastBitmap.BitmapData.Height, false, true))
{
dataStreamIn.CopyTo(dataStreamOut);
}
temp1.UnlockBits(BitmapDataIn);
BitmapDataIn = null;
}
}
return true;
}
有一個更快的方法???我試過了,像這樣的:
但DataRectangle正好有8倍以上的數據,那麼我需要在我的數據流中:
private bool BitmapFromTexture(FastBitmapSingle fastBitmap, Texture2D texture)
{
using (Texture2D buff = Helper.CreateTexture2D(device, texture.Description.Width, texture.Description.Height, Format.B8G8R8A8_UNorm, BindFlags.None, ResourceUsage.Staging, CpuAccessFlags.Read | CpuAccessFlags.Write))
{
device.ImmediateContext.CopyResource(texture, buff);
using (Surface surface = buff.AsSurface())
using (DataStream dataStream = new DataStream(fastBitmap.BitmapData.Scan0, fastBitmap.BitmapData.Stride * fastBitmap.BitmapData.Height, false, true))
{
DataRectangle rect = surface.Map(SlimDX.DXGI.MapFlags.Read);
rect.Data.CopyTo(dataStream);
surface.Unmap();
}
}
return true;
}
任何人可以幫助嗎? 複製我的數據大約佔整個計算時間的50%。 如果這可以解決,我的應用程序會更快...
爲了保留PixelFormat和Dpi我使用我的Temp位圖,然後將PixelData複製到具有正確格式的圖像。 –