如何用SharpDX 4.2.0将Stream转为D3DImage并解决显示异常
在WPF应用中使用SharpDX 4.2.0将JPG格式byte[]显示到D3DImage
我需要在WPF应用中通过SharpDX 4.2.0把JPG格式的byte[]显示到D3DImage控件上,梳理出以下实现步骤:
补充说明:我已创建两个D3D11.Texture2D,一个用于写入像素数据,另一个用于共享给Direct3D9,代码可正常编译,但屏幕无任何显示,希望有人帮忙排查原因。
- 步骤1:将
Stream转换为SharpDX.Direct3D11.Texture2D - 步骤2:创建
SharpDX.Direct3D11.Texture2D(d3dTexture_Draw)并写入像素数据 - 步骤3:创建用于共享给
Direct3D9.Texture的SharpDX.Direct3D11.Texture2D(_finalTexture) - 步骤4:调用
d11dContext.CopyResource(d3dTexture_Draw, _finalTexture) - 步骤5:将
_finalTexture共享给Direct3D9.Texture - 步骤6:调用
D3DImage.SetBackBuffer设置Direct3D9.Texture的表面
更新说明:
@Simon Mourier:你的解决方案完全有效!
private void LoadBmpToTexture(string filePath) { // 将JPG加载为Bitmap using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(filePath)) { SharpDX.Direct3D11.Device device = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.None | DeviceCreationFlags.BgraSupport, SharpDX.Direct3D.FeatureLevel.Level_11_0); SharpDX.Direct3D11.DeviceContext d11dContext = device.ImmediateContext; byte[] byteArray = ConvertBitmapToByteArray(bitmap); MemoryStream memoryStream = new MemoryStream(byteArray); using (var imagingFactory = new SharpDX.WIC.ImagingFactory2()) { var decoder = new SharpDX.WIC.BitmapDecoder(imagingFactory, memoryStream, DecodeOptions.CacheOnLoad); var frame = decoder.GetFrame(0); var formatConverter = new FormatConverter(imagingFactory); // 将图像格式转换为32位PBGRA formatConverter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPBGRA); var width = formatConverter.Size.Width; var height = formatConverter.Size.Height; var textureDesc = new Texture2DDescription { Width = width, Height = height, MipLevels = 1, ArraySize = 1, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, }; var d3dTexture_Draw = new Texture2D(device, textureDesc); // 将像素数据复制到Texture2D var dataBox = d11dContext.MapSubresource(d3dTexture_Draw, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None); using (var wicStream = new DataStream(dataBox.DataPointer, width * height * 4, true, true)) { formatConverter.CopyPixels(width * 4, wicStream); } d11dContext.UnmapSubresource(d3dTexture_Draw, 0); var final_descriptor = new Texture2DDescription() { Width = (int)img.Width, Height = (int)img.Height, ArraySize = 1, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Usage = ResourceUsage.Default, CpuAccessFlags = CpuAccessFlags.None, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, MipLevels = 1, OptionFlags = ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0) }; Texture2D _finalTexture = new Texture2D(device, final_descriptor); d11dContext.CopyResource(d3dTexture_Draw, _finalTexture); SharpDX.Direct3D9.Direct3DEx d9context = new SharpDX.Direct3D9.Direct3DEx(); SharpDX.Direct3D9.Device d9device = new SharpDX.Direct3D9.Device(d9context, 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing, new SharpDX.Direct3D9.PresentParameters() { Windowed = true, SwapEffect = SharpDX.Direct3D9.SwapEffect.Discard, DeviceWindowHandle = new WindowInteropHelper(this).Handle, PresentationInterval = PresentInterval.Immediate, }); IntPtr renderTextureHandle = _finalTexture.QueryInterface<SharpDX.DXGI.Resource>().SharedHandle; SharpDX.Direct3D9.Texture d9texture = new SharpDX.Direct3D9.Texture(d9device, _finalTexture.Description.Width, _finalTexture.Description.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, Pool.Default, ref renderTextureHandle); d3dImage = new D3DImage(); d3dImage.Lock(); d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, d9texture.GetSurfaceLevel(0).NativePointer, true); // 设置脏区域:宽720,高370 d3dImage.AddDirtyRect(new Int32Rect(0, 0, 720, 370)); d3dImage.Unlock(); img.Source= d3dImage; } } }
内容的提问来源于stack exchange,提问作者Steve Park
相关产品推荐
相关产品推荐

