Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF上带SharpDX的DirectX实时图形_C#_.net_Graphics_Directx_Sharpdx - Fatal编程技术网

C# WPF上带SharpDX的DirectX实时图形

C# WPF上带SharpDX的DirectX实时图形,c#,.net,graphics,directx,sharpdx,C#,.net,Graphics,Directx,Sharpdx,我在WPF应用程序中使用SharpDX上的DirectX实现了实时图形。我使用Direct2D1,直接合成,SwapCain。它可以完美地处理窗口上的一个对象,但如果我添加了多个对象(9),则会出现问题:在调试过程中,在1-2分钟后,或者如果我在监视器之间主动移动窗口,我会捕获访问冲突异常或删除的设备,在发布版本中,我会在部署后捕获一次。我尝试删除后重新创建设备,但它不工作 public class DirectX { public BitmapProperties1 B

我在WPF应用程序中使用SharpDX上的DirectX实现了实时图形。我使用Direct2D1,直接合成,SwapCain。它可以完美地处理窗口上的一个对象,但如果我添加了多个对象(9),则会出现问题:在调试过程中,在1-2分钟后,或者如果我在监视器之间主动移动窗口,我会捕获访问冲突异常或删除的设备,在发布版本中,我会在部署后捕获一次。我尝试删除后重新创建设备,但它不工作

public class DirectX
    {
        public BitmapProperties1 BitmapProperties1 { get; private set; }
        public SharpDX.Direct2D1.Factory1 Factory1 { get; private set; }
        private SharpDX.DXGI.Device _genericDevice;
        private SharpDX.DirectComposition.Visual _visual;
        private Target _target;
        private SharpDX.DirectComposition.Device _compDevice;
        private SharpDX.Direct3D11.Device d11Device;
        private IntPtr _hwnd;
        public Result? DeviceRemovedReason => d11Device?.DeviceRemovedReason;
        public DirectX(Window window)
        {
            _hwnd = new WindowInteropHelper(window).Handle;
            InitilizeDevices();
        }
        private bool _isRefreshed = false;
        public void InitilizeDevices()
        {
            var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
            var factory1Flags = DebugLevel.None;
#if DEBUG
            creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
            factory1Flags = DebugLevel.Information;
#endif
            SharpDX.Direct3D.FeatureLevel[] featureLevels =
            {
                SharpDX.Direct3D.FeatureLevel.Level_11_1,
                SharpDX.Direct3D.FeatureLevel.Level_11_0,
                SharpDX.Direct3D.FeatureLevel.Level_10_1,
                SharpDX.Direct3D.FeatureLevel.Level_10_0,
                SharpDX.Direct3D.FeatureLevel.Level_9_3,
                SharpDX.Direct3D.FeatureLevel.Level_9_2,
                SharpDX.Direct3D.FeatureLevel.Level_9_1,
            };
            if (d11Device != null)
                Utilities.Dispose(ref d11Device);
            d11Device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, creationFlags, featureLevels);
            Factory1 = new SharpDX.Direct2D1.Factory2(FactoryType.MultiThreaded, factory1Flags);
            if (_genericDevice != null)
                Utilities.Dispose(ref _genericDevice);
            _genericDevice = d11Device.QueryInterface<SharpDX.DXGI.Device>();
            if (_compDevice != null)
                Utilities.Dispose(ref _compDevice);
            _compDevice = new SharpDX.DirectComposition.Device(_genericDevice);
            BitmapProperties1 = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Ignore), Factory1.DesktopDpi.Width, Factory1.DesktopDpi.Height, BitmapOptions.CannotDraw | BitmapOptions.Target);
            if (_visual == null)
                _visual = new SharpDX.DirectComposition.Visual(_compDevice);
            if (_target == null)
                _target = Target.FromHwnd(_compDevice, _hwnd, true);
            _target.Root = _visual;
            _compDevice.Commit();
        }
        public void CreateSwapChainAndDeviceContext(int width, int height, float offsetX, float offsetY, out SwapChain1 swapChain, out DeviceContext deviceContext)
        {
            var desc = new SwapChainDescription1()
            {
                BufferCount = 2,
                Width = width,
                Height = height,
                Format = Format.R8G8B8A8_UNorm,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.FlipSequential,
                Usage = Usage.RenderTargetOutput
            };

            swapChain = new SwapChain1(new SharpDX.DXGI.Factory2(), _genericDevice, ref desc);

            var visual = new SharpDX.DirectComposition.Visual(_compDevice)
            {
                Content = swapChain,
            };
            visual.SetOffsetX(offsetX);
            visual.SetOffsetY(offsetY);
            _visual.AddVisual(visual, true, null);
            _compDevice.Commit();
            using (var d2Device = new SharpDX.Direct2D1.Device(Factory1, _genericDevice))
                deviceContext = new DeviceContext(d2Device, DeviceContextOptions.None) { AntialiasMode = AntialiasMode.Aliased };
        }
    }
公共类DirectX
{
公共BitmapProperties1 BitmapProperties1{get;private set;}
public SharpDX.Direct2D1.Factory1 Factory1{get;private set;}
专用SharpDX.DXGI.Device\u通用设备;
私有SharpDX.DirectComposition.Visual\u Visual;
私人目标(u Target),;
专用SharpDX.DirectComposition.Device\u compDevice;
专用SharpDX.Direct3D11.Device D11设备;
私人互联网;
公共结果?DeviceMovedReason=>D11设备?.DeviceMovedReason;
公共DirectX(窗口)
{
_hwnd=新的WindowInteropHelper(window).Handle;
初始化设备();
}
private bool_isRefreshed=假;
public void InitilizeDevices()
{
var creationFlags=SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
var factory1Flags=DebugLevel.None;
#如果调试
creationFlags |=SharpDX.Direct3D11.DeviceCreationFlags.Debug;
factory1Flags=DebugLevel.Information;
#恩迪夫
SharpDX.Direct3D.FeatureLevel[]FeatureLevel=
{
SharpDX.Direct3D.FeatureLevel.Level_11_1,
SharpDX.Direct3D.FeatureLevel.Level_11_0,
SharpDX.Direct3D.FeatureLevel.Level_10_1,
SharpDX.Direct3D.FeatureLevel.Level_10_0,
SharpDX.Direct3D.FeatureLevel.Level_9_3,
SharpDX.Direct3D.FeatureLevel.Level_9_2,
SharpDX.Direct3D.FeatureLevel.Level_9_1,
};
如果(D11设备!=null)
公用设施。处置(参考D11设备);
d11Device=新的SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware、creationFlags、FeatureLevel);
Factory1=新的SharpDX.Direct2D1.Factory2(FactoryType.MultiThreaded,factory1Flags);
if(_genericDevice!=null)
公用设施。处置(参考通用设备);
_genericDevice=d11Device.QueryInterface();
如果(_compDevice!=null)
公用设施。处置(参考装置);
_compDevice=新的SharpDX.DirectComposition.Device(\u genericDevice);
BitMapProperties 1=新的BitMapProperties 1(新的SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm,SharpDX.Direct2D1.AlphaMode.Ignore),Factory1.DesktopDpi.Width,Factory1.DesktopDpi.Height,BitmapOptions.CannotDraw | BitmapOptions.Target);
如果(_visual==null)
_visual=新的SharpDX.DirectComposition.visual(_compDevice);
如果(_target==null)
_target=target.FromHwnd(_compDevice,_hwnd,true);
_target.Root=\u visual;
_compDevice.Commit();
}
public void CreateSwapChain和DeviceContext(int-width、int-height、float-offsetX、float-offsetY、out-SwapChain1-swapChain、out-DeviceContext-DeviceContext)
{
var desc=新SwapChainDescription1()
{
BufferCount=2,
宽度=宽度,
高度=高度,
Format=Format.R8G8B8A8_UNorm,
SampleDescription=新的SampleDescription(1,0),
SwapEffect=SwapEffect.FlipSequential,
用法=用法.RenderTargetOutput
};
swapChain=新的SwapChain1(新的SharpDX.DXGI.Factory2(),通用设备,参考说明);
var visual=新的SharpDX.DirectComposition.visual(_compDevice)
{
内容=交换链,
};
visual.SetOffsetX(offsetX);
visual.SetOffsetY(offsetY);
_visual.AddVisual(visual,true,null);
_compDevice.Commit();
使用(var d2Device=new SharpDX.Direct2D1.Device(Factory1,通用设备))
deviceContext=新的deviceContext(d2Device,DeviceContextOptions.None){AntialiasMode=AntialiasMode.Aliased};
}
}
控制:

public void Init(float offsetX, float offsetY, DirectX directX)
        {
            _directX = directX;
            _offsetX = offsetX;
            _offsetY = offsetY;
            Stopwatch = new Stopwatch();
            _directX.CreateSwapChainAndDeviceContext((int)ActualWidth, (int)ActualHeight, _offsetX, _offsetY, out swapchain, out deviceContext);
            brush = new SharpDX.Direct2D1.SolidColorBrush(deviceContext, new Color4(1f, 0f, 0, 1f));
            stop = false;
            var thread = new Thread(new ThreadStart(Render));
            thread.Start();
        }
        public Action RefreshAction { get; set; } // call InitializeDevices in DirectX
        Random rand = new Random();
        public bool stop;
        SharpDX.DXGI.SwapChain1 swapchain;
        SharpDX.Direct2D1.SolidColorBrush brush;
        DeviceContext deviceContext;
        SharpDX.DXGI.Surface2 backBuffer;
        Stopwatch Stopwatch;
        object _lock = new object();
        DirectX _directX;
        private void Render()
        {
            while (!stop)
            {
                Stopwatch.Start();
                try
                {
                    backBuffer = swapchain.GetBackBuffer<SharpDX.DXGI.Surface2>(0);
                    var bitmap1 = new SharpDX.Direct2D1.Bitmap1(deviceContext, backBuffer, _directX.BitmapProperties1);
                    deviceContext.Target = bitmap1;
                    deviceContext.BeginDraw();
                    deviceContext.Clear(SharpDX.Color.Green);
                    using (var geometry = new SharpDX.Direct2D1.PathGeometry(_directX.Factory1))
                    {
                        using (var path = geometry.Open())
                        {
                            double angle = 10 * Math.PI * (rand.NextDouble() - 0.5);
                            sin.Add(100 + 10 * Math.Sin(angle));
                            sin.RemoveAt(0);
                            path.BeginFigure(new Vector2(0, (float)sin[0]), FigureBegin.Hollow);
                            for (int k = 1; k < 100; k++)
                            {
                                path.AddLine(new Vector2(k * 5, (float)sin[k]));
                            }
                            path.EndFigure(FigureEnd.Open);

                            path.Close();
                        }
                        deviceContext.DrawGeometry(geometry, brush, 1);
                    }
                    deviceContext.DrawEllipse(new SharpDX.Direct2D1.Ellipse(new Vector2(100, 100), 20, 20), brush);
                    deviceContext.EndDraw();
                    swapchain.Present(1, PresentFlags.None);
                    bitmap1.Dispose();
                    backBuffer.Dispose();
                }
                catch (SharpDXException ex) when ((uint)ex.HResult == 0x8899000C || (uint)ex.HResult == 0x887A0020 || (uint)ex.HResult == 0x887A0005)
                {
                    Console.WriteLine(_directX?.DeviceRemovedReason);
                    Console.WriteLine(ex);
                    if (!stop)
                        RefreshAction();
                     return;
                }
                catch (AccessViolationException ex)
                {
                    Console.WriteLine(ex);
                }
                Stopwatch.Stop();
                Stopwatch.Reset();
                if (Stopwatch.ElapsedMilliseconds < 20)
                    Thread.Sleep(20 - (int)Stopwatch.ElapsedMilliseconds);
            }
        }
public void Init(float offsetX、float offsetY、DirectX DirectX)
{
_directX=directX;
_offsetX=offsetX;
_offsetY=offsetY;
秒表=新秒表();
_directX.CreateSwapChain和deviceContext((int)实际宽度,(int)实际高度,_offsetX,_offsetY,out swapchain,out deviceContext);
画笔=新的SharpDX.Direct2D1.SolidColorBrush(deviceContext,新的颜色4(1f,0f,0,1f));
停止=错误;
var thread=new thread(new ThreadStart(Render));
thread.Start();
}
公共操作刷新操作{get;set;}//调用DirectX中的InitializeDevices
Random rand=新的Random();
公共停车场;
SharpDX.DXGI.SwapChain1 swapchain;
SharpDX.Direct2D1.SolidColorBrush;
DeviceContext DeviceContext;
SharpDX.DXGI.Surface2 backBuffer;
秒表;
对象_lock=新对象();
DirectX\u DirectX;
私有void Render()
{
当(!停止)
{
秒表。开始();
尝试
{
backBuffer=swapchain.GetBackBuffer(0);
var bitmap1=新的SharpDX.Direct2D1.bitmap1(deviceContext、backBuffer、_directX.BitmapProperties1);
deviceContext.Target=bitmap1;
deviceContext.BeginDraw();
deviceContext.Clear(SharpDX.Color.Green);
使用(var几何体=新的SharpDX.Dire)