public async Task LoadFromBytes(byte[] bytes, int width, int height) { this.bytes = bytes; this.width = width; this.height = height; //error checking if ((width * height) != (bytes.Length / 4)) { throw new ArgumentException(); } //creates the stream from the byte array /* * * stream = new InMemoryRandomAccessStream(); * await stream.WriteAsync(bytes.AsBuffer()); * stream.Seek(0); * * //creats the bitmapImage from the stream * image = new BitmapImage(); * image.SetSource(stream); * */ // Uh yeah sure stream = new InMemoryRandomAccessStream(); stream.Size = 0; BitmapEncoder encode = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); // Set the byte array encode.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, (uint)width, (uint)height, 96.0, 96.0, bytes); // Go into the stream plz await encode.FlushAsync(); image = new BitmapImage(); image.SetSource(stream); //read only stream to initialize the decoder and softwareBitmap decoder = await BitmapDecoder.CreateAsync(stream); // There's probably a way to do this better... softMap = await decoder.GetSoftwareBitmapAsync(); }
private async Task <SoftwareBitmap> GetSoftwareBitmapAsync(BitmapImage image) { var randomAccess = RandomAccessStreamReference.CreateFromUri(image.UriSource); using (var stream = await randomAccess.OpenReadAsync()) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file var softwareBitmap = await decoder.GetSoftwareBitmapAsync(); softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); return(softwareBitmap); } }
private async Task SaveWizSFTBPAsync(IRandomAccessStream randomStream) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(randomStream); SoftwareBitmap softBmp = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Testimage.jpg", CreationCollisionOption.GenerateUniqueName); using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); encoder.SetSoftwareBitmap(softBmp); await encoder.FlushAsync(); } }
private async Task CreateTriangleImage() { string triangleImage = @"Assets\colorpick_triangle.png"; SoftwareBitmap softwareBitmap; StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFile triangleImgFile = await InstallationFolder.GetFileAsync(triangleImage); using (IRandomAccessStream printingFileStream = await triangleImgFile.OpenAsync(FileAccessMode.Read)) { BitmapDecoder printingDecoder = await BitmapDecoder.CreateAsync(printingFileStream); softwareBitmap = await printingDecoder.GetSoftwareBitmapAsync(); } triangleSoftwareBitmap = softwareBitmap; }
private async Task CreateColorRingImage() { string colorRingImage = @"Assets\ColorPicker\asus_gc_aura_customize_colorpick_selected_colorring_mask.png"; SoftwareBitmap softwareBitmap; StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFile colorRingImgFile = await InstallationFolder.GetFileAsync(colorRingImage); using (IRandomAccessStream printingFileStream = await colorRingImgFile.OpenAsync(FileAccessMode.Read)) { BitmapDecoder printingDecoder = await BitmapDecoder.CreateAsync(printingFileStream); softwareBitmap = await printingDecoder.GetSoftwareBitmapAsync(); } colorRingSoftwareBitmap = softwareBitmap; }
private async Task ShowImage(StorageFile photo) { using (IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(softwareBitmapBGR8); ImageSource = bitmapSource; } }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- #endregion //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- /// <summary> /// Converts the given data to a <see cref="SoftwareBitmap"/> and returns it. /// </summary> /// <param name="data">A valid <see cref="SoftwareBitmap"/> in binary representation.</param> /// <returns>The resulting <see cref="SoftwareBitmap"/> from the given <paramref name="data"/>.</returns> public static async Task <SoftwareBitmap> ToSoftwareBitmapImageAsync(byte[] data) { Debug.Assert(!(data is null)); try { IRandomAccessStream stream = data.AsBuffer().AsStream().AsRandomAccessStream(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); return(await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied)); } catch (Exception e) { Logger.Error("Failed to convert binary to SoftwareBitmap.", e); // Debug output for now while this leads to crashes: AppCenter.AppCenterCrashHelper.INSTANCE.TrackError(e, $"Converting bytes to a bitmap image failed in {nameof(ToSoftwareBitmapImageAsync)}.", new Dictionary <string, string> { { "null", (data is null).ToString() }, { "length", data is null ? "-1" : data.Length.ToString() }
private static async Task <SoftwareBitmap> GetSoftwareBitmapFromBitmap(Bitmap bitmap) { using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Bmp); using (IRandomAccessStream ras = new InMemoryRandomAccessStream()) { ms.Seek(0, SeekOrigin.Begin); await ms.CopyToAsync(ras.AsStreamForWrite()); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(ras); return(await decoder.GetSoftwareBitmapAsync()); } } }
private async Task <SoftwareBitmap> InitialG704Bitmap() { string fname = @"Assets\g704.png"; SoftwareBitmap softwareBitmap; StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFile printingFile = await InstallationFolder.GetFileAsync(fname); using (IRandomAccessStream printingFileStream = await printingFile.OpenAsync(FileAccessMode.Read)) { BitmapDecoder printingDecoder = await BitmapDecoder.CreateAsync(printingFileStream); softwareBitmap = await printingDecoder.GetSoftwareBitmapAsync(); } return(softwareBitmap); }
private async Task CreateRandomBgImage() { string randomBgImage = @"Assets\RandomSlider\rainbow_bg.png"; SoftwareBitmap softwareBitmap; StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFile randomBgImgFile = await InstallationFolder.GetFileAsync(randomBgImage); using (IRandomAccessStream printingFileStream = await randomBgImgFile.OpenAsync(FileAccessMode.Read)) { BitmapDecoder printingDecoder = await BitmapDecoder.CreateAsync(printingFileStream); softwareBitmap = await printingDecoder.GetSoftwareBitmapAsync(); } randomBgSoftwareBitmap = softwareBitmap; }
private async void SignIntoAppService() { if (await AzureAppService.SignIn(TokenTwo)) { AccountName.Text = await MicrosoftGraph.GetAccountDetails(TokenOne, "displayName"); ApplicationData.Current.LocalSettings.Values.Remove(StoredNameKey); ApplicationData.Current.LocalSettings.Values[StoredNameKey] = AccountName.Text; using (var imageStream = await MicrosoftGraph.GetAccountPicture(TokenOne)) { using (var randomStream = imageStream.AsRandomAccessStream()) { BitmapImage image = new BitmapImage(); await image.SetSourceAsync(randomStream); ImageBrush imageBrush = new ImageBrush { ImageSource = image }; AccountImage.Fill = imageBrush; SignInListViewItem.Visibility = Visibility.Collapsed; AccountListViewItem.Visibility = Visibility.Visible; } } ApplicationData.Current.LocalSettings.Values.Remove(StoredEmailKey); ApplicationData.Current.LocalSettings.Values[StoredEmailKey] = await MicrosoftGraph.GetAccountDetails(TokenOne, "userPrincipalName"); using (var imageStream = await MicrosoftGraph.GetAccountPicture(TokenOne)) { using (var randomStream = imageStream.AsRandomAccessStream()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(randomStream); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); StorageFile file_Save = await ApplicationData.Current.LocalFolder.CreateFileAsync("ProfilePicture.jpg", CreationCollisionOption.ReplaceExisting); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, await file_Save.OpenAsync(FileAccessMode.ReadWrite)); encoder.SetSoftwareBitmap(softwareBitmap); await encoder.FlushAsync(); } } } }
private async void OpenCVButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // <SnippetOpenCVBlur> FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; fileOpenPicker.FileTypeFilter.Add(".jpg"); 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); } SoftwareBitmap outputBitmap = new SoftwareBitmap(inputBitmap.BitmapPixelFormat, inputBitmap.PixelWidth, inputBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); var helper = new OpenCVBridge.OpenCVHelper(); helper.Blur(inputBitmap, outputBitmap); var bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(outputBitmap); imageControl.Source = bitmapSource; // </SnippetOpenCVBlur> }
private async void DetectFaces() { if (file != null) { // Open the image file and decode the bitmap into memory. // We'll need to make 2 bitmap copies: one for the FaceDetector and another to display. using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); BitmapTransform transform = this.ComputeScalingTransformForSourceImage(decoder); using (SoftwareBitmap originalBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage)) { // We need to convert the image into a format that's compatible with FaceDetector. // Gray8 should be a good type but verify it against FaceDetector’s supported formats. const BitmapPixelFormat InputPixelFormat = BitmapPixelFormat.Gray8; if (FaceDetector.IsBitmapPixelFormatSupported(InputPixelFormat)) { using (detectorInput = SoftwareBitmap.Convert(originalBitmap, InputPixelFormat)) { // Create a WritableBitmap for our visualization display; copy the original bitmap pixels to wb's buffer. displaySource = new WriteableBitmap(originalBitmap.PixelWidth, originalBitmap.PixelHeight); originalBitmap.CopyToBuffer(displaySource.PixelBuffer); NotifyUser("Detecting...", NotifyType.StatusMessage); // Initialize our FaceDetector and execute it against our input image. // NOTE: FaceDetector initialization can take a long time, and in most cases // you should create a member variable and reuse the object. // However, for simplicity in this scenario we instantiate a new instance each time. FaceDetector detector = await FaceDetector.CreateAsync(); faces = await detector.DetectFacesAsync(detectorInput); // Create our display using the available image and face results. DrawDetectedFaces(displaySource, faces); } } else { NotifyUser("PixelFormat '" + InputPixelFormat.ToString() + "' is not supported by FaceDetector", NotifyType.ErrorMessage); } } } } }
private async void Capture_Person_Button_Click(object sender, RoutedEventArgs e) { CameraCaptureUI captureUI = new CameraCaptureUI(); captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; captureUI.PhotoSettings.CroppedSizeInPixels = new Windows.Foundation.Size(200, 200); StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo); if (photo == null) { // User cancelled photo capture return; } IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists); SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(softwareBitmapBGR8); User_Image.Source = bitmapSource; string name = LastName_Entry.Text + " " + FirstName_Entry.Text + ".jpg"; await photo.CopyAsync(destinationFolder, name, NameCollisionOption.GenerateUniqueName); string a = photo.Path; imageName = photo.Name; imagepath = a.Replace("\\", "/"); // await photo.DeleteAsync(); User_Image.Visibility = Visibility.Visible; Add_Person_Button.Visibility = Visibility.Visible; }
private async Task InitializeFileLoading() { var myPictures = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); Const.ROOT_DIR = await myPictures.SaveFolder.GetFolderAsync("Camera Roll"); if (Const.LOAD_IMAGES) { // check input data at image directory _imageFiles = await GetImageFiles(Const.ROOT_DIR, Const.TEST_IMAGE_DIR_NAME); if (_imageFiles.LongCount() == 0) { Debug.WriteLine("test image directory empty!"); } else { Debug.WriteLine("Number of image files in the input folder: " + _imageFiles.LongCount()); } _indexImageFile = 0; } if (Const.IS_EXPERIMENT) { _imageFilesCompress = await GetImageFiles(Const.ROOT_DIR, Const.COMPRESS_IMAGE_DIR_NAME); _imageBitmapsCompress = new List <SoftwareBitmap>(); int i = 0; foreach (var imageFile in _imageFilesCompress) { using (IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync(); _imageBitmapsCompress.Add(bitmap); } i++; if (i == Const.MAX_COMPRESS_IMAGE) { break; } } _imageFileCompressLength = i; } }
private async void SelectPictureButton_Click(object sender, RoutedEventArgs e) { FileOpenPicker picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".jpeg"); picker.FileTypeFilter.Add(".png"); StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.DecodePixelWidth = 600; await bitmapImage.SetSourceAsync(fileStream); myImage.Source = bitmapImage; StorageFolder localFolder = ApplicationData.Current.LocalFolder; String path = localFolder.Path; BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); String Item_id = null; if (ViewModel.AllItems.Count == 0) { Item_id = "1"; } else { Item_id = ("Update" == createButton.Content.ToString()) ? ViewModel.SelectedItem.id : ViewModel.AllItems[ViewModel.AllItems.Count - 1].id; } StorageFile localFile = await localFolder.CreateFileAsync(Item_id + ".jpg", CreationCollisionOption.ReplaceExisting); SaveSoftwareBitmapToFile(softwareBitmap, localFile); currentPath = "ms-appx:///" + localFile.Path; if (ViewModel.SelectedItem != null) { ViewModel.SelectedItem.path = currentPath; } } } }
private static async Task <VideoFrame> GetVideoFrame(StorageFile file) { SoftwareBitmap softwareBitmap; using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file in BGRA8 format softwareBitmap = await decoder.GetSoftwareBitmapAsync(); softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); return(VideoFrame.CreateWithSoftwareBitmap(softwareBitmap)); } }
private async void HandleDataPackage(DataPackageView data, string imagefilename) { if (data.Contains(StandardDataFormats.StorageItems)) { foreach (var file in await data.GetStorageItemsAsync()) { AddAttachement(file as StorageFile); } } else if (data.Contains(StandardDataFormats.Bitmap)) { var bmpDPV = await data.GetBitmapAsync(); var bmpSTR = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(imagefilename + ".png", CreationCollisionOption.OpenIfExists); using (var writeStream = (await bmpSTR.OpenStreamForWriteAsync()).AsRandomAccessStream()) using (var readStream = await bmpDPV.OpenReadAsync()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(readStream.CloneStream()); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, writeStream); encoder.SetSoftwareBitmap(await decoder.GetSoftwareBitmapAsync()); await encoder.FlushAsync(); AddAttachement(bmpSTR); } } else if (data.Contains(StandardDataFormats.Text)) { Editor.Text = await data.GetTextAsync(); } else if (data.Contains(StandardDataFormats.WebLink)) { Editor.Text = (await data.GetWebLinkAsync()).ToString(); } else if (data.Contains(StandardDataFormats.ApplicationLink)) { Editor.Text = (await data.GetApplicationLinkAsync()).ToString(); } else if (data.Contains(StandardDataFormats.Html)) { var converter = new Html2Markdown.Converter(); Editor.Text = converter.Convert(await data.GetHtmlFormatAsync()); } }
private async void TestBtn_Click(object sender, RoutedEventArgs e) { // Trigger file picker to select an image file FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; fileOpenPicker.FileTypeFilter.Add(".jpg"); fileOpenPicker.FileTypeFilter.Add(".png"); fileOpenPicker.ViewMode = PickerViewMode.Thumbnail; StorageFile selectedStorageFile = await fileOpenPicker.PickSingleFileAsync(); SoftwareBitmap softwareBitmap; using (IRandomAccessStream stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file in BGRA8 format softwareBitmap = await decoder.GetSoftwareBitmapAsync(); softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } // Display the image //SoftwareBitmapSource imageSource = new SoftwareBitmapSource(); //await imageSource.SetBitmapAsync(softwareBitmap); //UIPreviewImage.Source = imageSource; // Encapsulate the image within a VideoFrame to be bound and evaluated VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); ModelInput.input_placeholer00 = ImageFeatureValue.CreateFromVideoFrame(inputImage); //Evaluate the model ModelOutput = await ModelGen.EvaluateAsync(ModelInput); //Convert output to datatype IReadOnlyList <float> vectorImage = ModelOutput.stage_30mid_conv70BiasAdd00.GetAsVectorView(); IList <float> imageList = vectorImage.ToList(); //LINQ query to check for highest probability digit var maxIndex = imageList.IndexOf(imageList.Max()); //Display the results Result.Text = maxIndex.ToString(); }
private async Task <SoftwareBitmap> CreateSoftwareBitmapFromFile(StorageFile inputFile) { // <SnippetCreateSoftwareBitmapFromFile> SoftwareBitmap softwareBitmap; 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 softwareBitmap = await decoder.GetSoftwareBitmapAsync(); } // </SnippetCreateSoftwareBitmapFromFile> return(softwareBitmap); }
private async Task <(SoftwareBitmap software, WriteableBitmap writeable)> CaptureBitmapsAsync() { using var ras = new InMemoryRandomAccessStream(); var encoding = ImageEncodingProperties.CreateJpeg(); await MediaCapture.CapturePhotoToStreamAsync(encoding, ras); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(ras); SoftwareBitmap sw = await decoder.GetSoftwareBitmapAsync(); var writeable = new WriteableBitmap(sw.PixelWidth, sw.PixelHeight); sw.CopyToBuffer(writeable.PixelBuffer); return(sw, writeable); }
public static async Task <SoftwareBitmap> StorageFileToSoftwareBitmap(StorageFile file) { if (file == null) { return(null); } using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); return(softwareBitmap); } }
/// <summary> /// Trigger file picker and image evaluation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ButtonRun_Click(object sender, RoutedEventArgs e) { ButtonRun.IsEnabled = false; UIPreviewImage.Source = null; try { // Load the model await LoadModelAsync(); // Trigger file picker to select an image file FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; fileOpenPicker.FileTypeFilter.Add(".jpg"); fileOpenPicker.FileTypeFilter.Add(".png"); fileOpenPicker.ViewMode = PickerViewMode.Thumbnail; StorageFile selectedStorageFile = await fileOpenPicker.PickSingleFileAsync(); SoftwareBitmap softwareBitmap; using (IRandomAccessStream stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file in BGRA8 format softwareBitmap = await decoder.GetSoftwareBitmapAsync(); softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } // Display the image SoftwareBitmapSource imageSource = new SoftwareBitmapSource(); await imageSource.SetBitmapAsync(softwareBitmap); UIPreviewImage.Source = imageSource; // Encapsulate the image within a VideoFrame to be bound and evaluated VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); await EvaluateVideoFrameAsync(inputImage); } catch (Exception ex) { StatusBlock.Text = $"error: {ex.Message}"; ButtonRun.IsEnabled = true; } }
private async void button_addPhoto_Click(object sender, RoutedEventArgs e) { try { //Initialize Camera Capture UI CameraCaptureUI cc = new CameraCaptureUI(); //Set image format to png cc.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png; //Capture photo var photo = await cc.CaptureFileAsync(CameraCaptureUIMode.Photo); //If no photo is captured, show error message if (photo == null) { MessageDialog msg = new MessageDialog("No Photo", "No photo was captured!"); await msg.ShowAsync(); return; } else { //Read photo as stream stream = await photo.OpenAsync(FileAccessMode.Read); //Decode to bitmap format BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync(); SoftwareBitmap bitmap2 = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SoftwareBitmapSource bmpSrc = new SoftwareBitmapSource(); await bmpSrc.SetBitmapAsync(bitmap2); //Set source for the image control to display the captured image PhotoControl.Source = bmpSrc; } } catch (Exception ex) { msg.Title = "Error!"; msg.Content = ex.Message; await msg.ShowAsync(); } }
private async Task openAndSetImageAsync(StorageFile file) { IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(softwareBitmapBGR8); imageControl.Source = bitmapSource; imageStream = await file.OpenStreamForReadAsync(); }
// 파일의 내용을 TextBox의 컨텐츠로 세팅해주는 메소드이다. // 텍스트파일이면 FileOpenPicker로 파일을 열어 ReadTextAsync로 읽는다. // 또한 이미지파일이면 OcrEngine을 이용하여 안에 있는 텍스트를 인식한다. public async void OpenAsync(Image source, TextBox target) { try { FileOpenPicker picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.DocumentsLibrary }; picker.FileTypeFilter.Add(text_file_extension); picker.FileTypeFilter.Add(image_file_extension); StorageFile file = await picker.PickSingleFileAsync(); switch (file.FileType) { case text_file_extension: target.Text = await FileIO.ReadTextAsync(file); break; case image_file_extension: using (IRandomAccessStream stream = await file.OpenReadAsync()) { BitmapDecoder bitmapDecoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap = await bitmapDecoder.GetSoftwareBitmapAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); OcrEngine engine = OcrEngine.TryCreateFromLanguage(new Language("en-us")); OcrResult ocrResult = await engine.RecognizeAsync(softwareBitmap); target.Text = ocrResult.Text; stream.Seek(0); BitmapImage image = new BitmapImage(); image.SetSource(stream); source.Source = image; } break; default: break; } } catch { } }
/// <summary> /// Get the metadata for the specified image so that its size and resolution can be determined /// Typical usage would be /// softwareBitmap = await GetSoftwareBitmapFromFileAsync(file); /// </summary> /// <param name="file">Image file</param> /// <returns>Task<SoftwareBitmap></returns> public async Task <SoftwareBitmap> GetSoftwareBitmapFromFileAsync(StorageFile file) { SoftwareBitmap softwareBitmap = null; if (file != null) { using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); // Get the SoftwareBitmap representation of the file softwareBitmap = await decoder.GetSoftwareBitmapAsync(); } } return(softwareBitmap); }
private static async Task <SoftwareBitmap> ResourceFileToImageAsync(string fileName) { SoftwareBitmap image = null; StorageFile file = await GetResourceFileAsync(fileName); if (file != null) { using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); image = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } } return(image); }
async Task TakePhotoAsync() { var stream = new InMemoryRandomAccessStream(); await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); await StopPreviewAsync(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(); var data = await EncodedBytes(softwareBitmap, BitmapEncoder.JpegEncoderId); Photo?.Invoke(this, data.ToArray()); }
private async void OnOpenImageButtonClicked(object sender, RoutedEventArgs e) { var picker = new FileOpenPicker(); picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail; picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".jpeg"); picker.FileTypeFilter.Add(".png"); file = await picker.PickSingleFileAsync(); if (file == null) { return; } SoftwareBitmap softwareBitmap; using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { bitmapDecoder = await BitmapDecoder.CreateAsync(stream); softwareBitmap = await bitmapDecoder.GetSoftwareBitmapAsync(); } if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight) { softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } var source = new SoftwareBitmapSource(); await source.SetBitmapAsync(softwareBitmap); img_total.Source = source; var ttv = img_total.TransformToVisual(Window.Current.Content); imagePoint = ttv.TransformPoint(new Point(0, 0)); scale = Math.Min(imageView.ActualWidth / bitmapDecoder.PixelWidth, imageView.ActualHeight / bitmapDecoder.PixelHeight); imagePoint.X = Math.Max(imagePoint.X - (bitmapDecoder.PixelWidth * scale) / 2, 0); imagePoint.Y = Math.Max(imagePoint.Y - (bitmapDecoder.PixelHeight * scale) / 2, 0); isImageReady = true; }