Пример #1
0
        public Object LoadImage(String filename)
        {
            Object image = null;

            if (File.Exists(filename))
            {
                using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(filename))
                {
                    System.Drawing.Imaging.BitmapData bData = bmp.LockBits(
                        new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                        System.Drawing.Imaging.ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format32bppPArgb
                        );

                    SharpDX.DataStream                 stream  = new SharpDX.DataStream(bData.Scan0, bData.Stride * bData.Height, true, false);
                    SharpDX.Direct2D1.PixelFormat      pFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
                    SharpDX.Direct2D1.BitmapProperties bProps  = new SharpDX.Direct2D1.BitmapProperties(pFormat);

                    image = new SharpDX.Direct2D1.Bitmap(d2dRenderTarget, new Size2(bmp.Width, bmp.Height), stream, bData.Stride, bProps);
                    bmp.UnlockBits(bData);
                    stream.Dispose();
                }
            }

            return(image);
        }
Пример #2
0
        public DUIWindowRenderTarget(IntPtr handle)
        {
            Size size = System.Windows.Forms.Control.FromHandle(handle).Size;

            this.direct2D1factory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded);
            SharpDX.Direct2D1.HwndRenderTargetProperties hwndRenderTargetProperties = new SharpDX.Direct2D1.HwndRenderTargetProperties()
            {
                Hwnd           = handle,
                PixelSize      = new SharpDX.Size2(size.Width, size.Height),
                PresentOptions = SharpDX.Direct2D1.PresentOptions.None
            };
            SharpDX.Direct2D1.PixelFormat pixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            //强制设置DPI为96,96(默认值),无意间发现有的人居然会去修改系统的dpi
            SharpDX.Direct2D1.RenderTargetProperties renderTargetProperties = new SharpDX.Direct2D1.RenderTargetProperties(SharpDX.Direct2D1.RenderTargetType.Default, pixelFormat, 96, 96, SharpDX.Direct2D1.RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT);
            //初始化,在_d2dFactory创建渲染缓冲区并与Target绑定
            //SharpDX.Direct2D1.PixelFormat pf = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            this.windowRenderTarget = new SharpDX.Direct2D1.WindowRenderTarget(direct2D1factory, renderTargetProperties, hwndRenderTargetProperties);
            this.windowRenderTarget.AntialiasMode     = SharpDX.Direct2D1.AntialiasMode.Aliased;
            this.windowRenderTarget.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Aliased;
        }
Пример #3
0
        private void InitDx()
        {
            logger.Debug("GDICapture::InitDx()");

            SharpDX.DXGI.Factory1 dxgiFactory = null;
            SharpDX.DXGI.Adapter1 adapter     = null;
            SharpDX.DXGI.Output   output      = null;
            try
            {
                dxgiFactory = new SharpDX.DXGI.Factory1();

                logger.Info(MediaFoundation.DxTool.LogDxAdapters(dxgiFactory.Adapters1));

                //var hMonitor = NativeAPIs.User32.GetMonitorFromRect(this.srcRect);
                //if (hMonitor != IntPtr.Zero)
                //{
                //    foreach (var _adapter in dxgiFactory.Adapters1)
                //    {
                //        foreach (var _output in _adapter.Outputs)
                //        {
                //            var descr = _output.Description;
                //            if (descr.MonitorHandle == hMonitor)
                //            {
                //                adapter = _adapter;
                //                output = _output;

                //                break;
                //            }
                //        }
                //    }
                //}

                if (adapter == null)
                {// первым идет адаптер с которому подключен primary монитор
                    adapter = dxgiFactory.GetAdapter1(0);
                }

                AdapterId = adapter.Description.Luid;

                //logger.Info("Screen source info: " + adapter.Description.Description + " " + output.Description.DeviceName);

                var deviceCreationFlags =
                    //DeviceCreationFlags.Debug |
                    // DeviceCreationFlags.VideoSupport |
                    DeviceCreationFlags.BgraSupport;

                //var feature = FeatureLevel.Level_11_0;

                device = new Device(adapter, deviceCreationFlags);
                using (var multiThread = device.QueryInterface <SharpDX.Direct3D11.Multithread>())
                {
                    multiThread.SetMultithreadProtected(true);
                }

                SharedTexture = new Texture2D(device,
                                              new Texture2DDescription
                {
                    CpuAccessFlags = CpuAccessFlags.None,
                    BindFlags      = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    Format         = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width          = DestSize.Width,
                    Height         = DestSize.Height,

                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.Shared,
                });

                gdiTexture = new Texture2D(device,
                                           new Texture2DDescription
                {
                    CpuAccessFlags = CpuAccessFlags.None,
                    BindFlags      = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    Format         = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width          = SrcRect.Width,  //DestSize.Width,
                    Height         = SrcRect.Height, //DestSize.Height,

                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.GdiCompatible,
                });


                renderTexture = new Texture2D(device,
                                              new Texture2DDescription
                {
                    CpuAccessFlags = CpuAccessFlags.None,
                    BindFlags      = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    Format         = SharpDX.DXGI.Format.B8G8R8A8_UNorm,

                    Width             = DestSize.Width,
                    Height            = DestSize.Height,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Default,
                    //OptionFlags = ResourceOptionFlags.GdiCompatible//ResourceOptionFlags.None,
                    OptionFlags = ResourceOptionFlags.Shared,
                });

                if (DestSize.Width != SrcRect.Width || DestSize.Height != SrcRect.Height)
                {
                    using (SharpDX.Direct2D1.Factory1 factory2D1 = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded))
                    {
                        using (var surf = renderTexture.QueryInterface <SharpDX.DXGI.Surface>())
                        {
                            //var pixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Ignore);

                            var pixelFormat       = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
                            var renderTargetProps = new SharpDX.Direct2D1.RenderTargetProperties(pixelFormat);

                            renderTarget = new SharpDX.Direct2D1.RenderTarget(factory2D1, surf, renderTargetProps);

                            //var d2dContext = new SharpDX.Direct2D1.DeviceContext(surface);
                        }
                    }
                }
            }
            finally
            {
                if (adapter != null)
                {
                    adapter.Dispose();
                    adapter = null;
                }

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

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