Пример #1
0
        private void dislpayTimer_Tick(object sender, EventArgs ev)
        {
            // duplicate output stuff
            SharpDX.DXGI.Resource screenResource = null;
            SharpDX.DataStream    dataStream;
            SharpDX.DXGI.Surface  screenSurface;

            try
            {
                SharpDX.DXGI.OutputDuplicateFrameInformation duplicateFrameInformation;
                duplicatedOutput.AcquireNextFrame(1000, out duplicateFrameInformation, out screenResource);
            }
            catch (SharpDX.SharpDXException e)
            {
                if (e.ResultCode.Code == SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
                {
                    // keep retrying
                    return;
                }
                else
                {
                    throw e;
                }
            }

            // copy resource into memory that can be accessed by the CPU
            c_device.ImmediateContext.CopyResource(screenResource.QueryInterface <SharpDX.Direct3D11.Resource>(), screenTexture);
            // cast from texture to surface, so we can access its bytes
            screenSurface = screenTexture.QueryInterface <SharpDX.DXGI.Surface>();

            // map the resource to access it
            screenSurface.Map(SharpDX.DXGI.MapFlags.Read, out dataStream);

            getImageFromDXStream(dataStream);

            captureBox.Image = target;

            // free resources
            dataStream.Close();
            screenSurface.Unmap();
            screenSurface.Dispose();
            screenResource.Dispose();
            duplicatedOutput.ReleaseFrame();

            // print how many frames we could process within the last second
            Console.WriteLine("fps: {0}", 1f / sw.Elapsed.TotalSeconds);
            sw.Reset();
            sw.Start();
        }
Пример #2
0
        private void OnRender(IntPtr handle, bool isNewSurface)
        {
            if (isNewSurface)
            {
                if (brush != null)
                {
                    brush.Dispose();
                    brush = null;
                }

                if (renderTarget != null)
                {
                    renderTarget.Dispose();
                    renderTarget = null;
                }

                SharpDX.ComObject            comObject = new SharpDX.ComObject(handle);
                SharpDX.DXGI.Resource        resource  = comObject.QueryInterface <SharpDX.DXGI.Resource>();
                SharpDX.Direct3D10.Texture2D texture   = resource.QueryInterface <SharpDX.Direct3D10.Texture2D>();
                using (var surface = texture.QueryInterface <SharpDX.DXGI.Surface>())
                {
                    var properties = new RenderTargetProperties();
                    properties.DpiX        = 96;
                    properties.DpiY        = 96;
                    properties.MinLevel    = FeatureLevel.Level_DEFAULT;
                    properties.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, AlphaMode.Premultiplied);
                    properties.Type        = RenderTargetType.Default;
                    properties.Usage       = RenderTargetUsage.None;

                    renderTarget = new RenderTarget(new Factory(), surface, properties);
                }
            }

            if (brush == null)
            {
                brush = new SharpDX.Direct2D1.SolidColorBrush(renderTarget, new SharpDX.Color4(0.2f, 0.2f, 0.2f, 0.5f));
            }

            renderTarget.BeginDraw();
            renderTarget.DrawTextLayout(new SharpDX.Vector2(50, 50), textLayout, brush);
            renderTarget.EndDraw();
        }