Exemplo n.º 1
0
        /**
         * 後始末
         */
        private void OnUnloaded(object sender, RoutedEventArgs e)
        {
            MediaPlayer.Pause();
            if (null != mTruckingTimer)
            {
                mTruckingTimer.Stop();
            }
            MediaPlayer.MediaOpened         -= MP_MediaOpened;
            MediaPlayer.VideoFrameAvailable -= MP_FrameAvailable;
            //PlaybackSession.PositionChanged -= PBS_PositionChanged;
            PlaybackSession.SeekCompleted        -= PBS_SeekCompletedForExtractFrames;
            PlaybackSession.PlaybackStateChanged -= PBS_PlaybackStateChanged;
            mMoviePlayer.UnregisterPropertyChangedCallback(MediaPlayerElement.IsFullWindowProperty, mFullWindowListenerToken);

            mMarkerView.MarkerSelected -= MV_MarkerSelected;
            mMarkerView.MarkerAdded    -= MV_MarkerAdded;
            mMarkerView.MarkerRemoved  -= MV_MarkerRemoved;

            mMoviePlayer.SetMediaPlayer(null);
            if (null != mFrameServerDest)
            {
                mFrameServerDest.Dispose();
                mFrameServerDest   = null;
                mCanvasImageSource = null;
            }
        }
Exemplo n.º 2
0
        private async Task NextFrame()
        {
            mediaPlayer.StepForwardOneFrame();
            SoftwareBitmap frameServerDest = null;
            SoftwareBitmap sb = null;
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();
                frameServerDest           = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)1200, (int)1200, BitmapAlphaMode.Premultiplied);
                var canvasImageSource     = new CanvasImageSource(canvasDevice, (int)1200, (int)1200, DisplayInformation.GetForCurrentView().LogicalDpi);//96);



                using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest))
                    using (CanvasDrawingSession ds = canvasImageSource.CreateDrawingSession(Windows.UI.Colors.Transparent))
                    {
                        mediaPlayer.CopyFrameToVideoSurface(inputBitmap);
                        ds.DrawImage(inputBitmap);



                        frameServerDest = SoftwareBitmap.CreateCopyFromSurfaceAsync(inputBitmap, BitmapAlphaMode.Premultiplied).AsTask().Result;
                    }
                ImagePreview.Source = canvasImageSource;
            });


            VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(frameServerDest);

            // Evaluate the image

            await EvaluateVideoFrameAsync(inputImage);
        }
Exemplo n.º 3
0
        public static CanvasImageSource PrintImage(float[] data)
        {
            if (data.Length == 784)
            {
                var device       = CanvasDevice.GetSharedDevice();
                var startPoint   = new Vector2();
                var renderTarget = new CanvasImageSource(device, 280, 280, 96f);

                using (var drawingSession = renderTarget.CreateDrawingSession(Colors.White))
                {
                    for (int j = 0; j < 28; j++)
                    {
                        startPoint.X = 0;
                        for (int k = 0; k < 28; k++)
                        {
                            if (data[j * 28 + k] == 1)
                            {
                                drawingSession.FillRectangle(startPoint.X, startPoint.Y, 10, 10, Colors.Black);
                            }
                            startPoint.X += 10;
                        }
                        startPoint.Y += 10;
                    }
                }
                return(renderTarget);
            }
            return(null);
        }
Exemplo n.º 4
0
        public ChartViewModel()
        {
            _editor = new ImageEditor();
            if (DesignMode.DesignModeEnabled)
            {
                ImageDisplayName = "Design Mode";
            }

            var device = CanvasDevice.GetSharedDevice();

            ImageSource = new CanvasRenderTarget(device, 1000, 1000, 96);
            //var service = new ImageService(ImageSource);
            //service.ClearScreen(Colors.Orange);
            using (var session = ImageSource.CreateDrawingSession())
            {
                session.Clear(Colors.Orange);
            }

            CanvasImageSource cis = new CanvasImageSource(device, 1000, 1000, 96);


            ImageTarget = new CanvasImageSource(device, 1000, 1000, 96);
            using (var session = ImageTarget.CreateDrawingSession(Colors.Blue))
            {
                session.DrawImage(ImageSource);
            }

            FlipSource = new List <CanvasImageSource>();
            FlipSource.Add(ImageTarget);
        }
        private WindowsImageSource LoadImage(ImageSource imagesource)
        {
            if (!(imagesource is IconImageSource iconsource) || !IconFontRegistry.Instance.TryFindIconForKey(iconsource.Name, out var icon))
            {
                return(null);
            }

            var device     = CanvasDevice.GetSharedDevice();
            var dpi        = Math.Max(_minimumDpi, Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi);
            var canvasSize = (float)iconsource.Size + 2;

            var canvas = new CanvasImageSource(device, canvasSize, canvasSize, dpi);

            using (var ds = canvas.CreateDrawingSession(Windows.UI.Colors.Transparent))
            {
                var textFormat = new CanvasTextFormat
                {
                    FontFamily          = icon.FontFamily,
                    FontSize            = (float)iconsource.Size,
                    HorizontalAlignment = CanvasHorizontalAlignment.Center,
                    Options             = CanvasDrawTextOptions.Default,
                };

                var iconcolor = ToWindowsColor(iconsource.Color != Color.Default ? iconsource.Color : Color.White);
                ds.DrawText(icon.Glyph, textFormat.FontSize / 2, 0, iconcolor, textFormat);
            }

            return(canvas);
        }
Exemplo n.º 6
0
        private async Task mediaPlayer_VideoFrameAvailableToExtractFrames(MediaPlayer mediaPlayer, object args)
        {
            CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                var canvasImageSrc = new CanvasImageSource(canvasDevice, (int)ThumbnailSize.Width, (int)ThumbnailSize.Height, DisplayInformation.GetForCurrentView().LogicalDpi);//96);
                using (SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)ThumbnailSize.Width, (int)ThumbnailSize.Height, BitmapAlphaMode.Ignore))
                    using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, softwareBitmap))
                        using (CanvasDrawingSession ds = canvasImageSrc.CreateDrawingSession(Windows.UI.Colors.Black))
                        {
                            try
                            {
                                Debug.WriteLine(string.Format("...Extract Position:{0} (State={1})", mediaPlayer.PlaybackSession.Position, mediaPlayer.PlaybackSession.PlaybackState.ToString()));

                                mediaPlayer.CopyFrameToVideoSurface(inputBitmap);
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine(e);
                                return;
                            }
                            ds.DrawImage(inputBitmap);
                            Frames.Add(canvasImageSrc);
                        }
            });
        }
Exemplo n.º 7
0
        protected override void DrawFrame(CanvasImageSource sender, CanvasDrawingSession args)
        {
            if (_bitmap == null || _animation == null || _unloaded)
            {
                return;
            }

            if (_flipped)
            {
                args.Transform = Matrix3x2.CreateScale(-1, 1, sender.Size.ToVector2() / 2);
            }

            if (sender.Size.Width >= _bitmap.Size.Width || sender.Size.Height >= _bitmap.Size.Height)
            {
                args.DrawImage(_bitmap,
                               new Rect(0, 0, sender.Size.Width, sender.Size.Height));
            }
            else
            {
                args.DrawImage(_bitmap,
                               new Rect(0, 0, sender.Size.Width, sender.Size.Height),
                               new Rect(0, 0, _bitmap.Size.Width, _bitmap.Size.Height), 1,
                               CanvasImageInterpolation.MultiSampleLinear);
            }

            if (_hideThumbnail)
            {
                _hideThumbnail = false;

                FirstFrameRendered?.Invoke(this, EventArgs.Empty);
                ElementCompositionPreview.SetElementChildVisual(this, null);
            }
        }
Exemplo n.º 8
0
        private void canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            arrow = MakeDirectionIcon(sender);

            var bounds  = arrow.ComputeStrokeBounds(3);
            var outline = arrow.Stroke(2).Outline();

            var foregroundBrush = (SolidColorBrush)directionsCombo.Foreground;
            var color           = foregroundBrush.Color;

            Directions = new List <DirectionInfo>();

            foreach (CanvasTextDirection direction in Enum.GetValues(typeof(CanvasTextDirection)))
            {
                var arrowImage = new CanvasImageSource(sender, 64, 64);
                using (var ds = arrowImage.CreateDrawingSession(Colors.Transparent))
                {
                    var directionTransform = GetDirectionTransform(direction);
                    ds.Transform = directionTransform * Matrix3x2.CreateTranslation((float)(32 - bounds.Width / 2), (float)(32 - bounds.Height / 2));

                    ds.DrawGeometry(arrow, color, 1);
                }

                Directions.Add(new DirectionInfo(direction, arrowImage));
            }

            // Poke the property so that the control gets a chance to pick up the new images
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Directions"));
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CurrentDirectionIndex"));
            }
        }
Exemplo n.º 9
0
        //<SnippetVideoFrameAvailable>
        private async void mediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
        {
            CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (frameServerDest == null)
                {
                    // FrameServerImage in this example is a XAML image control
                    frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)FrameServerImage.Width, (int)FrameServerImage.Height, BitmapAlphaMode.Ignore);
                }
                if (canvasImageSource == null)
                {
                    canvasImageSource       = new CanvasImageSource(canvasDevice, (int)FrameServerImage.Width, (int)FrameServerImage.Height, DisplayInformation.GetForCurrentView().LogicalDpi);//96);
                    FrameServerImage.Source = canvasImageSource;
                }

                using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest))
                    using (CanvasDrawingSession ds = canvasImageSource.CreateDrawingSession(Windows.UI.Colors.Black))
                    {
                        mediaPlayer.CopyFrameToVideoSurface(inputBitmap);

                        var gaussianBlurEffect = new GaussianBlurEffect
                        {
                            Source       = inputBitmap,
                            BlurAmount   = 5f,
                            Optimization = EffectOptimization.Speed
                        };

                        ds.DrawImage(gaussianBlurEffect);
                    }
            });
        }
Exemplo n.º 10
0
        private void OnCreateResources(CanvasControl sender, object args)
        {
            imageSource  = new CanvasImageSource(sender, width, height);
            image.Source = imageSource;

            Draw(0, 0, width, height, Colors.Blue);
        }
        public Task <Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesource,
                                                                       CancellationToken cancelationToken = default(CancellationToken))
        {
            if (!(imagesource is FontImageSource fontsource))
            {
                return(null);
            }

            var device     = CanvasDevice.GetSharedDevice();
            var dpi        = Math.Max(_minimumDpi, Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi);
            var canvasSize = (float)fontsource.Size + 2;

            var imageSource = new CanvasImageSource(device, canvasSize, canvasSize, dpi);

            using (var ds = imageSource.CreateDrawingSession(Windows.UI.Colors.Transparent))
            {
                var textFormat = new CanvasTextFormat
                {
                    FontFamily          = fontsource.FontFamily,
                    FontSize            = (float)fontsource.Size,
                    HorizontalAlignment = CanvasHorizontalAlignment.Center,
                    Options             = CanvasDrawTextOptions.Default,
                };
                var iconcolor = (fontsource.Color != Color.Default ? fontsource.Color : Color.White).ToWindowsColor();

                // offset by 1 as we added a 1 inset
                var x = textFormat.FontSize / 2f + 1f;
                var y = -1f;
                ds.DrawText(fontsource.Glyph, x, y, iconcolor, textFormat);
            }

            return(Task.FromResult((Windows.UI.Xaml.Media.ImageSource)imageSource));
        }
Exemplo n.º 12
0
            private void CreateCanvasResources()
            {
                const float desiredDpi = 96.0f; // GIF does not encode DPI

                var sharedDevice = GetSharedDevice();
                var pixelWidth   = _imageProperties.PixelWidth;
                var pixelHeight  = _imageProperties.PixelHeight;

                _canvasImageSource = new CanvasImageSource(
                    sharedDevice,
                    pixelWidth,
                    pixelHeight,
                    desiredDpi,
                    CanvasAlphaMode.Premultiplied
                    );
                _accumulationRenderTarget = new CanvasRenderTarget(
                    sharedDevice,
                    pixelWidth,
                    pixelHeight,
                    desiredDpi,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    CanvasAlphaMode.Premultiplied
                    );

                _hasCanvasResources = true;
            }
Exemplo n.º 13
0
            public ImageSource RecreateSurfaces()
            {
                if (_hasCanvasResources)
                {
                    var sharedDevice = GetSharedDevice();

                    _canvasImageSource = new CanvasImageSource(
                        sharedDevice,
                        _imageProperties.PixelWidth,
                        _imageProperties.PixelHeight,
                        _canvasImageSource.Dpi,
                        _canvasImageSource.AlphaMode
                        );
                    _accumulationRenderTarget = new CanvasRenderTarget(
                        sharedDevice,
                        _imageProperties.PixelWidth,
                        _imageProperties.PixelHeight,
                        _accumulationRenderTarget.Dpi,
                        _accumulationRenderTarget.Format,
                        _accumulationRenderTarget.AlphaMode
                        );

                    if (_isAnimating)
                    {
                        StartAnimation(); // Reset animation as the accumulation buffer is invalid
                    }

                    return(_canvasImageSource);
                }
                else
                {
                    return(null);
                }
            }
Exemplo n.º 14
0
        /**
         * カレント位置のサムネイルを作成する。
         */
        private async void createThumbnailCore()
        {
            if (mDoneOnce)
            {
                return;
            }
            mDoneOnce = true;

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();

                // Debug.WriteLine(mPlayerElement.MediaPlayer.PlaybackSession.Position);

                double mw             = mPlayerElement.MediaPlayer.PlaybackSession.NaturalVideoWidth, mh = mPlayerElement.MediaPlayer.PlaybackSession.NaturalVideoHeight;
                var limit             = Math.Min(Math.Max(mh, mw), 1024);
                var playerSize        = calcFittingSize(mw, mh, limit, limit);
                var canvasImageSource = new CanvasImageSource(canvasDevice, (int)playerSize.Width, (int)playerSize.Height, DisplayInformation.GetForCurrentView().LogicalDpi);//96);

                using (var frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)playerSize.Width, (int)playerSize.Height, BitmapAlphaMode.Ignore))
                    using (CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest))
                    {
                        mPlayerElement.MediaPlayer.CopyFrameToVideoSurface(canvasBitmap);
                        // ↑これで、frameServerDest に描画されるのかと思ったが、このframeServerDestは、単に、空のCanvasBitmapを作るための金型であって、
                        // 実際の描画は、CanvasBitmap(IDirect3DSurface)に対してのみ行われ、frameServerDestはからBitmapを作っても、黒い画像しか取り出せない。
                        // このため、有効な画像を取り出すには、CangasBitmapから softwareBitmapを生成してエンコードする必要がある。

                        using (var softwareBitmap = await SoftwareBitmap.CreateCopyFromSurfaceAsync(canvasBitmap))
                        {
                            var stream            = new InMemoryRandomAccessStream();
                            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                            encoder.SetSoftwareBitmap(softwareBitmap);

                            try
                            {
                                await encoder.FlushAsync();
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine(e);
                                stream.Dispose();
                                stream = null;
                            }

                            if (null != mOnSelected)
                            {
                                mOnSelected(this, mPlayerElement.MediaPlayer.PlaybackSession.Position.TotalMilliseconds, stream);
                                closeDialog();
                            }
                            else
                            {
                                stream.Dispose();
                            }
                        }
                    }
            });
        }
Exemplo n.º 15
0
 public Eyedropper()
 {
     this.DefaultStyleKey = typeof(Eyedropper);
     RenderTransform      = _layoutTransform;
     HorizontalAlignment  = HorizontalAlignment.Left;
     VerticalAlignment    = VerticalAlignment.Top;
     IsHitTestVisible     = false;
     _previewImageSource  = new CanvasImageSource(_device, PreviewPixelWidth, PreviewPixelWidth, 96f);
     Preview = _previewImageSource;
 }
Exemplo n.º 16
0
        private void UserControl_Loaded(object sender, RoutedEventArgs args)
        {
            CompositionTarget.SurfaceContentsLost += CompositionTarget_SurfaceContentsLost;

            var device = CanvasDevice.GetSharedDevice();
            imageSource = new CanvasImageSource(device, width, height, dpi);
            image.Source = imageSource;

            UpdateImageSource(true);
        }
Exemplo n.º 17
0
        // Draw to the CanvasImageSource.
        void DrawToImageSource(Size size)
        {
            var imageSource = new CanvasImageSource(canvasControl, (float)size.Width, (float)size.Height);

            using (var ds = imageSource.CreateDrawingSession(Colors.Yellow))
            {
                DrawTextLabel(ds, "Canvas\nImage\nSource", size);
            }

            imageControl.Source = imageSource;
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs args)
        {
            CompositionTarget.SurfaceContentsLost += CompositionTarget_SurfaceContentsLost;

            var device = CanvasDevice.GetSharedDevice(false);

            imageSource  = new CanvasImageSource(device, width, height, dpi);
            image.Source = imageSource;

            UpdateImageSource(true);
        }
Exemplo n.º 19
0
        void Canvas_CreateResources(CanvasControl sender, object args)
        {
            mainDeviceResources = new PerDeviceResources(sender);

            imageBrush = new CanvasImageBrush(sender);

            imageSource         = new CanvasImageSource(sender, controlSize, controlSize);
            imageControl.Source = imageSource;

            swapChain = new CanvasSwapChain(sender, controlSize, controlSize);
            swapChainPanel.SwapChain = swapChain;
        }
Exemplo n.º 20
0
        protected override void DrawFrame(CanvasImageSource sender, CanvasDrawingSession args)
        {
            if (_bitmap == null || _animation == null || _unloaded)
            {
                return;
            }

            if (_flipped)
            {
                args.Transform = Matrix3x2.CreateScale(-1, 1, sender.Size.ToVector2() / 2);
            }

            double width  = _bitmap.Size.Width;
            double height = _bitmap.Size.Height;

            double ratioX = (double)sender.Size.Width / width;
            double ratioY = (double)sender.Size.Height / height;

            if (ratioX > ratioY)
            {
                width   = sender.Size.Width;
                height *= ratioX;
            }
            else
            {
                width *= ratioY;
                height = sender.Size.Height;
            }

            var y = (sender.Size.Height - height) / 2;
            var x = (sender.Size.Width - width) / 2;

            if (sender.Size.Width >= _logicalSize.Width || sender.Size.Height >= _logicalSize.Height)
            {
                args.DrawImage(_bitmap,
                               new Rect(x, y, width, height));
            }
            else
            {
                args.DrawImage(_bitmap,
                               new Rect(x, y, width, height),
                               new Rect(0, 0, _bitmap.Size.Width, _bitmap.Size.Height), 1,
                               CanvasImageInterpolation.MultiSampleLinear);
            }

            if (_hideThumbnail == true)
            {
                _hideThumbnail = false;

                FirstFrameRendered?.Invoke(this, EventArgs.Empty);
                ElementCompositionPreview.SetElementChildVisual(this, null);
            }
        }
Exemplo n.º 21
0
        private async void MediaPlayer_VideoFrameAvailableAsync(MediaPlayer sender, object args)
        {
            if (!isRenderringFinished)
            {
                return;
            }
            isRenderringFinished = false;
            //CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice();

            if (frameServerDest == null)
            {
                frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)sender.PlaybackSession.NaturalVideoWidth, (int)sender.PlaybackSession.NaturalVideoHeight, BitmapAlphaMode.Ignore);
            }
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                canvasImageSource  = new CanvasImageSource(canvasDevice, (int)sender.PlaybackSession.NaturalVideoWidth, (int)sender.PlaybackSession.NaturalVideoWidth, DisplayInformation.GetForCurrentView().LogicalDpi);//96);
                OutputImage.Source = canvasImageSource;

                try
                {
                    using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest))
                    //using (CanvasDrawingSession ds = canvasImageSource.CreateDrawingSession(Colors.Black))
                    {
                        sender.CopyFrameToVideoSurface(inputBitmap);

                        using (var cds = canvasRenderTarget.CreateDrawingSession())
                        {
                            cds.DrawImage(inputBitmap, canvasRenderTarget.Bounds);
                        }

                        var pixelBytes = canvasRenderTarget.GetPixelBytes();

                        if (pixelBytes[40] == 0)
                        {
                            System.Diagnostics.Debug.WriteLine("fuckzero");
                        }

                        var boxes = await DetectObjectPoseFromImagePixelsAsync(pixelBytes).ConfigureAwait(true);

                        DrawBoxes(inputBitmap, boxes, canvasImageSource);
                        //ds.DrawImage(inputBitmap);
                    }
                }
                catch (Exception exception)
                {
                    System.Diagnostics.Debug.WriteLine(exception.Message);
                }
            });

            isRenderringFinished = true;
        }
Exemplo n.º 22
0
        public Task <Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesource,
                                                                       CancellationToken cancelationToken = default(CancellationToken))
        {
            if (!(imagesource is FontImageSource fontsource))
            {
                return(null);
            }

            var device = CanvasDevice.GetSharedDevice();
            var dpi    = Math.Max(_minimumDpi, Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi);

            // There's really no perfect solution to handle font families with fallbacks (comma-separated)
            // So if the font family has fallbacks, only the first one is taken, because CanvasTextFormat
            // only supports one font family

            var fontFamily  = fontsource.FontFamily.ToFontFamily();
            var allFamilies = fontFamily.Source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            if (allFamilies.Length < 1)
            {
                return(null);
            }

            var textFormat = new CanvasTextFormat
            {
                FontFamily          = allFamilies[0],
                FontSize            = (float)fontsource.Size,
                HorizontalAlignment = CanvasHorizontalAlignment.Center,
                VerticalAlignment   = CanvasVerticalAlignment.Center,
                Options             = CanvasDrawTextOptions.Default
            };

            using (var layout = new CanvasTextLayout(device, fontsource.Glyph, textFormat, (float)fontsource.Size, (float)fontsource.Size))
            {
                var canvasWidth  = (float)layout.LayoutBounds.Width + 2;
                var canvasHeight = (float)layout.LayoutBounds.Height + 2;

                var imageSource = new CanvasImageSource(device, canvasWidth, canvasHeight, dpi);
                using (var ds = imageSource.CreateDrawingSession(Windows.UI.Colors.Transparent))
                {
                    var iconcolor = (fontsource.Color != Color.Default ? fontsource.Color : Color.White).ToWindowsColor();

                    // offset by 1 as we added a 1 inset
                    var x = (float)layout.DrawBounds.X * -1;

                    ds.DrawTextLayout(layout, x, 1f, iconcolor);
                }

                return(Task.FromResult((Windows.UI.Xaml.Media.ImageSource)imageSource));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Eyedropper"/> class.
        /// </summary>
        public Eyedropper()
        {
            DefaultStyleKey = typeof(Eyedropper);
            _rootGrid       = new Grid();
            _targetGrid     = new Grid
            {
                Background = new SolidColorBrush(Color.FromArgb(0x01, 0x00, 0x00, 0x00))
            };

            RenderTransform     = _layoutTransform;
            _previewImageSource = new CanvasImageSource(_device, PreviewPixelsPerRawPixel * PixelCountPerRow, PreviewPixelsPerRawPixel * PixelCountPerRow, 96f);
            Preview             = _previewImageSource;
            Loaded += Eyedropper_Loaded;
        }
Exemplo n.º 24
0
        void Canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            args.TrackAsyncAction(LoadCanvasResources(sender).AsAsyncAction());

            imageBrush = new CanvasImageBrush(sender);

            imageSource         = new CanvasImageSource(sender, controlSize, controlSize);
            imageControl.Source = imageSource;

            virtualImageSource         = new CanvasVirtualImageSource(sender, controlSize, controlSize);
            virtualImageControl.Source = virtualImageSource.Source;

            swapChain = new CanvasSwapChain(sender, controlSize, controlSize);
            swapChainPanel.SwapChain = swapChain;
        }
Exemplo n.º 25
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         this.Stop();
         _isInitialized      = false;
         _hasCanvasResources = false;
         _bitmapDecoder      = null;
         _frameProperties?.Clear();
         _frameProperties          = null;
         _canvasImageSource        = null;
         _accumulationRenderTarget = null;
         _animationTimer           = null;
     }
 }
Exemplo n.º 26
0
        // Draw to the CanvasImageSource.
        void DrawToImageSource(Size size)
        {
            if (size.Width <= 0 || size.Height <= 0)
            {
                imageControl.Source = null;
                return;
            }

            imageSource = new CanvasImageSource(canvasControl, (float)size.Width, (float)size.Height);

            using (var ds = imageSource.CreateDrawingSession(Colors.Transparent))
            {
                Draw(ds, "Canvas\nImage\nSource", size);
            }

            imageControl.Source = imageSource;
        }
Exemplo n.º 27
0
        // Draw to the CanvasImageSource.
        void DrawToImageSource(Size size)
        {
            if (size.Width <= 0 || size.Height <= 0)
            {
                imageControl.Source = null;
                return;
            }

            imageSource = new CanvasImageSource(canvasControl, size);

            using (var ds = imageSource.CreateDrawingSession(Colors.Transparent))
            {
                Draw(ds, "Canvas\nImage\nSource", size);
            }

            imageControl.Source = imageSource;
        }
Exemplo n.º 28
0
        public SisPageRenderer(DjvuPage page, Size pageViewSize)
        {
            _page = page;
            var rawPixelsPerViewPixel = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
            var width  = Math.Min(pageViewSize.Width, page.Width / rawPixelsPerViewPixel);
            var height = Math.Min(pageViewSize.Height, page.Height / rawPixelsPerViewPixel);

            Source = new CanvasImageSource(
                resourceCreator: CanvasDevice.GetSharedDevice(),
                width: (float)width,
                height: (float)height,
                dpi: DisplayInformation.GetForCurrentView().LogicalDpi,
                alphaMode: CanvasAlphaMode.Ignore);

            RenderRegion(new Rect(0, 0, width, height));

            _page = null;
        }
Exemplo n.º 29
0
        public Task <Microsoft.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesource,
                                                                         CancellationToken cancelationToken = default(CancellationToken))
        {
            if (!(imagesource is FontImageSource fontsource))
            {
                return(null);
            }

            var device = CanvasDevice.GetSharedDevice();

            // https://github.com/microsoft/microsoft-ui-xaml/issues/4205#issuecomment-780204896
            // var dpi = Math.Max(_minimumDpi, Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi);

            var dpi = _minimumDpi;

            var textFormat = new CanvasTextFormat
            {
                FontFamily          = GetFontSource(fontsource),
                FontSize            = (float)fontsource.Size,
                HorizontalAlignment = CanvasHorizontalAlignment.Center,
                VerticalAlignment   = CanvasVerticalAlignment.Center,
                Options             = CanvasDrawTextOptions.Default
            };

            using (var layout = new CanvasTextLayout(device, fontsource.Glyph, textFormat, (float)fontsource.Size, (float)fontsource.Size))
            {
                var canvasWidth  = (float)layout.LayoutBounds.Width + 2;
                var canvasHeight = (float)layout.LayoutBounds.Height + 2;

                var imageSource = new CanvasImageSource(device, canvasWidth, canvasHeight, dpi);
                using (var ds = imageSource.CreateDrawingSession(Microsoft.UI.Colors.Transparent))
                {
                    var iconcolor = (fontsource.Color != null ? fontsource.Color : Colors.White).ToWindowsColor();

                    // offset by 1 as we added a 1 inset
                    var x = (float)layout.DrawBounds.X * -1;

                    ds.DrawTextLayout(layout, x, 1f, iconcolor);
                }

                return(Task.FromResult((UI.Xaml.Media.ImageSource)imageSource));
            }
        }
Exemplo n.º 30
0
        internal CanvasImageSource RenderImageSource(IFontImageSource imageSource, float scale)
        {
            // TODO: The DPI not working as the view is not respecting the
            //       value, so just reset to 1 to keep the correct size.
            scale = 1;

            var dpi = scale * DeviceDisplay.BaseLogicalDpi;

            var fontFamily = GetFontSource(imageSource);
            var fontSize   = (float)imageSource.Font.Size;
            var color      = (imageSource.Color ?? Colors.White).ToWindowsColor();

            var textFormat = new CanvasTextFormat
            {
                FontFamily          = fontFamily,
                FontSize            = fontSize,
                HorizontalAlignment = CanvasHorizontalAlignment.Center,
                VerticalAlignment   = CanvasVerticalAlignment.Center,
                Options             = CanvasDrawTextOptions.Default
            };

            var device = CanvasDevice.GetSharedDevice();

            using var layout = new CanvasTextLayout(device, imageSource.Glyph, textFormat, 0, 0);

            // add a 1px padding all around
            var canvasWidth  = (float)layout.DrawBounds.Width + 2;
            var canvasHeight = (float)layout.DrawBounds.Height + 2;

            var canvasImageSource = new CanvasImageSource(device, canvasWidth, canvasHeight, dpi);

            using (var ds = canvasImageSource.CreateDrawingSession(UI.Colors.Transparent))
            {
                // offset by 1px as we added a 1px padding
                var x = (layout.DrawBounds.X * -1) + 1;
                var y = (layout.DrawBounds.Y * -1) + 1;

                ds.DrawTextLayout(layout, (float)x, (float)y, color);
            }

            return(canvasImageSource);
        }
Exemplo n.º 31
0
        private async Task <CanvasImageSource> SvgToImageSource(SvgElement svg)
        {
            if (svg == null)
            {
                return(null);
            }
            var device = CanvasDevice.GetSharedDevice();

            using (var win2DSvg = await Win2DSvgElement.Parse(device, svg))
            {
                win2DSvg.Progress = 1;
                var offScreen = new CanvasImageSource(device, (float)_cacheImageResolution.Width, (float)_cacheImageResolution.Height, 96);
                using (var drawingSession = offScreen.CreateDrawingSession(Colors.Transparent))
                {
                    drawingSession.Transform = Matrix3x2.CreateTranslation(new Vector2(-(float)svg.ViewBox.X, -(float)svg.ViewBox.Y)) * Matrix3x2.CreateScale((float)(200 / Math.Max(svg.ViewBox.Width, svg.ViewBox.Height)));
                    win2DSvg.Draw(drawingSession, 0);
                }
                return(offScreen);
            }
        }
Exemplo n.º 32
0
        /**
         * 動画ファイルのオープンに成功したのち、フレームサムネイルの抽出を開始する。
         */
        private void beginExtractFrames()
        {
            if (null != mFrameServerDest)
            {
                mFrameServerDest.Dispose();
                mFrameServerDest   = null;
                mCanvasImageSource = null;
            }
            double total = MediaPlayer.PlaybackSession.NaturalDuration.TotalMilliseconds;

            mSpan   = total / mFrameCount;
            mOffset = mSpan / 2;
            mFrame  = 0;
            var videoSize = new Size(MediaPlayer.PlaybackSession.NaturalVideoWidth, MediaPlayer.PlaybackSession.NaturalVideoHeight);

            CTX.VideoSize         = videoSize;
            mThumbnailSize.Height = ThumbnailHeight;
            mThumbnailSize.Width  = videoSize.Width * mThumbnailSize.Height / videoSize.Height;
            MediaPlayer.PlaybackSession.Position = TimeSpan.FromMilliseconds(mOffset);
        }
    public static ImageSource ToQrImageSource(this ISdp sdp, int width, int height)
    {
      var qrCodeWriter = new QRCodeWriter();
      var bitMatrix = qrCodeWriter.encode(sdp.ToString(), ZXing.BarcodeFormat.QR_CODE, width, height);

      var canvasImageSource = new CanvasImageSource(CanvasDevice.GetSharedDevice(), width, height, 96);
      using (var drawingSession = canvasImageSource.CreateDrawingSession(Color.FromArgb(255, 255, 255, 255)))
      {
        for (var y = 0; y < height; y++)
        {
          for (var x = 0; x < width; x++)
          {
            if (bitMatrix.get(x, y))
            {
              drawingSession.DrawRectangle(x, y, 1, 1, Color.FromArgb(255, 0, 0, 0));
            }
          }
        }
      }

      return canvasImageSource;
    }
            private void CreateCanvasResources()
            {
                const float desiredDpi = 96.0f; // GIF does not encode DPI

                var sharedDevice = GetSharedDevice();
                var pixelWidth = _imageProperties.PixelWidth;
                var pixelHeight = _imageProperties.PixelHeight;

                _canvasImageSource = new CanvasImageSource(
                    sharedDevice,
                    pixelWidth,
                    pixelHeight,
                    desiredDpi,
                    CanvasAlphaMode.Premultiplied
                    );
                _accumulationRenderTarget = new CanvasRenderTarget(
                    sharedDevice,
                    pixelWidth,
                    pixelHeight,
                    desiredDpi,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    CanvasAlphaMode.Premultiplied
                    );

                _hasCanvasResources = true;
            }
Exemplo n.º 35
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         this.Stop();
         _isInitialized = false;
         _hasCanvasResources = false;
         _bitmapDecoder = null;
         _frameProperties?.Clear();
         _frameProperties = null;
         _canvasImageSource = null;
         _accumulationRenderTarget = null;
         _animationTimer = null;
     }
 }
Exemplo n.º 36
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         this.Stop();
         _isInitialized = false;
         _hasCanvasResources = false;
         _bitmapDecoder = null;
         if (_frameProperties != null)
         {
             foreach (var item in _frameProperties)
             {
                 item.ReleasePixels();
             }
             _frameProperties.Clear();
             _frameProperties = null;
         }
         _canvasImageSource = null;
         _accumulationRenderTarget = null;
         _animationTimer = null;
     }
 }
Exemplo n.º 37
0
 public DirectionInfo(CanvasTextDirection direction, CanvasImageSource imageSource)
 {
     Direction = direction;
     ImageSource = imageSource;
 }
Exemplo n.º 38
0
        private void canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            arrow = MakeDirectionIcon(sender);

            var bounds = arrow.ComputeStrokeBounds(3);
            var outline = arrow.Stroke(2).Outline();

            var foregroundBrush = (SolidColorBrush)directionsCombo.Foreground;
            var color = foregroundBrush.Color;

            Directions = new List<DirectionInfo>();

            foreach (CanvasTextDirection direction in Enum.GetValues(typeof(CanvasTextDirection)))
            {
                var arrowImage = new CanvasImageSource(sender, 64, 64);
                using (var ds = arrowImage.CreateDrawingSession(Colors.Transparent))
                {

                    var directionTransform = GetDirectionTransform(direction);
                    ds.Transform = directionTransform * Matrix3x2.CreateTranslation((float)(32 - bounds.Width / 2), (float)(32 - bounds.Height / 2));

                    ds.DrawGeometry(arrow, color, 1);
                }

                Directions.Add(new DirectionInfo(direction, arrowImage));
            }

            // Poke the property so that the control gets a chance to pick up the new images
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Directions"));
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CurrentDirectionIndex"));
            }
        }
Exemplo n.º 39
0
        void Canvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            args.TrackAsyncAction(LoadCanvasResources(sender).AsAsyncAction());

            imageBrush = new CanvasImageBrush(sender);

            imageSource = new CanvasImageSource(sender, controlSize, controlSize);
            imageControl.Source = imageSource;

            virtualImageSource = new CanvasVirtualImageSource(sender, controlSize, controlSize);
            virtualImageControl.Source = virtualImageSource.Source;

            swapChain = new CanvasSwapChain(sender, controlSize, controlSize);
            swapChainPanel.SwapChain = swapChain;
        }
Exemplo n.º 40
0
        void Canvas_CreateResources(CanvasControl sender, object args)
        {
            mainDeviceResources = new PerDeviceResources(sender);

            imageBrush = new CanvasImageBrush(sender);

            imageSource = new CanvasImageSource(sender, controlSize, controlSize);
            imageControl.Source = imageSource;

            swapChain = new CanvasSwapChain(sender, controlSize, controlSize);
            swapChainPanel.SwapChain = swapChain;
        }
            public ImageSource RecreateSurfaces()
            {
                if (_hasCanvasResources)
                {
                    var sharedDevice = GetSharedDevice();

                    _canvasImageSource = new CanvasImageSource(
                        sharedDevice,
                        _imageProperties.PixelWidth,
                        _imageProperties.PixelHeight,
                        _canvasImageSource.Dpi,
                        _canvasImageSource.AlphaMode
                        );
                    _accumulationRenderTarget = new CanvasRenderTarget(
                        sharedDevice,
                        _imageProperties.PixelWidth,
                        _imageProperties.PixelHeight,
                        _accumulationRenderTarget.Dpi,
                        _accumulationRenderTarget.Format,
                        _accumulationRenderTarget.AlphaMode
                        );

                    if (_isAnimating)
                    {
                        StartAnimation(); // Reset animation as the accumulation buffer is invalid
                    }

                    return _canvasImageSource;
                }
                else
                {
                    return null;
                }
            }
Exemplo n.º 42
0
            internal async Task Go(CancellationToken cancellationToken, CanvasDevice device, ScenarioData scenarioData)
            {
                var runner = MakeScenarioRunner();
                scenarioData.AddDataToScenarioRunner(runner);

                // Create the image source and the render target.  These sizes are hardcoded and independent of the 
                // display's DPI since we just want a small image to convince ourselves that the scenarios really are 
                // rendering the right thing.
                Image = new CanvasImageSource(device, 128, 128, 96);

                using (var rt = new CanvasRenderTarget(device, 128, 128, 96))
                {
                    // We actually run the scenario on the thread pool - XAML really falls down if you overload the UI thread.
                    var time = await Task.Run(() =>
                    {
                        // Run the scenario multiple times to try and avoid too much noise
                        List<CpuGpuTime> times = new List<CpuGpuTime>();

                        for (int i = 0; i < 10; ++i)
                        {
                            // Hold the device lock while we run the scenario - this prevents other threads
                            // from interacting with the device and interfering with our recorded time.
                            using (var deviceLock = device.Lock())
                            {
                                times.Add(RunScenario(runner, rt));
                            }
                            if (cancellationToken.IsCancellationRequested)
                                return new CpuGpuTime();
                        }

                        var cpuTimes = from entry in times select entry.CpuTimeInMs;
                        var gpuTimes = from entry in times select entry.GpuTimeInMs;

                        return new CpuGpuTime
                        {
                            CpuTimeInMs = GetAverage(cpuTimes),
                            GpuTimeInMs = GetAverage(gpuTimes)
                        };
                    }, cancellationToken);

                    Data[scenarioData.Value] = time;

                    if (cancellationToken.IsCancellationRequested)
                        return;

                    using (var ds = Image.CreateDrawingSession(Colors.Transparent))
                    {
                        ds.DrawImage(rt);

                        var timing = string.Format("{0:0.00}ms\n{1:0.00}ms", time.CpuTimeInMs, time.GpuTimeInMs);
                        ds.DrawText(timing, 0, 0, Colors.White);
                        ds.DrawText(timing, 1, 1, Colors.Black);
                    }
                }

                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Image"));
                    PropertyChanged(this, new PropertyChangedEventArgs("Summary"));
                }
            }
        private void OnCreateResources(CanvasControl sender, object args)
        {
            imageSource = new CanvasImageSource(sender, width, height);
            image.Source = imageSource;

            Draw(0, 0, width, height, Colors.Blue);
        }