async Task processRenderingHR() { var time = DateTime.Now; session.UndoAll(); var currentSize = new Size( outputSize.Width / currentScale, outputSize.Height / currentScale); var corner = new Point(currentPos.X - currentSize.Width / 2, currentPos.Y - currentSize.Height / 2); session.AddFilter(CreateOrientedROIFilter(new Rect(corner, currentSize), currentAngle)); await session.RenderToBitmapAsync(outputBitmapHRTmp.AsBitmap()); outputBitmapHRTmp.Pixels.CopyTo(outputBitmapHR.Pixels, 0); outputBitmapHR.Invalidate(); if (output.Source != outputBitmapHR) { output.Source = outputBitmapHR; } lastDuration = DateTime.Now - time; if (nbHRImage == 0) { minHRtime = lastDuration; maxHRtime = lastDuration; } if (lastDuration < minHRtime) { minHRtime = lastDuration; } if (lastDuration > maxHRtime) { maxHRtime = lastDuration; } sumHRtime += lastDuration; nbHRImage++; }
/// <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 Task GetNewFrameAndApplyEffect(IBuffer processedBuffer) { if (captureDevice == null) { return; } captureDevice.GetPreviewBufferArgb(cameraBitmap.Pixels); Bitmap outputBtm = new Bitmap( outputBufferSize, ColorMode.Bgra8888, (uint)outputBufferSize.Width * 4, // 4 bytes per pixel in BGRA888 mode processedBuffer); EditingSession session = new EditingSession(cameraBitmap.AsBitmap()); session.AddFilter(FilterFactory.CreateSketchFilter(SketchMode.Gray)); await session.RenderToBitmapAsync(outputBtm); }
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); } }
/// <summary> /// Begins a photo stream item rendering process loop. Loop is executed asynchronously item by item /// until there are no more items in the queue. /// </summary> private async void Process() { _processingNow++; while (_enabled && Count > 0) { StreamItemViewModel item; if (_priorityQueue.Count > 0) { Busy = true; item = _priorityQueue[0]; _priorityQueue.RemoveAt(0); } else { item = _standardQueue[0]; _standardQueue.RemoveAt(0); } try { WriteableBitmap bitmap = null; using (MemoryStream thumbnailStream = new MemoryStream()) { System.Diagnostics.Debug.Assert(item.RequestedSize != StreamItemViewModel.Size.None); if (item.RequestedSize == StreamItemViewModel.Size.Large) { bitmap = new WriteableBitmap(280, 280); item.Model.Picture.GetImage().CopyTo(thumbnailStream); } else if (item.RequestedSize == StreamItemViewModel.Size.Medium) { bitmap = new WriteableBitmap(140, 140); item.Model.Picture.GetThumbnail().CopyTo(thumbnailStream); } else { bitmap = new WriteableBitmap(70, 70); item.Model.Picture.GetThumbnail().CopyTo(thumbnailStream); } using (EditingSession session = new EditingSession(thumbnailStream.GetWindowsRuntimeBuffer())) { Windows.Foundation.Rect rect; if (session.Dimensions.Width > session.Dimensions.Height) { rect = new Windows.Foundation.Rect() { Width = session.Dimensions.Height, Height = session.Dimensions.Height, X = session.Dimensions.Width / 2 - session.Dimensions.Height / 2, Y = 0 }; } else { rect = new Windows.Foundation.Rect() { Width = session.Dimensions.Width, Height = session.Dimensions.Width, X = 0, Y = session.Dimensions.Height / 2 - session.Dimensions.Width / 2 }; } session.AddFilter(FilterFactory.CreateCropFilter(rect)); if (item.Model.Filter != null) { foreach (IFilter f in item.Model.Filter.Components) { session.AddFilter(f); } } await session.RenderToBitmapAsync(bitmap.AsBitmap()); } } item.TransitionToImage(bitmap); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Rendering stream item failed:" + ex.Message); item.TransitionToImage(null); } } _processingNow--; if (_processingNow == 0) { Busy = false; } }
/// <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 FiltersWrapPanel) { using (EditingSession session = new EditingSession(bitmap)) { //render filtered photo int i = 0; foreach (FilterModel filter in list) { WriteableBitmap writeableBitmap = new WriteableBitmap(side, side); //crop the bitmap 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 += async delegate { ProgressIndicator.IsRunning = true; SetScreenButtonsEnabled(false); App.ThumbnailModel.UndoAllFilters(); App.ThumbnailModel.ApplyFilter(filter, _shouldCrop); App.ThumbnailModel.Dirty = true; WriteableBitmap photo = new WriteableBitmap((int)App.ThumbnailModel.Width, (int)App.ThumbnailModel.Height); await App.ThumbnailModel.RenderBitmapAsync(photo); PhotoViewer.Source = photo; SetScreenButtonsEnabled(true); ProgressIndicator.IsRunning = false; }; FiltersWrapPanel.Children.Add(photoThumbnail); session.UndoAll(); } } ProgressIndicator.IsRunning = false; }
public async Task GetNewFrameAndApplyEffect(IBuffer processedBuffer) { if (captureDevice == null) { return; } captureDevice.GetPreviewBufferArgb(cameraBitmap.Pixels); var outputBtm = new Bitmap( outputBufferSize, ColorMode.Bgra8888, (uint)outputBufferSize.Width * 4, // 4 bytes per pixel in BGRA888 mode processedBuffer); using (var session = new EditingSession(cameraBitmap.AsBitmap())) { switch (effectIndex) { case 0: session.AddFilter(FilterFactory.CreateLomoFilter(0.5, 0.5, LomoVignetting.High, LomoStyle.Yellow)); break; case 1: session.AddFilter(FilterFactory.CreateMagicPenFilter()); break; case 2: session.AddFilter(FilterFactory.CreateGrayscaleFilter()); break; case 3: session.AddFilter(FilterFactory.CreateAntiqueFilter()); break; case 4: session.AddFilter(FilterFactory.CreateStampFilter(5, 100)); break; case 5: session.AddFilter(FilterFactory.CreateCartoonFilter(false)); break; } await session.RenderToBitmapAsync(outputBtm); } }
/// <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()); }
/// <summary> /// Begins a photo stream item rendering process loop. Loop is executed asynchronously item by item /// until there are no more items in the queue. /// </summary> private async void Process() { _processingNow++; while (_enabled && Count > 0) { StreamItemViewModel item; if (_priorityQueue.Count > 0) { Busy = true; item = _priorityQueue[0]; _priorityQueue.RemoveAt(0); } else { item = _standardQueue[0]; _standardQueue.RemoveAt(0); } try { WriteableBitmap bitmap = null; using (MemoryStream thumbnailStream = new MemoryStream()) { System.Diagnostics.Debug.Assert(item.RequestedSize != StreamItemViewModel.Size.None); if (item.RequestedSize == StreamItemViewModel.Size.Large) { bitmap = new WriteableBitmap(280, 280); item.Model.Picture.GetImage().CopyTo(thumbnailStream); } else if (item.RequestedSize == StreamItemViewModel.Size.Medium) { bitmap = new WriteableBitmap(140, 140); item.Model.Picture.GetThumbnail().CopyTo(thumbnailStream); } else { bitmap = new WriteableBitmap(70, 70); item.Model.Picture.GetThumbnail().CopyTo(thumbnailStream); } using (EditingSession session = new EditingSession(thumbnailStream.GetWindowsRuntimeBuffer())) { Windows.Foundation.Rect rect; if (session.Dimensions.Width > session.Dimensions.Height) { rect = new Windows.Foundation.Rect() { Width = session.Dimensions.Height, Height = session.Dimensions.Height, X = session.Dimensions.Width / 2 - session.Dimensions.Height / 2, Y = 0 }; } else { rect = new Windows.Foundation.Rect() { Width = session.Dimensions.Width, Height = session.Dimensions.Width, X = 0, Y = session.Dimensions.Height / 2 - session.Dimensions.Width / 2 }; } session.AddFilter(FilterFactory.CreateCropFilter(rect)); if (item.Model.Filter != null) { foreach (IFilter f in item.Model.Filter.Components) { session.AddFilter(f); } } await session.RenderToBitmapAsync(bitmap.AsBitmap()); } } item.TransitionToImage(bitmap); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Rendering stream item failed:" + ex.Message); item.TransitionToImage(null); } } _processingNow--; if (_processingNow == 0) { Busy = false; } }
/// <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(); }); }