// 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)); } }
/// <summary> /// 画像保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void AppBarSave_Click(object sender, RoutedEventArgs e) { var now = DateTime.Now; var filename = "dpt-" + now.ToString("yyyy-MM-dd HHmmss") + ".jpg"; var wb = image1.Source as WriteableBitmap; SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(wb.PixelBuffer, BitmapPixelFormat.Bgra8, wb.PixelWidth, wb.PixelHeight); StorageFolder storageFolder = KnownFolders.PicturesLibrary; StorageFolder folder; try { folder = await storageFolder.GetFolderAsync("DpDisp"); } catch { await storageFolder.CreateFolderAsync("DpDisp"); folder = await storageFolder.GetFolderAsync("DpDisp"); } StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); SaveSoftwareBitmapToFile(outputBitmap, file); }
public static SoftwareBitmap ConvertBufferBitmapToBgra8ForXaml( IBuffer buffer, BitmapPixelFormat format, int width, int height) { SoftwareBitmap bitmap = null; var alpha = format == BitmapPixelFormat.Bgra8 ? BitmapAlphaMode.Premultiplied : BitmapAlphaMode.Ignore; var intermediate = SoftwareBitmap.CreateCopyFromBuffer( buffer, format, width, height, alpha); if (intermediate.BitmapPixelFormat != BitmapPixelFormat.Bgra8) { bitmap = SoftwareBitmap.Convert( intermediate, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); intermediate.Dispose(); } else { bitmap = intermediate; } return(bitmap); }
public static async Task <VideoFrame> GetHandWrittenImageAsync(Grid grid) { RenderTargetBitmap renderBitmap = new RenderTargetBitmap(); await renderBitmap.RenderAsync(grid); var buffer = await renderBitmap.GetPixelsAsync(); using (var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer( buffer, BitmapPixelFormat.Bgra8, renderBitmap.PixelWidth, renderBitmap.PixelHeight, BitmapAlphaMode.Ignore)) { buffer = null; renderBitmap = null; using (VideoFrame vf = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap)) { // The MNIST model takes a 28 x 28 image as input return(await CropAndDisplayInputImageAsync(vf, new Size(28, 28))); } } }
/// <summary> /// Start of the Drag and Drop operation: we set some content and change the DragUI /// depending on the selected options /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private async void SourceGrid_DragStarting(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.DragStartingEventArgs args) { args.Data.SetText(SourceTextBox.Text); if ((bool)DataPackageRB.IsChecked) { // Standard icon will be used as the DragUIContent args.DragUI.SetContentFromDataPackage(); } else if ((bool)CustomContentRB.IsChecked) { // Generate a bitmap with only the TextBox // We need to take the deferral as the rendering won't be completed synchronously var deferral = args.GetDeferral(); var rtb = new RenderTargetBitmap(); await rtb.RenderAsync(SourceTextBox); var buffer = await rtb.GetPixelsAsync(); var bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, rtb.PixelWidth, rtb.PixelHeight, BitmapAlphaMode.Premultiplied); args.DragUI.SetContentFromSoftwareBitmap(bitmap); deferral.Complete(); } // else just show the dragged UIElement }
private async void AnalyzeImage() { var rtBitmap = new RenderTargetBitmap(); await rtBitmap.RenderAsync(MainImage); var buffer = await rtBitmap.GetPixelsAsync(); var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, rtBitmap.PixelWidth, rtBitmap.PixelHeight); buffer = null; rtBitmap = null; var frame = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); //var croppedFrame = await CropAndDisplayInputImageAsync(frame); ModelInput.data = frame; ModelOutput = await Model.EvaluateAsync(ModelInput); if (ModelOutput.classLabel.Count > 0) { Results = $"{ModelOutput.classLabel.First()} - "; } else { Results = "could not identify image"; } }
private async Task ToImage(TensorFloat tensorFloat, Image image) { var pixels = tensorFloat .GetAsVectorView() .SelectMany( f => { byte v = Convert.ToByte(f * 255); return(new byte[] { v, v, v, 255 }); }) .ToArray(); var writeableBitmap = new WriteableBitmap(320, 320); // Open a stream to copy the image contents to the WriteableBitmap's pixel buffer using (Stream stream = writeableBitmap.PixelBuffer.AsStream()) { await stream.WriteAsync(pixels, 0, pixels.Length); } var dest = SoftwareBitmap.CreateCopyFromBuffer(writeableBitmap.PixelBuffer, BitmapPixelFormat.Bgra8, 320, 320, BitmapAlphaMode.Premultiplied); var destSouce = new SoftwareBitmapSource(); await destSouce.SetBitmapAsync(dest); image.Source = destSouce; }
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)); } }
private async Task OnStudyLoaded() { this.ImageCollection.ImageChanged += async(s, args) => { await this.RunOnUi(async() => { this.index = args.Index; this.OnPropertyChanged(nameof(this.Index)); var bm = SoftwareBitmap.CreateCopyFromBuffer( args.Image, BitmapPixelFormat.Bgra8, args.Width, args.Height, BitmapAlphaMode.Premultiplied); this.SoftwareBitmapSource = new SoftwareBitmapSource(); await this.SoftwareBitmapSource.SetBitmapAsync(bm); this.OnPropertyChanged(nameof(this.SoftwareBitmapSource)); }); }; this.LoadingScreen.Hide(); this.RunningScreen.Show(); this.OnPropertyChanged(nameof(this.SeriesNamesItems)); this.SeriesSelect.SelectedIndex = 0; this.StretchSelect.SelectedIndex = 2; await this.StartListening(); }
public static List <string> Recognize(string language, Byte[] image, int width, int height) { if (!_engines.ContainsKey(language)) { _engines.Add(language, OcrEngine.TryCreateFromLanguage(new Language(language))); } OcrEngine engine = _engines[language]; IBuffer buf = CryptographicBuffer.CreateFromByteArray(image); image = null; SoftwareBitmap sbitmap = SoftwareBitmap.CreateCopyFromBuffer(buf, BitmapPixelFormat.Bgra8, width, height); Task <OcrResult> task = engine.RecognizeAsync(sbitmap).AsTask(); task.Wait(); OcrResult result = task.Result; buf = null; GC.Collect(); List <string> lines = new List <string>(); foreach (OcrLine line in result.Lines) { lines.Add(line.Text); } return(ProcessLines(lines, engine.RecognizerLanguage.LayoutDirection == LanguageLayoutDirection.Rtl)); }
/// <summary> /// Gets the file location to save the Bitmap /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <summary> /// Closes the Program /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void saveBtn_Command(XamlUICommand sender, ExecuteRequestedEventArgs args) { FileSavePicker fileSavePicker = new FileSavePicker(); fileSavePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; fileSavePicker.FileTypeChoices.Add("JPEG files", new List <string>() { ".jpg", ".jpeg" }); fileSavePicker.FileTypeChoices.Add("PNG files", new List <string>() { ".png" }); fileSavePicker.SuggestedFileName = "image"; var outputFile = await fileSavePicker.PickSaveFileAsync(); if (outputFile != null) { SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer( mainCanvas.Bitmap.PixelBuffer, BitmapPixelFormat.Bgra8, mainCanvas.Bitmap.PixelWidth, mainCanvas.Bitmap.PixelHeight); SaveSoftwareBitmapToFile(outputBitmap, outputFile); } }
private async void Save() { if (isNewFile) { FileSaveAsCommand_ExecuteRequested(null, null); } else { if (outputFile == null) // The user cancelled the picking operation { return; } SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer( drawArea.ImageDataLayer.BitmapDrawingData.PixelBuffer, BitmapPixelFormat.Bgra8, drawArea.ImageDataLayer.BitmapDrawingData.PixelWidth, drawArea.ImageDataLayer.BitmapDrawingData.PixelHeight ); await SaveSoftwareBitmapToFile(outputBitmap, outputFile); if (exit) { Application.Current.Exit(); } if (openNew) { EditUndoCommand_ExecuteRequested(null, null); isNewFile = true; outputFile = null; openNew = false; } } }
private async Task <List <BitmapBounds> > DetectFace(SoftwareBitmap image) { if (imageData == null || imageData.Length != image.PixelHeight * image.PixelWidth) { imageData = new byte[image.PixelHeight * image.PixelWidth]; } unsafe { fixed(byte *grayPointer = imageData) { FaceProcessing.ImageProcessing.ColorToGrayscale(image, (Int32)(grayPointer)); } } SoftwareBitmap grayImage = SoftwareBitmap.CreateCopyFromBuffer(imageData.AsBuffer(), BitmapPixelFormat.Gray8, image.PixelWidth, image.PixelHeight); var faces = await faceDetector.DetectFacesAsync(grayImage); List <BitmapBounds> boundingBoxes = new List <BitmapBounds>(); for (int i = 0; i < faces.Count; i++) { boundingBoxes.Add(faces[i].FaceBox); } return(boundingBoxes); }
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 unsafe SoftwareBitmap ConvertToImage(byte[] input, int width, int height) { using (var yuv = SoftwareBitmap.CreateCopyFromBuffer(input.AsBuffer(), BitmapPixelFormat.Nv12, width, height)) { return(SoftwareBitmap.Convert(yuv, BitmapPixelFormat.Bgra8)); } }
public async Task UpdateOutputImage() { SoftwareBitmap softWriteableOutputImage = SoftwareBitmap.CreateCopyFromBuffer(WriteableOutputImage.PixelBuffer, BitmapPixelFormat.Bgra8, WriteableOutputImage.PixelWidth, WriteableOutputImage.PixelHeight); OutputImageStream = await GetRandomAccessStreamFromSoftwareBitmap(softWriteableOutputImage, BitmapEncoder.PngEncoderId); await LoadOutputVirtualBitmap(); }
public static async Task <CompositionBrush> GetAlphaMaskAsync(this UIElement element) { CompositionBrush mask = null; try { //For some reason, using TextBlock and getting the AlphaMask //generates a shadow with a size more smaller than the control size. if (element is TextBlock textElement) { mask = textElement.GetAlphaMask(); } // We also use this option with images and shapes, even though have the option to // get the AlphaMask directly (in case it is clipped). else if (element is FrameworkElement frameworkElement) { var height = (int)frameworkElement.ActualHeight; var width = (int)frameworkElement.ActualWidth; if (height > 0 && width > 0) { var visual = ElementCompositionPreview.GetElementVisual(element); var elementVisual = visual.Compositor.CreateSpriteVisual(); elementVisual.Size = element.RenderSize.ToVector2(); var bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync( element, width, height); var pixels = await bitmap.GetPixelsAsync(); using (var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer( pixels, BitmapPixelFormat.Bgra8, bitmap.PixelWidth, bitmap.PixelHeight, BitmapAlphaMode.Premultiplied)) { var brush = CompositionImageBrush.FromBGRASoftwareBitmap( visual.Compositor, softwareBitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight)); mask = brush.Brush; } } } } catch (Exception exc) { Debug.WriteLine($"Failed to get AlphaMask {exc}"); mask = null; } return(mask); }
public static SoftwareBitmap DrawInk(this IList <InkStroke> strokes, double targetWidth = 0, double targetHeight = 0, float rotation = 0, Color?backgroundColor = null) { if (strokes == null) { throw new ArgumentNullException($"{nameof(strokes)} cannot be null"); } var boundingBox = strokes.GetBoundingBox(); if (targetWidth == 0) { targetWidth = DEFAULT_WIDTH; } if (targetHeight == 0) { targetHeight = DEFAULT_HEIGHT; } if (backgroundColor == null) { backgroundColor = Colors.White; } var scale = CalculateScale(boundingBox, targetWidth, targetHeight); var scaledStrokes = ScaleAndTransformStrokes(strokes, scale, rotation); WriteableBitmap writeableBitmap = null; CanvasDevice device = CanvasDevice.GetSharedDevice(); using (CanvasRenderTarget offscreen = new CanvasRenderTarget(device, (float)targetWidth, (float)targetHeight, 96)) { using (CanvasDrawingSession ds = offscreen.CreateDrawingSession()) { ds.Units = CanvasUnits.Pixels; ds.Clear(backgroundColor.Value); ds.DrawInk(scaledStrokes); } writeableBitmap = new WriteableBitmap((int)offscreen.SizeInPixels.Width, (int)offscreen.SizeInPixels.Height); offscreen.GetPixelBytes().CopyTo(writeableBitmap.PixelBuffer); } SoftwareBitmap inkBitmap = SoftwareBitmap.CreateCopyFromBuffer( writeableBitmap.PixelBuffer, BitmapPixelFormat.Bgra8, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, BitmapAlphaMode.Premultiplied ); return(inkBitmap); }
private void WriteableBitmapToSoftwareBitmap(WriteableBitmap writeableBitmap) { // <SnippetWriteableBitmapToSoftwareBitmap> SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer( writeableBitmap.PixelBuffer, BitmapPixelFormat.Bgra8, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight ); // </SnippetWriteableBitmapToSoftwareBitmap> }
public async Task <SoftwareBitmap> CaptureElementToBitmap(UIElement uiElement) { RenderTargetBitmap renderTarget = new RenderTargetBitmap(); await renderTarget.RenderAsync(uiElement); IBuffer pixelBuffer = await renderTarget.GetPixelsAsync(); SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, (int)uiElement.RenderSize.Width, (int)uiElement.RenderSize.Height, BitmapAlphaMode.Premultiplied); return(bitmap); }
public void RefreshImage() { if (renderTarget != null) { var bytes = renderTarget.GetPixelBytes(); var bitmap = SoftwareBitmap.CreateCopyFromBuffer(bytes.AsBuffer(), BitmapPixelFormat.Bgra8, (int)renderTarget.Size.Width, (int)renderTarget.Size.Height); ClipImage.Image.OnNext(new ImageData { SoftwareBitmap = bitmap, Bytes = bytes }); } }
public static SoftwareBitmap GetSoftwareBitmap(this InkCanvas canvas, IEnumerable <InkStroke> strokes = null) { CanvasDevice device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)canvas.ActualWidth, (int)canvas.ActualHeight, 96); using (var ds = renderTarget.CreateDrawingSession()) { ds.Clear(Colors.White); ds.DrawInk(strokes ?? canvas.InkPresenter.StrokeContainer.GetStrokes(), true); } return(SoftwareBitmap.CreateCopyFromBuffer(renderTarget.GetPixelBytes().AsBuffer(), BitmapPixelFormat.Bgra8, (int)canvas.ActualWidth, (int)canvas.ActualHeight, BitmapAlphaMode.Premultiplied)); }
public static SoftwareBitmap AsSoftwareBitmap <TPixel>(this Image <TPixel> img) where TPixel : unmanaged, IPixel <TPixel> { var buffer = img.AsBuffer(); var format = BitmapPixelFormat.Unknown; if (typeof(TPixel) == typeof(Bgra32)) { format = BitmapPixelFormat.Bgra8; } var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, img.Width, img.Height); return(softwareBitmap); }
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 async Task <VideoFrame> Render(InkCanvas inkCanvas) { RenderTargetBitmap target = new RenderTargetBitmap(); await target.RenderAsync(inkCanvas, 28, 28); IBuffer buffer = await target.GetPixelsAsync(); SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, target.PixelWidth, target.PixelHeight, BitmapAlphaMode.Ignore); buffer = null; target = null; return(VideoFrame.CreateWithSoftwareBitmap(bitmap)); }
/* * //untuk WPF * public static void ConvertUiElementToBitmap(UIElement elt, string path) * { * double h = elt.RenderSize.Height; * double w = elt.RenderSize.Width; * if (h > 0) * { * PresentationSource source = PresentationSource.FromVisual(elt); * RenderTargetBitmap rtb = new RenderTargetBitmap((int)w, (int)h, 96, 96, PixelFormats.Default); * * VisualBrush sourceBrush = new VisualBrush(elt); * DrawingVisual drawingVisual = new DrawingVisual(); * DrawingContext drawingContext = drawingVisual.RenderOpen(); * using (drawingContext) * { * drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), * new Point(w, h))); * } * rtb.Render(drawingVisual); * * // return rtb; * var encoder = new PngBitmapEncoder(); * var outputFrame = BitmapFrame.Create(rtb); * encoder.Frames.Add(outputFrame); * * using (var file = System.IO.File.OpenWrite(path)) * { * encoder.Save(file); * } * } * } */ public async Task <StorageFile> CaptureElement(UIElement uiElement) { RenderTargetBitmap renderTarget = new RenderTargetBitmap(); await renderTarget.RenderAsync(uiElement); IBuffer pixelBuffer = await renderTarget.GetPixelsAsync(); SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, (int)uiElement.RenderSize.Width, (int)uiElement.RenderSize.Height, BitmapAlphaMode.Premultiplied); Random rnd = new Random(); string fName = DateTime.Now.ToString("dd_MM_yy_HH_mm_") + rnd.Next(1, 999) + ".jpg"; var tempFile = await KnownFolders.PicturesLibrary.CreateFileAsync(fName); //var tempFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appdata:///temp/{fName}")); SaveSoftwareBitmapToFile(bitmap, tempFile); return(tempFile); }
private async Task SaveCanvasAsImage(StorageFile file) { using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { RenderTargetBitmap rtb = new RenderTargetBitmap(); BitmapEncoder bmpEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream); await rtb.RenderAsync(MyCanvas); var buffer = await rtb.GetPixelsAsync(); SoftwareBitmap softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, rtb.PixelWidth, rtb.PixelHeight); bmpEncoder.SetSoftwareBitmap(softwareBitmap); await bmpEncoder.FlushAsync(); } }
static string RunOcrTests(string language, string file, bool spacing, bool newLines) { Language lang = null; foreach (Language ocrLanguage in OcrEngine.AvailableRecognizerLanguages) { if (ocrLanguage.LanguageTag.Equals(language, StringComparison.OrdinalIgnoreCase)) { lang = ocrLanguage; } } if (lang == null) { return(null); } using (var image = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(file)) { IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(BitmapToByteArray(image)); using (SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Rgba8, image.Width, image.Height)) { OcrEngine engine = OcrEngine.TryCreateFromLanguage(lang); var result = engine.RecognizeAsync(bitmap); Task.WaitAll(result.AsTask <OcrResult>()); string extractedText = string.Empty; OcrResult ocrResult = result.GetResults(); if (ocrResult != null && ocrResult.Lines != null) { foreach (OcrLine line in ocrResult.Lines) { foreach (OcrWord word in line.Words) { extractedText += word.Text + (spacing ? " " : string.Empty); } extractedText = extractedText.TrimEnd(); if (newLines) { extractedText += Environment.NewLine; } } } return(extractedText.TrimEnd()); } } }
private async Task <byte[]> getRandomBitmapArray(Random random, int width = 400, int height = 400) { // random bytes var randomBytes = new byte[width * height * 4]; // BGRA random.NextBytes(randomBytes); // create bitmap WriteableBitmap writeableBitmap = new WriteableBitmap(width, height); var bufferStream = writeableBitmap.PixelBuffer.AsStream(); await bufferStream.WriteAsync(randomBytes, 0, randomBytes.Length); // neues Bild generieren? SoftwareBitmap outputBitmap = SoftwareBitmap.CreateCopyFromBuffer( writeableBitmap.PixelBuffer, BitmapPixelFormat.Bgra8, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, BitmapAlphaMode.Premultiplied ); // darstellung //var bitmapSource = new SoftwareBitmapSource(); //await bitmapSource.SetBitmapAsync(outputBitmap); //RandomImage = bitmapSource; // save image to file //SaveSoftwareBitmapToFile(outputBitmap); // read byte array and return using (var stream = new InMemoryRandomAccessStream()) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); encoder.SetSoftwareBitmap(outputBitmap); await encoder.FlushAsync(); byte[] bitmapBytes = new byte[stream.Size]; using (DataReader reader = new DataReader(stream.GetInputStreamAt(0))) { await reader.LoadAsync((uint)stream.Size); reader.ReadBytes(bitmapBytes); } return(bitmapBytes); } }
private static async Task <VideoFrame> CreateVideoFrameFromImage(Image image) { var renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(image); IBuffer pixels = await renderTargetBitmap.GetPixelsAsync(); var bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixels, BitmapPixelFormat.Bgra8, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); var frame = VideoFrame.CreateWithSoftwareBitmap(bitmap); return(frame); }