public void StartCaptureInternal(GraphicsCaptureItem item) { StopCapture(); _item = item; _lastSize = _item.Size; _swapChain = new CanvasSwapChain(_canvasDevice, _item.Size.Width, _item.Size.Height, 96); swapChain.SwapChain = _swapChain; _framePool = Direct3D11CaptureFramePool.Create( _canvasDevice, // D3D device DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format 60, // Number of frames _item.Size); // Size of the buffers _session = _framePool.CreateCaptureSession(_item); _framePool.FrameArrived += (s, a) => { using (var frame = _framePool.TryGetNextFrame()) { ProcessFrame(frame); } }; _item.Closed += (s, a) => { StopCapture(); }; _session.StartCapture(); }
private void DetermineMaxItemSize(IDrawingContext dc, Item[] items, out SizeInt32 maxItemSizeResult, out SizeInt32 maxImageSizeResult) { int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; foreach (Item item in items) { num = Math.Max(num, (item.Image == null) ? 0 : item.Image.Width); num2 = Math.Max(num2, (item.Image == null) ? 0 : item.Image.Height); TextLayoutAlgorithm?layoutAlgorithm = null; TextLayout resourceSource = UIText.CreateLayout(dc, item.Name, this.Font, layoutAlgorithm, HotkeyRenderMode.Ignore, 65535.0, 65535.0); TextMetrics metrics = dc.GetCachedOrCreateResource <ITextLayout>(resourceSource).Metrics; SizeInt32 num9 = new SizeInt32((int)Math.Ceiling((double)metrics.WidthMax), (int)Math.Ceiling((double)metrics.Height)); num3 = Math.Max(num9.Width, num3); num4 = Math.Max(num9.Height, num4); } int recommendedExtent = this.dropShadowRenderer.GetRecommendedExtent(num, num2); int width = ((((((recommendedExtent + this.imageXInset) + num) + this.imageXInset) + recommendedExtent) + this.textLeftMargin) + num3) + this.textRightMargin; int height = Math.Max((int)((((this.imageYInset + recommendedExtent) + num2) + this.imageYInset) + recommendedExtent), (int)((this.textVMargin + num4) + this.textVMargin)); maxItemSizeResult = new SizeInt32(width, height); maxImageSizeResult = new SizeInt32(num, num2); }
private void ResetFramePool(SizeInt32 size, bool recreateDevice) { do { try { if (recreateDevice) { _canvasDevice = new CanvasDevice(); } _framePool.Recreate( _canvasDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, size); } // This is the device-lost convention for Win2D. catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult)) { _canvasDevice = null; recreateDevice = true; } } while (_canvasDevice == null); }
protected virtual void ResetFramePool(SizeInt32 size, bool recreateDevice) { do { try { if (recreateDevice) { _device = Direct3D11Helper.CreateDevice(!PresentationToNDIAddIn.Properties.Settings.Default.UseHw); } } catch { _device = null; recreateDevice = true; } } while (_device == null); // detect change from or to fullscreen --> captureItem needs to be recreated if ((_wasFullscreenBefore && !IsFullscreen) || (!_wasFullscreenBefore && IsFullscreen)) { _wasFullscreenBefore = IsFullscreen; DisposeCaptureItemDependentStuff(); _item = GetItem(); CreateCaptureItemDependendStuff(); } else { _framePool.Recreate(_device, PixelFormat, 2, size); } }
private async Task StartCaptureInternal(GraphicsCaptureItem item) { // Stop the previous capture if we had one. StopCapture(); _item = item; _lastSize = _item.Size; _canvasDevice = new CanvasDevice(); _framePool = Direct3D11CaptureFramePool.Create( _canvasDevice, // D3D device DirectXPixelFormat.B8G8R8A8UIntNormalized, // Pixel format 2, // Number of frames _item.Size); // Size of the buffers _item.Closed += (s, a) => { StopCapture(); }; _session = _framePool.CreateCaptureSession(_item); _session.StartCapture(); await Task.Delay(500); var frame = _framePool.TryGetNextFrame(); await ProcessFrame(frame); StopCapture(); }
private void CloseWorkspace(AppWorkspace appWorkspace, DocumentWorkspace dw) { if (dw != null) { if (dw.Document == null) { appWorkspace.RemoveDocumentWorkspace(dw); } else if (!dw.Document.Dirty) { appWorkspace.RemoveDocumentWorkspace(dw); } else { appWorkspace.ActiveDocumentWorkspace = dw; TaskButton button = new TaskButton(PdnResources.GetImageResource("Icons.MenuFileSaveIcon.png").Reference, PdnResources.GetString("CloseWorkspaceAction.SaveButton.ActionText"), PdnResources.GetString("CloseWorkspaceAction.SaveButton.ExplanationText")); TaskButton button2 = new TaskButton(PdnResources.GetImageResource("Icons.MenuFileCloseIcon.png").Reference, PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ActionText"), PdnResources.GetString("CloseWorkspaceAction.DontSaveButton.ExplanationText")); TaskButton button3 = new TaskButton(PdnResources.GetImageResource("Icons.CancelIcon.png").Reference, PdnResources.GetString("CloseWorkspaceAction.CancelButton.ActionText"), PdnResources.GetString("CloseWorkspaceAction.CancelButton.ExplanationText")); string str = PdnResources.GetString("CloseWorkspaceAction.Title"); string str3 = string.Format(PdnResources.GetString("CloseWorkspaceAction.IntroText.Format"), dw.GetFileFriendlyName()); int thumbEdgeLength = UIUtil.ScaleWidth(80); SizeInt32 num = ThumbnailHelpers.ComputeThumbnailSize(dw.Document.Size(), thumbEdgeLength); SizeInt32 fullThumbSize = new SizeInt32(num.Width + 4, num.Height + 4); bool animating = true; Image finalThumb = null; Action <Image> animationEvent = null; Image[] busyAnimationFrames = AnimatedResources.Working; Image[] busyAnimationThumbs = new Image[busyAnimationFrames.Length]; int animationHz = 50; Stopwatch timing = new Stopwatch(); timing.Start(); long elapsedTicks = timing.ElapsedTicks; using (System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer()) {
private void ResetFramePool(SizeInt32 size, bool recreateDevice) { Logger.Debug("Screen", "ResetFramePool"); do { try { if (recreateDevice) { canvasDevice = new CanvasDevice(); } framePool.Recreate( canvasDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, size); } catch (Exception e) when(canvasDevice.IsDeviceLost(e.HResult)) { canvasDevice = null; recreateDevice = true; } } while (canvasDevice == null); }
public void UpdateResolution(SizeInt32 size) { if (size.Height <= 0 || size.Width <= 0) { return; } if (_framePool is not null && _session is not null) { // Unsubscribe from old framePool _framePool.FrameArrived -= OnFrameArrived; // Dispose old session and framePool _session.Dispose(); _framePool.Dispose(); } _canvasDevice = CanvasDevice.GetSharedDevice(); // Create a frame pool with room for only 1 frame because we're getting a single frame, not a video. _framePool = Direct3D11CaptureFramePool.Create(_canvasDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 1, size); _session = _framePool.CreateCaptureSession(GraphicsCaptureItem.CreateFromVisual(_capturedVisual)); _framePool.FrameArrived += OnFrameArrived; _session.StartCapture(); }
private void ResetFramePool(SizeInt32 size, bool recreateDevice) { do { try { if (recreateDevice) { _canvasDevice = new CanvasDevice(); foreach (var clip in ClipImages.Values) { clip.Resize(_canvasDevice, true); } } _framePool.Recreate( _canvasDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, size); renderTarget.Dispose(); renderTarget = new CanvasRenderTarget(_canvasDevice, size.Width, size.Height, 96); } // This is the device-lost convention for Win2D. catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult)) { _canvasDevice = null; recreateDevice = true; } } while (_canvasDevice == null); }
protected BrushStamp(SizeInt32 size, double opacity, bool antialiased) { this.Size = size; this.Opacity = opacity; this.Antialiased = antialiased; this.lazyMaskBitmap = new LazyResult <IBitmap <ColorAlpha8>, int>(_ => this.CreateMaskBitmap(), 0); }
private void InitializeBlankTexture(SizeInt32 size) { var description = new SharpDX.Direct3D11.Texture2DDescription { Width = size.Width, Height = size.Height, MipLevels = 1, ArraySize = 1, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, SampleDescription = new SharpDX.DXGI.SampleDescription() { Count = 1, Quality = 0 }, Usage = SharpDX.Direct3D11.ResourceUsage.Default, BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None }; _blankTexture = new SharpDX.Direct3D11.Texture2D(_d3dDevice, description); using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, _blankTexture)) { _d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1)); } }
private void SetConfig(SizeInt32 size, bool directX) { Config = new InputConfig( new InputStreamProperties[] { new InputStreamProperties { CodecProps = new CodecProperties { codec_type = AVMediaType.AVMEDIA_TYPE_VIDEO, sample_aspect_ratio = new AVRational { num = 0, den = 1 }, codec_id = Core.Const.CODEC_ID_RAWVIDEO, width = size.Width, height = size.Height, bits_per_coded_sample = 4 * 8, format = directX ? Core.PIX_FMT_INTERNAL_DIRECTX : Core.Const.PIX_FMT_BGRA, extradata = new byte[1024] } } } ); }
private void OnThumbnailRendered(object sender, ValueEventArgs <Tuple <IThumbnailProvider, ISurface <ColorBgra> > > e) { this.VerifyThreadAccess(); RenderArgs args = null; DocumentWorkspace item = (DocumentWorkspace)e.Value.Item1; ISurface <ColorBgra> renderer = e.Value.Item2; if (this.documents.Contains(item)) { SizeInt32 num = renderer.Size <ColorBgra>(); if (this.thumbs.ContainsKey(item)) { args = this.thumbs[item]; if (args.Size.ToSizeInt32() != num) { args.ISurface.Dispose(); args.Dispose(); args = null; this.thumbs.Remove(item); } } if (args == null) { args = new RenderArgs(e.Value.Item2.ToSurface()); this.thumbs.Add(item, args); } e.Value.Item2.Render <ColorBgra>(args.ISurface); e.Value.Item2.Dispose(); this.OnThumbnailUpdated(item); } }
private IDeviceBitmap GetOrCreateDeviceBitmap(IDeviceResourceFactory factory, SizeInt32 desiredPixelSize) { base.VerifyAccess(); IDeviceBitmap result = null; int num = Int32Util.Pow2RoundUp(desiredPixelSize.Width); int num2 = Int32Util.Pow2RoundUp(desiredPixelSize.Height); int width = Math.Max(Math.Max(num, num2), 0x100); SizeInt32 num5 = new SizeInt32(width, width); while (this.deviceBitmapPool.TryDequeue(out result)) { SizeInt32 pixelSize = result.PixelSize; if (((pixelSize.Width >= num5.Width) && (pixelSize.Width <= (num5.Width * 2))) && ((pixelSize.Height >= num5.Height) && (pixelSize.Height <= (num5.Height * 2)))) { break; } DisposableUtil.Free <IDeviceBitmap>(ref result); } if (result == null) { result = factory.CreateDeviceBitmap(desiredPixelSize, maskBitmapProperties); AttachedData.SetValue(result, isPooledBitmapAttachedKey, BooleanUtil.GetBoxed(true)); } return(result); }
protected override void OnDraw(Graphics g) { if (string.IsNullOrWhiteSpace(this.ClassName)) { g.Clear(Color.Transparent); } else { SizeInt32 size = base.Size; try { VisualStyleRenderer renderer = new VisualStyleRenderer(this.ClassName, this.Part, this.State); Rectangle bounds = new Rectangle(0, 0, size.Width, size.Height); renderer.DrawBackground(g, bounds, bounds); } catch (InvalidOperationException) { g.Clear(Color.Transparent); GdiPlusDrawing fallbackDrawing = this.FallbackDrawing; if (fallbackDrawing != null) { fallbackDrawing.Size = size; fallbackDrawing.Draw(g); } } } }
public void SetGraphicItem(GraphicsCaptureItem item) { this.ResetState(); this.item = item; if (this.item != null) { this.renderTargetDescription = new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, Width = item.Size.Width, Height = item.Size.Height, Usage = ResourceUsage.Default, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, ArraySize = 1, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, OptionFlags = ResourceOptionFlags.Shared, MipLevels = 1, SampleDescription = new SampleDescription(1, 0), }; this.renderTarget = new Texture2D(d3dDevice, this.renderTargetDescription); framePool = Direct3D11CaptureFramePool.Create(device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, this.item.Size); session = framePool.CreateCaptureSession(this.item); lastSize = this.item.Size; framePool.FrameArrived += this.OnFrameArrived; session.StartCapture(); } }
public void RequestRender() { this.fpsCount++; using var frame = this.framePool.TryGetNextFrame(); if (frame != null && this.IsFrontBufferAvailable && this.fpsCount % this.FpsDivider == 0) { if (this.TryLock(new Duration(TimeSpan.Zero))) { var newSize = false; if (frame.ContentSize.Width != lastSize.Width || frame.ContentSize.Height != lastSize.Height) { // The thing we have been capturing has changed size. // We need to resize the swap chain first, then blit the pixels. // After we do that, retire the frame and then recreate the frame pool. newSize = true; lastSize = frame.ContentSize; this.renderTargetDescription.Width = lastSize.Width; this.renderTargetDescription.Height = lastSize.Height; this.renderTarget?.Dispose(); this.renderTarget = new Texture2D(d3dDevice, this.renderTargetDescription); backBufferSetted = false; } using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface)) { d3dDevice.ImmediateContext.CopyResource(bitmap, this.renderTarget); } if (newSize || !backBufferSetted) { using var resource = this.renderTarget.QueryInterface <SharpDX.DXGI.Resource>(); var handle = resource.SharedHandle; using var texture = new Texture( this.d9device, this.renderTarget.Description.Width, this.renderTarget.Description.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8, Pool.Default, ref handle); using var surface = texture.GetSurfaceLevel(0); this.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer, true); backBufferSetted = true; } this.AddDirtyRect(new Int32Rect(0, 0, lastSize.Width, lastSize.Height)); if (newSize) { framePool.Recreate(device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, lastSize); } } this.Unlock(); } }
public Brush() { _lastUsedSize = new SizeInt32(0, 0); _image = new BitmapImage(new Uri(@"pack://application:,,,/TEdit;component/Images/Tools/paintbrush.png")); _name = "Brush"; _type = ToolType.Brush; _isActive = false; }
private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args) { var newSize = false; using (var frame = sender.TryGetNextFrame()) { if (frame.ContentSize.Width != lastSize.Width || frame.ContentSize.Height != lastSize.Height) { // The thing we have been capturing has changed size. // We need to resize the swap chain first, then blit the pixels. // After we do that, retire the frame and then recreate the frame pool. newSize = true; lastSize = frame.ContentSize; swapChain.ResizeBuffers( 2, lastSize.Width, lastSize.Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None); } if (!isWait) { if (isStartCapture) { using (var backBuffer = swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0)) using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface)) { //d3dDevice.ImmediateContext.CopyResource(bitmap, backBuffer); CopyBitmap(frame.Surface, bitmap, frame.ContentSize.Width, frame.ContentSize.Height); Console.WriteLine("Capture"); bitmap.Dispose(); }; isStartCapture = false; } } //using (var backBuffer = swapChain.GetBackBuffer<SharpDX.Direct3D11.Texture2D>(0)) } // Retire the frame. swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); if (newSize) { framePool.Recreate( device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, lastSize); } }
private void ProcessFrame(Direct3D11CaptureFrame frame) { // Resize and device-lost leverage the same function on the // Direct3D11CaptureFramePool. Refactoring it this way avoids // throwing in the catch block below (device creation could always // fail) along with ensuring that resize completes successfully and // isn’t vulnerable to device-lost. bool needsReset = false; bool recreateDevice = false; var scale = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; if ((frame.ContentSize.Width != _lastSize.Width) || (frame.ContentSize.Height != _lastSize.Height)) { needsReset = true; _lastSize = frame.ContentSize; var _height = frame.ContentSize.Height - 32; bool result = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(new Size { Width = frame.ContentSize.Width / scale, Height = _height / scale }); System.Diagnostics.Debug.WriteLine(result); if (!result) { bool _ = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryEnterFullScreenMode(); System.Diagnostics.Debug.WriteLine(_); } } try { // Take the D3D11 surface and draw it into a // Composition surface. // Convert our D3D11 surface into a Win2D object. CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface( _canvasDevice, frame.Surface); _currentFrame = canvasBitmap; // Helper that handles the drawing for us. FillSurfaceWithBitmap(canvasBitmap); } // This is the device-lost convention for Win2D. catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult)) { // We lost our graphics device. Recreate it and reset // our Direct3D11CaptureFramePool. needsReset = true; recreateDevice = true; } if (needsReset) { ResetFramePool(frame.ContentSize, recreateDevice); } }
private void InitializeComposeTexture(SizeInt32 size) { var description = new SharpDX.Direct3D11.Texture2DDescription { Width = size.Width, Height = size.Height, MipLevels = 1, ArraySize = 1, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, SampleDescription = new SharpDX.DXGI.SampleDescription() { Count = 1, Quality = 0 }, Usage = SharpDX.Direct3D11.ResourceUsage.Default, BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None }; var tardescription = new SharpDX.Direct3D11.Texture2DDescription { Width = 1920, Height = 1080, MipLevels = 1, ArraySize = 1, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, SampleDescription = new SharpDX.DXGI.SampleDescription() { Count = 1, Quality = 0 }, Usage = SharpDX.Direct3D11.ResourceUsage.Default, BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None }; _tarComposeTexture = new SharpDX.Direct3D11.Texture2D(_d3dDevice, tardescription); _composeTexture = new SharpDX.Direct3D11.Texture2D(_d3dDevice, description); _composeRenderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, _composeTexture); byte[] data = new byte[1920 * 1080 * 4]; for (int i = 0; i < 1920 * 1080 * 4; i++) { if ((i + 1) % 4 == 0) { data[i] = 255; } else { data[i] = 255; } } DataStream s = DataStream.Create(data, true, true); DataRectangle rect = new DataRectangle(s.DataPointer, 1920 * 4); _blankComposeTexture = new SharpDX.Direct3D11.Texture2D(_d3dDevice, tardescription, rect); }
private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args) { using (var frame = sender.TryGetNextFrame()) { var needsReset = false; var recreateDevice = false; if ((frame.ContentSize.Width != _lastSize.Width) || (frame.ContentSize.Height != _lastSize.Height)) { needsReset = true; _lastSize = frame.ContentSize; } try { using (var backBuffer = _swapChain.GetBackBuffer <Texture2D>(0)) { using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface)) { // copy current surface to backbuffer _d3dDevice.ImmediateContext.CopyResource(bitmap, backBuffer); // Create buffer for the resized copy var width = StreamWidth; var height = StreamHeight; using (var copy = new Texture2D(_d3dDevice, new Texture2DDescription { Width = width, Height = height, MipLevels = 1, ArraySize = 1, Format = bitmap.Description.Format, Usage = ResourceUsage.Staging, SampleDescription = new SampleDescription(1, 0), BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, OptionFlags = ResourceOptionFlags.None })) { // Copy region from captured bitmap to stream bitmap _d3dDevice.ImmediateContext.CopySubresourceRegion(backBuffer, 0, ROI, copy, 0); // access the copied data in a stream _d3dDevice.ImmediateContext.MapSubresource(copy, 0, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out DataStream stream); _buf.Add(new BufferedFrame(stream, new SizeInt32 { Width = width, Height = height }, bitmap.Description.Format)); _d3dDevice.ImmediateContext.UnmapSubresource(copy, 0); } } } } catch (Exception ex) { needsReset = true; recreateDevice = true; } if (needsReset) { _swapChain.ResizeBuffers(_swapChain.Description1.BufferCount, _lastSize.Width, _lastSize.Height, _swapChain.Description1.Format, _swapChain.Description1.Flags); ResetFramePool(_lastSize, recreateDevice); } } }
private RectI TileViewportRect() { var vm = (WorldViewModel)DataContext; var partView = (ScrollViewer)FindName("WorldScrollViewer"); var xy = new PointInt32((int)(partView.HorizontalOffset / vm.Zoom), (int)(partView.VerticalOffset / vm.Zoom)); var size = new SizeInt32((int)(partView.ActualWidth / vm.Zoom), (int)(partView.ActualHeight / vm.Zoom)); return(new RectI(xy, size)); }
private static void ResizeWindow(Window window, int width, int height) { IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window); WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd); AppWindow appWindow = AppWindow.GetFromWindowId(windowId); var size = new SizeInt32(width, height); appWindow.Resize(size); }
public void UpdateWorldImage(RectI area, bool isRenderedLayer = false, string renderMsg = "Render Update Complete.", WriteableBitmap img = null) { // validate area area.Rebound(_world.Header.WorldBounds); int width = area.Width; int height = area.Height; int rts = isRenderedLayer ? 8 : 1; string renderProgressMsg = isRenderedLayer ? "Rendering Textured World..." : "Rendering Pixel World..."; if (img == null) { img = isRenderedLayer ? _worldImage.Rendered : _worldImage.Image; } var pixels = new BytePixels(area.Size * rts, 4); var stride = img.PixelWidth * img.Format.BitsPerPixel / 8; for (int x = area.X; x <= area.Right; x++) { int dx = x - area.X; if (renderMsg != null) { OnProgressChanged(this, dx, width, renderProgressMsg); } for (int y = area.Y; y <= area.Bottom; y++) { int dy = y - area.Y; Tile tile = _world.Tiles[x, y]; if (tile != null) { var xy = (new PointInt32(x, y) - area.TopLeft) * rts; var bp = isRenderedLayer ? GetTexture(y, tile) : GetTextureLayer("TilePixel", y, tile); bp.PutData(pixels, xy); } } } SizeInt32 ts = new SizeInt32(rts, rts); var realArea = isRenderedLayer ? new RectI(new PointInt32(), area.Size * ts) : area; // Rendered layer starts at 0,0 img.Lock(); img.WritePixels(realArea, pixels.GetData(), stride, 0); if (!isRenderedLayer) { img.AddDirtyRect(realArea); } img.Unlock(); if (renderMsg != null) { OnProgressChanged(this, 100, 100, renderMsg); } }
public CaptureEngine(IDirect3DDevice device, GraphicsCaptureItem item) { _device = device; _d3dDevice = Direct3D11Helper.CreateSharpDXDevice(_device); _framePool = Direct3D11CaptureFramePool.Create( _device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, item.Size); _session = _framePool.CreateCaptureSession(item); _lastSize = item.Size; _framePool.FrameArrived += OnFrameArrived; }
private void InitializeCapture(SizeInt32 size) { _item.Closed += OnClosed; _framePool = Direct3D11CaptureFramePool.CreateFreeThreaded( _device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 1, size); _framePool.FrameArrived += OnFrameArrived; _session = _framePool.CreateCaptureSession(_item); _session.StartCapture(); }
public override void PerformAction(AppWorkspace appWorkspace) { if (appWorkspace.CanSetActiveWorkspace) { using (NewFileDialog dialog = new NewFileDialog()) { SizeInt32?clipboardImageSize; SizeInt32 newDocumentSize = appWorkspace.GetNewDocumentSize(); using (new WaitCursorChanger(appWorkspace)) { CleanupManager.RequestCleanup(); try { IPdnDataObject dataObject = PdnClipboard.GetDataObject(); clipboardImageSize = ClipboardUtil.GetClipboardImageSize(appWorkspace, dataObject); dataObject = null; } catch (Exception) { clipboardImageSize = null; } CleanupManager.RequestCleanup(); } if (clipboardImageSize.HasValue) { newDocumentSize = clipboardImageSize.Value; } dialog.OriginalSize = new Size(newDocumentSize.Width, newDocumentSize.Height); dialog.OriginalDpuUnit = AppSettings.Instance.Workspace.LastNonPixelUnits.Value; dialog.OriginalDpu = Document.GetDefaultDpu(dialog.OriginalDpuUnit); dialog.Units = dialog.OriginalDpuUnit; dialog.Resolution = dialog.OriginalDpu; dialog.ConstrainToAspect = AppSettings.Instance.Workspace.LastMaintainAspectRatioNF.Value; if ((((dialog.ShowDialog(appWorkspace) == DialogResult.OK) && (dialog.ImageWidth > 0)) && ((dialog.ImageHeight > 0) && dialog.Resolution.IsFinite())) && (dialog.Resolution > 0.0)) { SizeInt32 size = new SizeInt32(dialog.ImageWidth, dialog.ImageHeight); if (appWorkspace.CreateBlankDocumentInNewWorkspace(size, dialog.Units, dialog.Resolution, false)) { appWorkspace.ActiveDocumentWorkspace.ZoomBasis = ZoomBasis.FitToWindow; AppSettings.Instance.Workspace.LastMaintainAspectRatioNF.Value = dialog.ConstrainToAspect; if (dialog.Units != MeasurementUnit.Pixel) { AppSettings.Instance.Workspace.LastNonPixelUnits.Value = dialog.Units; } if (appWorkspace.Units != MeasurementUnit.Pixel) { appWorkspace.Units = dialog.Units; } } } } } }
public CancellableBitmapSource(IBitmapSource <TPixel> source, Func <RectInt32, IEnumerable <RectInt32> > sourceRectSplitter, Action <RectInt32> rectCompletedCallback, ICancellationToken cancelToken) { Validate.Begin().IsNotNull <IBitmapSource <TPixel> >(source, "source").IsNotNull <Func <RectInt32, IEnumerable <RectInt32> > >(sourceRectSplitter, "sourceRectSplitter").IsNotNull <ICancellationToken>(cancelToken, "cancelToken").Check(); this.source = source.CreateRef <TPixel>(); this.sourceSize = this.source.Size; this.sourceRectSplitter = sourceRectSplitter; this.rectCompletedCallback = rectCompletedCallback; this.cancelToken = cancelToken; TPixel local = default(TPixel); this.bytesPerPixel = local.BytesPerPixel; }
private void ProcessFrame(Direct3D11CaptureFrame frame) { // Resize and device-lost leverage the same function on the // Direct3D11CaptureFramePool. Refactoring it this way avoids // throwing in the catch block below (device creation could always // fail) along with ensuring that resize completes successfully and // isn’t vulnerable to device-lost. bool needsReset = false; bool recreateDevice = false; if ((frame.ContentSize.Width != _lastSize.Width) || (frame.ContentSize.Height != _lastSize.Height)) { needsReset = true; _lastSize = frame.ContentSize; } try { canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface( _canvasDevice, frame.Surface); foreach (var clip in ClipImages.Values) { clip.Resize(_canvasDevice, false); } using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession()) { ds.Clear(Colors.Black); ds.DrawImage(canvasBitmap); } foreach (var clip in ClipImages.Values) { clip.Rendar(canvasBitmap); } } // This is the device-lost convention for Win2D. catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult)) { // We lost our graphics device. Recreate it and reset // our Direct3D11CaptureFramePool. needsReset = true; recreateDevice = true; } if (needsReset) { ResetFramePool(frame.ContentSize, recreateDevice); } }