Example #1
0
    protected override async void OnNavigatedTo(NavigationEventArgs e) {

      StreamResourceInfo sri = Application.GetResourceStream(new Uri("DemoImage.jpg", UriKind.Relative));
      var myBitmap = new WriteableBitmap(800, 480);
      img.ImageSource = myBitmap;

      using (var editsession = await EditingSessionFactory.CreateEditingSessionAsync(sri.Stream)) {
        // First add an antique effect 
       editsession.AddFilter(FilterFactory.CreateCartoonFilter(true));


       editsession.AddFilter(FilterFactory.CreateAntiqueFilter());
       // Then rotate the image
       editsession.AddFilter(FilterFactory.CreateFreeRotationFilter(35.0f, RotationResizeMode.FitInside));


        await editsession.RenderToBitmapAsync(myBitmap.AsBitmap());

   
      }
     
      myBitmap.Invalidate();

      base.OnNavigatedTo(e);
    }
        public async static Task<WriteableBitmap> Render(WriteableBitmap actualImage, List<IFilter> filters)
        {
            var bitmap = actualImage.AsBitmap();
            BitmapImageSource bitmapSource = new BitmapImageSource(bitmap);

            FilterEffect effects = new FilterEffect(bitmapSource);

            effects.Filters = filters;
            WriteableBitmapRenderer renderer = new WriteableBitmapRenderer(effects, actualImage);

            return await renderer.RenderAsync();

        }
        private static IReadOnlyList<IImageProvider> CreateFramedAnimation(IReadOnlyList<IImageProvider> images, Rect animationBounds, int w, int h)
        {
            List<IImageProvider> framedAnimation = new List<IImageProvider>();

            WriteableBitmap maskBitmap = new WriteableBitmap(w, h);

            var backgroundRectangle = new Rectangle
            {
                Fill = new SolidColorBrush(Colors.Black),
                Width = w,
                Height = h,
            };

            maskBitmap.Render(backgroundRectangle, new TranslateTransform());

            var foregroundRectangle = new Rectangle
            {
                Fill = new SolidColorBrush(Colors.White),
                Width = animationBounds.Width,
                Height = animationBounds.Height,
            };

            TranslateTransform foregroundTranslate = new TranslateTransform
            {
                X = animationBounds.X,
                Y = animationBounds.Y
            };
            maskBitmap.Render(foregroundRectangle, foregroundTranslate);
            maskBitmap.Invalidate();

            foreach (IImageProvider frame in images)
            {
                FilterEffect filterEffect = new FilterEffect(images[0]);

                BlendFilter blendFilter = new BlendFilter(frame, BlendFunction.Normal, 1.0)
                {
                    MaskSource = new BitmapImageSource(maskBitmap.AsBitmap())
                };

                filterEffect.Filters = new List<IFilter>() { blendFilter };
                framedAnimation.Add(filterEffect);
            }

            return framedAnimation;
        }
        private async void ApplyFilterToBackgroundImageAsync()
        {
            MemoryStream bitmapStream = new MemoryStream();
            Source.SaveJpeg(bitmapStream, Source.PixelWidth, Source.PixelHeight, 0, 50);
            IBuffer bmpBuffer = bitmapStream.GetWindowsRuntimeBuffer();

            // Output buffer
            WriteableBitmap outputImage = new WriteableBitmap(Source.PixelWidth, Source.PixelHeight);

            using (EditingSession editsession = new EditingSession(bmpBuffer))
            {
                // First add an antique effect 
                editsession.AddFilter(FilterFactory.CreateBlurFilter(BlurLevel.Blur6));

                // Finally, execute the filtering and render to a bitmap
                await editsession.RenderToBitmapAsync(outputImage.AsBitmap());
                outputImage.Invalidate();
                FadeInNewImage(outputImage);
            }
        }
Example #5
0
        private async void SplitImageFromBitmap(WriteableBitmap bmp)
        {
            _session = new EditingSession(bmp.AsBitmap());
            if (playMode == PlayMode.CameraVideo) _session.AddFilter(FilterFactory.CreateStepRotationFilter(Rotation.Rotate90));
            if (playMode == PlayMode.CameraVideo) _session.AddFilter(FilterFactory.CreateCropFilter(new Windows.Foundation.Rect(15, 20, 450, 600)));
            IFilter selectedFilter = GetFilter();
            try
            {
                if (Utils.IsChallengeMode())
                {
                    foreach (Image img in images)
                    {
                        _session.AddFilter(FilterFactory.CreateCropFilter(new Windows.Foundation.Rect(images.IndexOf(img) % 3 * 150, images.IndexOf(img) / 3 * 150, 150, 150)));
                        if (selectedFilter != null ) _session.AddFilter(selectedFilter);
                        await _session.RenderToImageAsync(img, OutputOption.PreserveAspectRatio);
                        if (selectedFilter != null && _session.CanUndo()) _session.Undo();
                        if (_session.CanUndo()) _session.Undo();
                    }
                }
                else
                {
                    foreach (Image img in images)
                    {
                        _session.AddFilter(FilterFactory.CreateCropFilter(new Windows.Foundation.Rect(images.IndexOf(img) % 6 * 75, images.IndexOf(img) / 6 * 75, 75, 75)));
                        if (selectedFilter != null) _session.AddFilter(selectedFilter);
                        await _session.RenderToImageAsync(img, OutputOption.PreserveAspectRatio);
                        if (selectedFilter != null && _session.CanUndo()) _session.Undo();
                        if (_session.CanUndo()) _session.Undo();
                    }
                }

            }
            catch (Exception exception)
            {
                MessageBox.Show("Exception:" + exception.Message);
                return;
            }
            if (playMode == PlayMode.CameraVideo) { processNextFrame(); }

        }
Example #6
0
 /// <summary>
 /// Renders current image with applied filters to the given bitmap.
 /// </summary>
 /// <param name="bitmap">Bitmap to render to</param>
 public async Task RenderBitmapAsync(WriteableBitmap bitmap)
 {
     await _session.RenderToBitmapAsync(bitmap.AsBitmap());
 }
Example #7
0
        private async void AttemptUpdatePreviewAsync()
        {
            if (!Processing)
            {
                Processing = true;

                do
                {
                    _processingPending = false;

                    if (Model.OriginalImage != null && ForegroundAnnotationsDrawn && BackgroundAnnotationsDrawn)
                    {
                        Model.OriginalImage.Position = 0;

                        var maskBitmap = new WriteableBitmap((int)AnnotationsCanvas.ActualWidth, (int)AnnotationsCanvas.ActualHeight);
                        var annotationsBitmap = new WriteableBitmap((int)AnnotationsCanvas.ActualWidth, (int)AnnotationsCanvas.ActualHeight);

                        annotationsBitmap.Render(AnnotationsCanvas, new ScaleTransform
                        {
                            ScaleX = 1,
                            ScaleY = 1
                        });

                        annotationsBitmap.Invalidate();

                        Model.OriginalImage.Position = 0;

                        using (var source = new StreamImageSource(Model.OriginalImage))
                        using (var segmenter = new InteractiveForegroundSegmenter(source))
                        using (var renderer = new WriteableBitmapRenderer(segmenter, maskBitmap))
                        using (var annotationsSource = new BitmapImageSource(annotationsBitmap.AsBitmap()))
                        {
                            var foregroundColor = Model.ForegroundBrush.Color;
                            var backgroundColor = Model.BackgroundBrush.Color;

                            segmenter.ForegroundColor = Windows.UI.Color.FromArgb(foregroundColor.A, foregroundColor.R, foregroundColor.G, foregroundColor.B);
                            segmenter.BackgroundColor = Windows.UI.Color.FromArgb(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B);
                            segmenter.Quality = 0.5;
                            segmenter.AnnotationsSource = annotationsSource;

                            await renderer.RenderAsync();

                            MaskImage.Source = maskBitmap;

                            maskBitmap.Invalidate();

                            Model.AnnotationsBitmap = (Bitmap)annotationsBitmap.AsBitmap();
                        }
                    }
                    else
                    {
                        MaskImage.Source = null;
                    }
                }
                while (_processingPending && !_manipulating);

                Processing = false;
            }
            else
            {
                _processingPending = true;
            }
        }
Example #8
0
        /// <summary>
        /// For the given bitmap renders filtered thumbnails for each filter in given list and populates
        /// the given wrap panel with the them.
        /// 
        /// For quick rendering, renders 10 thumbnails synchronously and then releases the calling thread.
        /// </summary>
        /// <param name="bitmap">Source bitmap to be filtered</param>
        /// <param name="side">Side length of square thumbnails to be generated</param>
        /// <param name="list">List of filters to be used, one per each thumbnail to be generated</param>
        /// <param name="panel">Wrap panel to be populated with the generated thumbnails</param>
        private async Task RenderThumbnailsAsync(Bitmap bitmap, int side, List<FilterModel> list, WrapPanel panel)
        {
            using (EditingSession session = new EditingSession(bitmap))
            {
                int i = 0;

                foreach (FilterModel filter in list)
                {
                    WriteableBitmap writeableBitmap = new WriteableBitmap(side, side);

                    foreach (IFilter f in filter.Components)
                    {
                        session.AddFilter(f);
                    }

                    Windows.Foundation.IAsyncAction action = session.RenderToBitmapAsync(writeableBitmap.AsBitmap());

                    i++;

                    if (i % 10 == 0)
                    {
                        // async, give control back to UI before proceeding.
                        await action;
                    }
                    else
                    {
                        // synchroneous, we keep the CPU for ourselves.
                        Task task = action.AsTask();
                        task.Wait();
                    }

                    PhotoThumbnail photoThumbnail = new PhotoThumbnail()
                    {
                        Bitmap = writeableBitmap,
                        Text = filter.Name,
                        Width = side,
                        Margin = new Thickness(6)
                    };

                    photoThumbnail.Tap += (object sender, System.Windows.Input.GestureEventArgs e) =>
                    {
                        App.PhotoModel.ApplyFilter(filter);
                        App.PhotoModel.Dirty = true;

                        NavigationService.GoBack();
                    };

                    panel.Children.Add(photoThumbnail);

                    session.UndoAll();
                }
            }
        }
        public async void PrepareImageForUploadAsync()
        {
            WriteableBitmap bmp = new WriteableBitmap(OriginalImage);

            bitmapStream = new MemoryStream();
            bmp.SaveJpeg(bitmapStream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
            IBuffer bmpBuffer = bitmapStream.GetWindowsRuntimeBuffer();

            // Output buffer
            bitmapForUpload = new WriteableBitmap(bmp.PixelWidth, bmp.PixelHeight);

            using (EditingSession editsession = new EditingSession(bmpBuffer))
            {
                // First add an antique effect 
                foreach (FilterBase fx in FilterManager.AppliedFilters)
                {
                    editsession.AddFilter(fx.FinalOutputFilter);
                }                

                // Finally, execute the filtering and render to a bitmap
                await editsession.RenderToBitmapAsync(bitmapForUpload.AsBitmap());
                bitmapForUpload.Invalidate();

                bitmapStream.Close();
                bitmapStream = null;
            }

            Dispatcher.BeginInvoke(() => {
                BeginUpload();
            });
        }