/// <summary> /// resizeBitmap /// </summary> /// <param name="creator">ICanvasResourceCreator</param> public void resizeBitmap(ICanvasResourceCreator creator) // Resize the bitMap { var scaledSoftwareBitmap = CanvasBitmap.CreateFromSoftwareBitmap(creator, new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)128, (int)128)); this.Bitmap = scaledSoftwareBitmap; // Set the scaled bitmap this.Effect = ImageManipulation.img(this.Bitmap); // manipulate the image }
private void OnDebugDraw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { var session = args.DrawingSession; session.Clear(Colors.DarkSlateBlue); var dpi = Windows.Graphics.Display.DisplayProperties.LogicalDpi; var layers = Sequensor.Controller.CachedFrame; foreach (var layer in layers) { var l = layer; foreach (var render in layer) { var cbi = CanvasBitmap.CreateFromSoftwareBitmap(session.Device, render.Source); ICanvasImage image = new Transform2DEffect { Source = cbi, TransformMatrix = render.Transformaion }; session.DrawImage(image); } } }
//<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); } }); }
public async Task <MediaComposition> TimeBasedReconstruction(Stream aedatFile, CameraParameters cam, EventColor onColor, EventColor offColor, int frameTime, int maxFrames, float playback_frametime) { byte[] aedatBytes = new byte[5 * Convert.ToInt32(Math.Pow(10, 8))]; // Read 0.5 GB at a time MediaComposition composition = new MediaComposition(); int lastTime = -999999; int timeStamp; int frameCount = 0; Stream pixelStream = InitBitMap(cam); byte[] currentFrame = new byte[pixelStream.Length]; int bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length); while (bytesRead != 0 && frameCount < maxFrames) { // Read through AEDAT file for (int i = 0, length = bytesRead; i < length; i += AedatUtilities.dataEntrySize) // iterate through file, 8 bytes at a time. { AEDATEvent currentEvent = new AEDATEvent(aedatBytes, i, cam); AedatUtilities.SetPixel(ref currentFrame, currentEvent.x, currentEvent.y, (currentEvent.onOff ? onColor.Color : offColor.Color), cam.cameraX); timeStamp = currentEvent.time; if (lastTime == -999999) { lastTime = timeStamp; } else { if (lastTime + frameTime <= timeStamp) // Collected events within specified timeframe, add frame to video { WriteableBitmap b = new WriteableBitmap(cam.cameraX, cam.cameraY); using (Stream stream = b.PixelBuffer.AsStream()) { await stream.WriteAsync(currentFrame, 0, currentFrame.Length); } SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(b.PixelBuffer, BitmapPixelFormat.Bgra8, b.PixelWidth, b.PixelHeight, BitmapAlphaMode.Ignore); CanvasBitmap bitmap2 = CanvasBitmap.CreateFromSoftwareBitmap(CanvasDevice.GetSharedDevice(), outputBitmap); // Set playback framerate MediaClip mediaClip = MediaClip.CreateFromSurface(bitmap2, TimeSpan.FromSeconds(playback_frametime)); composition.Clips.Add(mediaClip); frameCount++; // Stop adding frames to video if max frames has been reached if (frameCount >= maxFrames) { break; } currentFrame = new byte[pixelStream.Length]; lastTime = timeStamp; } } } bytesRead = aedatFile.Read(aedatBytes, 0, aedatBytes.Length); } return(composition); }
public static async Task <SoftwareBitmap> ResizeSoftwareBitmap(SoftwareBitmap softwareBitmap, double scaleFactor) { var resourceCreator = CanvasDevice.GetSharedDevice(); var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap); var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, (int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor), 96); using (var cds = canvasRenderTarget.CreateDrawingSession()) { cds.DrawImage(canvasBitmap, canvasRenderTarget.Bounds); } var pixelBytes = canvasRenderTarget.GetPixelBytes(); var writeableBitmap = new WriteableBitmap((int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor)); using (var stream = writeableBitmap.PixelBuffer.AsStream()) { await stream.WriteAsync(pixelBytes, 0, pixelBytes.Length); } var scaledSoftwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Rgba16, (int)(softwareBitmap.PixelWidth * scaleFactor), (int)(softwareBitmap.PixelHeight * scaleFactor)); scaledSoftwareBitmap.CopyFromBuffer(writeableBitmap.PixelBuffer); return(scaledSoftwareBitmap); }
public void CanvasBitmap_CreateFromSoftwareBitmap_Roundtrip() { var colors = new Color[] { Colors.Red, Colors.Green, Colors.Yellow, Colors.Green, Colors.Yellow, Colors.Red, Colors.Yellow, Colors.Red, Colors.Green }; float anyDpi = 10; var device = new CanvasDevice(true); var originalBitmap = CanvasBitmap.CreateFromColors(device, colors, 3, 3, anyDpi); var softwareBitmap = SoftwareBitmap.CreateCopyFromSurfaceAsync(originalBitmap, BitmapAlphaMode.Premultiplied).AsTask().Result; softwareBitmap.DpiX = anyDpi; softwareBitmap.DpiY = anyDpi; var roundtrippedBitmap = CanvasBitmap.CreateFromSoftwareBitmap(device, softwareBitmap); Assert.AreEqual(originalBitmap.Format, roundtrippedBitmap.Format); Assert.AreEqual(anyDpi, roundtrippedBitmap.Dpi); var roundtrippedColors = roundtrippedBitmap.GetPixelColors(); Assert.AreEqual(colors.Length, roundtrippedColors.Length); for (var i = 0; i < colors.Length; ++i) { Assert.AreEqual(colors[i], roundtrippedColors[i]); } }
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); }
public static SoftwareBitmap CropAndResize(this SoftwareBitmap softwareBitmap, Rect bounds, float newWidth, float newHeight) { var resourceCreator = CanvasDevice.GetSharedDevice(); using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap)) using (var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, newWidth, newHeight, canvasBitmap.Dpi)) using (var drawingSession = canvasRenderTarget.CreateDrawingSession()) using (var scaleEffect = new ScaleEffect()) using (var cropEffect = new CropEffect()) using (var atlasEffect = new AtlasEffect()) { drawingSession.Clear(Colors.White); cropEffect.SourceRectangle = bounds; cropEffect.Source = canvasBitmap; atlasEffect.SourceRectangle = bounds; atlasEffect.Source = cropEffect; scaleEffect.Source = atlasEffect; scaleEffect.Scale = new System.Numerics.Vector2(newWidth / (float)bounds.Width, newHeight / (float)bounds.Height); drawingSession.DrawImage(scaleEffect); drawingSession.Flush(); return(SoftwareBitmap.CreateCopyFromBuffer(canvasRenderTarget.GetPixelBytes().AsBuffer(), BitmapPixelFormat.Bgra8, (int)newWidth, (int)newHeight, BitmapAlphaMode.Premultiplied)); } }
// crop public SoftwareBitmap Crop(SoftwareBitmap softwareBitmap, Rect bounds) { if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8) { softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8); } var resourceCreator = CanvasDevice.GetSharedDevice(); using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap)) using (var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, (float)bounds.Width, (float)bounds.Width, canvasBitmap.Dpi)) using (var drawingSession = canvasRenderTarget.CreateDrawingSession()) using (var cropEffect = new CropEffect()) using (var atlasEffect = new AtlasEffect()) { drawingSession.Clear(Colors.White); cropEffect.SourceRectangle = bounds; cropEffect.Source = canvasBitmap; atlasEffect.SourceRectangle = bounds; atlasEffect.Source = cropEffect; drawingSession.DrawImage(atlasEffect); drawingSession.Flush(); return(SoftwareBitmap.CreateCopyFromBuffer(canvasRenderTarget.GetPixelBytes().AsBuffer(), BitmapPixelFormat.Bgra8, (int)bounds.Width, (int)bounds.Width, BitmapAlphaMode.Premultiplied)); } }
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); } }); }
private void CanvasControlRight_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { Rect destRect; lock (lockObj) { CanvasBitmap c; if (background == null) { return; } c = CanvasBitmap.CreateFromSoftwareBitmap(args.DrawingSession, background); var rects = GetSourceDest(background, args); args.DrawingSession.DrawImage(c, rects.Item2, rects.Item1); destRect = rects.Item2; } if (figure == null) { return; } DrawFigure(args, destRect, zIndex, rightEyePos); }
void DrawSoftwareBitmap(SoftwareBitmap softwareBitmap, Size renderSize) { using (var drawingSession = CanvasComposition.CreateDrawingSession(_drawingSurface)) using (var bitmap = CanvasBitmap.CreateFromSoftwareBitmap(drawingSession.Device, softwareBitmap)) { drawingSession.DrawImage(bitmap, new Rect(0, 0, renderSize.Width, renderSize.Height)); } }
/** * カレント位置のサムネイルを作成する。 */ 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(); } } } }); }
public async Task Draw(CompositionGraphicsDevice device, Object drawingLock, CompositionDrawingSurface surface, Size size) { var canvasDevice = CanvasComposition.GetCanvasDevice(device); CanvasBitmap canvasBitmap; if (_softwareBitmap != null) { canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, _softwareBitmap); } else if (_file != null) { SoftwareBitmap softwareBitmap = await LoadFromFile(_file); canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, softwareBitmap); } else { canvasBitmap = await CanvasBitmap.LoadAsync(canvasDevice, _uri); } var bitmapSize = canvasBitmap.Size; // // Because the drawing is done asynchronously and multiple threads could // be trying to get access to the device/surface at the same time, we need // to do any device/surface work under a lock. // lock (drawingLock) { Size surfaceSize = size; if (surface.Size != size || surface.Size == new Size(0, 0)) { // Resize the surface to the size of the image CanvasComposition.Resize(surface, bitmapSize); surfaceSize = bitmapSize; } // Allow the app to process the bitmap if requested if (_handler != null) { _handler(surface, canvasBitmap, device); } else { // Draw the image to the surface using (var session = CanvasComposition.CreateDrawingSession(surface)) { session.Clear(Windows.UI.Color.FromArgb(0, 0, 0, 0)); session.DrawImage(canvasBitmap, new Rect(0, 0, surfaceSize.Width, surfaceSize.Height), new Rect(0, 0, bitmapSize.Width, bitmapSize.Height)); } } } }
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; }
public static async Task <SoftwareBitmap> Render(RenderObjectBase[][] renderChain) { var sources = new List <RenderObjectBase>(); foreach (var layer in renderChain) { foreach (var item in layer) { sources.Add(item); } } var width = sources.Max(x => x.Source.PixelWidth); var height = sources.Max(x => x.Source.PixelHeight); CanvasDevice device = new CanvasDevice(); CanvasRenderTarget render = new CanvasRenderTarget(device, (float)width, (float)height, 96); using (var session = render.CreateDrawingSession()) { CanvasBitmap cb; ICanvasImage cbi; // Проходим по слоям foreach (var layer in renderChain) { foreach (var renderObject in layer) { cb = CanvasBitmap.CreateFromSoftwareBitmap(device, renderObject.Source); cbi = new Transform2DEffect() { Source = cb, TransformMatrix = renderObject.Transformaion }; session.DrawImage(cbi); } } } return(SoftwareBitmap.Convert( await SoftwareBitmap.CreateCopyFromSurfaceAsync(render), BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied )); }
public static SoftwareBitmap Resize(SoftwareBitmap softwareBitmap, float newWidth, float newHeight) { using (var resourceCreator = CanvasDevice.GetSharedDevice()) using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap)) using (var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, newWidth, newHeight, canvasBitmap.Dpi)) using (var drawingSession = canvasRenderTarget.CreateDrawingSession()) using (var scaleEffect = new ScaleEffect()) { scaleEffect.Source = canvasBitmap; scaleEffect.Scale = new System.Numerics.Vector2(newWidth / softwareBitmap.PixelWidth, newHeight / softwareBitmap.PixelHeight); drawingSession.DrawImage(scaleEffect); drawingSession.Flush(); return(SoftwareBitmap.CreateCopyFromBuffer(canvasRenderTarget.GetPixelBytes().AsBuffer(), BitmapPixelFormat.Bgra8, (int)newWidth, (int)newHeight, BitmapAlphaMode.Premultiplied)); } }
private Tuple <Rect, Rect> GetSourceDest(SoftwareBitmap background, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { double sourceWidth; double sourceHeight; double destWidth; double destHeight; if (background == null) { return(null); } var c = CanvasBitmap.CreateFromSoftwareBitmap(args.DrawingSession, background); sourceWidth = background.PixelWidth; sourceHeight = background.PixelHeight; var sourceAspectRatio = sourceWidth / sourceHeight; Rect sourceRect; destWidth = canvLeft.ActualWidth; destHeight = canvLeft.ActualHeight; var destAspectRatio = destWidth / destHeight; var destRect = new Rect() { X = 0, Y = 0, Width = destWidth, Height = destHeight }; if (sourceAspectRatio > destAspectRatio) { var sourceNewWidth = sourceHeight * destAspectRatio; sourceRect = new Rect() { X = (sourceWidth - sourceNewWidth) / 2, Y = 0, Width = sourceNewWidth, Height = sourceHeight }; } else { var sourceNewHeight = sourceWidth * destAspectRatio; sourceRect = new Rect() { X = 0, Y = (sourceHeight - sourceNewHeight) / 2, Width = sourceWidth, Height = sourceNewHeight }; } return(new Tuple <Rect, Rect>(sourceRect, destRect)); }
private async void OpenFile_Button(object sender, RoutedEventArgs e) { FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; fileOpenPicker.FileTypeFilter.Add(".jpg"); fileOpenPicker.FileTypeFilter.Add(".png"); fileOpenPicker.FileTypeFilter.Add(".bmp"); fileOpenPicker.ViewMode = PickerViewMode.Thumbnail; var inputFile = await fileOpenPicker.PickSingleFileAsync(); if (inputFile == null) { // The user cancelled the picking operation return; } SoftwareBitmap inputBitmap; using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file inputBitmap = await decoder.GetSoftwareBitmapAsync(); } if (inputBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || inputBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied) { inputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } _layers = new List <CanvasBitmap>(); _layers.Add(CanvasBitmap.CreateFromSoftwareBitmap(canvasControl.Device, inputBitmap)); byte[] emptyBitmap = new byte[inputBitmap.PixelWidth * inputBitmap.PixelHeight * 4]; _layers.Add(CanvasBitmap.CreateFromBytes(canvasControl.Device, emptyBitmap, inputBitmap.PixelWidth, inputBitmap.PixelHeight, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized)); _imgDims = new Vector2(_layers[0].SizeInPixels.Width, _layers[0].SizeInPixels.Height); RenderMain(); inputBitmap.Dispose(); }
/// <summary> /// Helper metohd that crop a SoftwareBitmap given a new bounding box /// </summary> private SoftwareBitmap CropSoftwareBitmap(SoftwareBitmap softwareBitmap, float x, float y, float width, float height) { using (var resourceCreator = CanvasDevice.GetSharedDevice()) using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap)) using (var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, width, height, canvasBitmap.Dpi)) using (var drawingSession = canvasRenderTarget.CreateDrawingSession()) using (var cropEffect = new CropEffect()) { cropEffect.Source = canvasBitmap; drawingSession.DrawImage( cropEffect, new Rect(0.0, 0.0, width, height), new Rect(x, y, width, height), (float)1.0, CanvasImageInterpolation.HighQualityCubic); drawingSession.Flush(); return(SoftwareBitmap.CreateCopyFromBuffer(canvasRenderTarget.GetPixelBytes().AsBuffer(), BitmapPixelFormat.Rgba8, (int)width, (int)height, BitmapAlphaMode.Premultiplied)); } }
private void PrepareVideo(WriteableBitmap bmp, bool EN) { if (EN) { SoftwareBitmap frame = SoftwareBitmap.CreateCopyFromBuffer(bmp.PixelBuffer, BitmapPixelFormat.Bgra8, bmp.PixelWidth, bmp.PixelHeight, BitmapAlphaMode.Premultiplied); CanvasRenderTarget rendertarget = null; using (CanvasBitmap canvas = CanvasBitmap.CreateFromSoftwareBitmap(CanvasDevice.GetSharedDevice(), frame)) { rendertarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), canvas.SizeInPixels.Width, canvas.SizeInPixels.Height, 96); using (CanvasDrawingSession ds = rendertarget.CreateDrawingSession()) { ds.Clear(Windows.UI.Colors.Black); ds.DrawImage(canvas); } } thisframe = DateTime.Now; MediaClip m = MediaClip.CreateFromSurface(rendertarget, thisframe - lastframe); lastframe = thisframe; composition.Clips.Add(m); } }
private void RenderRegion(Rect updateRect) { var renderRegion = new BitmapBounds { X = ConvertDipsToPixels(updateRect.X), Y = ConvertDipsToPixels(updateRect.Y), Width = ConvertDipsToPixels(updateRect.Width), Height = ConvertDipsToPixels(updateRect.Height) }; using (var bitmap = _page.RenderRegionToSoftwareBitmap(Source.SizeInPixels, renderRegion)) { using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap( resourceCreator: CanvasDevice.GetSharedDevice(), sourceBitmap: bitmap)) using (var drawingSession = Source.CreateDrawingSession(Colors.White, updateRect)) { drawingSession.DrawImage(canvasBitmap, updateRect); } } }
void TestCreateFromSoftwareBitmap(CanvasDevice device, BitmapPixelFormat pixelFormat, BitmapAlphaMode alphaMode) { if (pixelFormat == BitmapPixelFormat.Unknown) { return; } int anyWidth = 3; int anyHeight = 5; var softwareBitmap = new SoftwareBitmap(pixelFormat, anyWidth, anyHeight, alphaMode); if (!IsFormatSupportedByWin2D(pixelFormat, alphaMode)) { Assert.ThrowsException <Exception>(() => { CanvasBitmap.CreateFromSoftwareBitmap(device, softwareBitmap); }); return; } var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(device, softwareBitmap); Assert.AreEqual(anyWidth, (int)canvasBitmap.SizeInPixels.Width); Assert.AreEqual(anyHeight, (int)canvasBitmap.SizeInPixels.Height); Assert.AreEqual(GetDirectXPixelFormatUsedForBitmapPixelFormat(pixelFormat), canvasBitmap.Format); CanvasAlphaMode expectedAlphaMode = CanvasAlphaMode.Straight; switch (alphaMode) { case BitmapAlphaMode.Ignore: expectedAlphaMode = CanvasAlphaMode.Ignore; break; case BitmapAlphaMode.Premultiplied: expectedAlphaMode = CanvasAlphaMode.Premultiplied; break; case BitmapAlphaMode.Straight: expectedAlphaMode = CanvasAlphaMode.Straight; break; } Assert.AreEqual(expectedAlphaMode, canvasBitmap.AlphaMode); }
void OnDraw(CanvasControl sender, CanvasDrawEventArgs args) { // Capture this here (in a race) in case it gets over-written // while this function is still running. var poseEventArgs = this.lastPoseEventArgs; args.DrawingSession.Clear(Colors.Black); // Do we have a colour frame to draw? if (this.lastConvertedColorBitmap != null) { using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap( this.canvasControl, this.lastConvertedColorBitmap)) { // Draw the colour frame args.DrawingSession.DrawImage( canvasBitmap, this.canvasSize, this.bitmapSize.Value); // Have we got a skeletal frame hanging around? if (poseEventArgs?.PoseEntries?.Length > 0) { foreach (var entry in poseEventArgs.PoseEntries) { foreach (var pose in entry.Points) { var centrePoint = ScalePosePointToDrawCanvasVector2(pose); args.DrawingSession.FillCircle( centrePoint, circleRadius, Colors.Red); } } } } } Interlocked.Exchange(ref this.isBetweenRenderingPass, 0); }
void OnCanvasControlDraw(CanvasControl sender, CanvasDrawEventArgs args) { // Note It´s not allowed to use async calls in this method the DrawingSession is disposed when leving this function. // Thats why the m_sepiaEffectSoftwareBitmap is crated on loaded if (m_sepiaEffectSoftwareBitmap != null) { var destinationRect = new Rect(0, 0, sender.ActualWidth, sender.ActualHeight); CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(sender.Device, m_sepiaEffectSoftwareBitmap); args.DrawingSession.DrawImage(CreateDirectionalBlur(canvasBitmap), destinationRect, new Rect(0, 0, m_imageSize.Width, m_imageSize.Height)); } args.DrawingSession.DrawRectangle(new Rect(0, 0, sender.ActualWidth, sender.ActualHeight), Colors.Black, 20); for (int i = 0; i < 5; i++) { args.DrawingSession.DrawText("Lumia Imaging SDK!", RndPosition(), Color.FromArgb(255, RndByte(), RndByte(), RndByte())); args.DrawingSession.DrawText("Win2D!", RndPosition(), Color.FromArgb(255, RndByte(), RndByte(), RndByte())); args.DrawingSession.DrawCircle(RndPosition(), RndRadius(), Color.FromArgb(255, RndByte(), RndByte(), RndByte())); args.DrawingSession.DrawLine(RndPosition(), RndPosition(), Color.FromArgb(255, RndByte(), RndByte(), RndByte())); } }
/** * 1フレーム抽出 */ private void extractFrame(MediaPlayer mediaPlayer) { CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice(); var canvasImageSrc = new CanvasImageSource(canvasDevice, (int)mThumbnailSize.Width, (int)mThumbnailSize.Height, 96 /*DisplayInformation.GetForCurrentView().LogicalDpi*/); using (SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)mThumbnailSize.Width, (int)mThumbnailSize.Height, BitmapAlphaMode.Ignore)) using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, softwareBitmap)) using (CanvasDrawingSession ds = canvasImageSrc.CreateDrawingSession(Windows.UI.Colors.Black)) { try { mediaPlayer.CopyFrameToVideoSurface(inputBitmap); ds.DrawImage(inputBitmap); } catch (Exception e) { // 無視する Debug.WriteLine(e.ToString()); } CTX.Frames.Add(canvasImageSrc); } }
public async Task <SoftwareBitmap> RenderToBitmapAsync( RenderTargetBitmap renderTargetBitmap, IReadOnlyList <InkStroke> inkStrokes, IReadOnlyList <InkStroke> highlightStrokes, double width, double height) { var dpi = DisplayInformation.GetForCurrentView().LogicalDpi; var renderTarget = new CanvasRenderTarget(_canvasDevice, (float)width, (float)height, dpi); using (renderTarget) { try { using (var drawingSession = renderTarget.CreateDrawingSession()) { var pixels = await renderTargetBitmap.GetPixelsAsync(); var bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixels, BitmapPixelFormat.Bgra8, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight); var convertedImage = SoftwareBitmap.Convert( bitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied ); var background = CanvasBitmap.CreateFromSoftwareBitmap(_canvasDevice, convertedImage); drawingSession.DrawImage(background, new Rect(0, 0, width, height)); drawingSession.DrawInk(inkStrokes); } return(await SoftwareBitmap.CreateCopyFromSurfaceAsync(renderTarget, BitmapAlphaMode.Premultiplied)); } catch (Exception e) when(_canvasDevice.IsDeviceLost(e.HResult)) { _canvasDevice.RaiseDeviceLost(); } } return(null); }
/** * CustomDrawingモード時のフレーム描画処理 */ private async void MP_FrameAvailable(MediaPlayer mediaPlayer, object args) { if (mGettingFrame) { return; } CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice(); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (mFrameServerDest == null) { // FrameServerImage in this example is a XAML image control var playerSize = CTX.PlayerSize; mFrameServerDest = new SoftwareBitmap(BitmapPixelFormat.Rgba8, (int)playerSize.Width, (int)playerSize.Height, BitmapAlphaMode.Ignore); mCanvasImageSource = new CanvasImageSource(canvasDevice, (int)playerSize.Width, (int)playerSize.Height, 96 /*DisplayInformation.GetForCurrentView().LogicalDpi*/); mFrameImage.Source = mCanvasImageSource; } Debug.WriteLine("Frame: {0}", mediaPlayer.PlaybackSession.Position); updateSliderPosition(mediaPlayer.PlaybackSession.Position.TotalMilliseconds); using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, mFrameServerDest)) using (CanvasDrawingSession ds = mCanvasImageSource.CreateDrawingSession(Windows.UI.Colors.Black)) { MediaPlayer.CopyFrameToVideoSurface(inputBitmap); if (null != CustomDraw) { CustomDraw(this, ds, inputBitmap); } else { ds.DrawImage(inputBitmap); } } }); }
private async void MediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args) { CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice(); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { SoftwareBitmap softwareBitmapImg; SoftwareBitmap frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 500, 500, BitmapAlphaMode.Premultiplied); using (CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameServerDest)) { sender.CopyFrameToVideoSurface(canvasBitmap); softwareBitmapImg = await SoftwareBitmap.CreateCopyFromSurfaceAsync(canvasBitmap, BitmapAlphaMode.Ignore); } SoftwareBitmapSource imageSource = new SoftwareBitmapSource(); await imageSource.SetBitmapAsync(softwareBitmapImg); UIPreviewImage.Source = imageSource; // Encapsulate the image within a VideoFrame to be bound and evaluated VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmapImg); await EvaluateVideoFrameAsync(inputImage); }); }
private async void VideoFrameAvailable(MediaPlayer sender, object args) { CanvasDevice canvasDevice = CanvasDevice.GetSharedDevice(); int width = (int)sender.PlaybackSession.NaturalVideoWidth; int height = (int)sender.PlaybackSession.NaturalVideoHeight; if (frameBuffer == null) { frameBuffer = new SoftwareBitmap(BitmapPixelFormat.Rgba8, width, height, BitmapAlphaMode.Premultiplied); } await window.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { SoftwareBitmap frame; using (var inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, frameBuffer)) { sender.CopyFrameToVideoSurface(inputBitmap); frame = await SoftwareBitmap.CreateCopyFromSurfaceAsync(inputBitmap); } Width = frame.PixelWidth; Height = frame.PixelHeight; var cam = camera.View.Inverse(); var webcamToWorld = new Matrix4(cam.m00, cam.m01, cam.m02, cam.m03, cam.m10, cam.m11, cam.m12, cam.m13, cam.m20, cam.m21, cam.m22, cam.m23, 0, 0, 0, 1); FrameReady?.Invoke(new FrameData() { bitmap = frame, webcamToWorldMatrix = webcamToWorld, projectionMatrix = camera.Projection }); }); }