public void Execute(NativeViewHierarchyManager nvhm) { var view = nvhm.ResolveView(tag) as FrameworkElement; if (view == null) { promise.Reject(ErrorUnableToSnapshot, "No view found with reactTag: " + tag); return; } try { BitmapEncoder image = CaptureView(view); if ("file" == result) { string filePath = GetFilePath(); Stream stream = File.Create(filePath); image.Save(stream); promise.Resolve(filePath); stream.Close(); } else if ("base64" == result) { MemoryStream stream = new MemoryStream(); image.Save(stream); byte[] imageBytes = stream.ToArray(); string data = Convert.ToBase64String(imageBytes); promise.Resolve(data); stream.Close(); } else if ("data-uri" == result) { MemoryStream stream = new MemoryStream(); image.Save(stream); byte[] imageBytes = stream.ToArray(); string data = Convert.ToBase64String(imageBytes); data = "data:image/" + extension + ";base64," + data; promise.Resolve(data); stream.Close(); } else { promise.Reject(ErrorUnableToSnapshot, "Unsupported result: " + result + ". Try one of: file | base64 | data-uri"); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); promise.Reject(ErrorUnableToSnapshot, "Failed to capture view snapshot"); } }
public async void Execute(NativeViewHierarchyManager nvhm) { var view = nvhm.ResolveView(tag) as FrameworkElement; if (view == null) { promise.Reject(ErrorUnableToSnapshot, "No view found with reactTag: " + tag); return; } try { if ("file" == result) { using (var ras = new InMemoryRandomAccessStream()) { await CaptureView(view, ras); var file = await GetStorageFile(); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { await RandomAccessStream.CopyAndCloseAsync(ras.GetInputStreamAt(0), fileStream.GetOutputStreamAt(0)); promise.Resolve(file.Path); } } } else if ("base64" == result) { using (var ras = new InMemoryRandomAccessStream()) { await CaptureView(view, ras); var imageBytes = new byte[ras.Size]; await ras.AsStream().ReadAsync(imageBytes, 0, imageBytes.Length); string data = Convert.ToBase64String(imageBytes); promise.Resolve(data); } } else if ("data-uri" == result) { using (var ras = new InMemoryRandomAccessStream()) { await CaptureView(view, ras); var imageBytes = new byte[ras.Size]; await ras.AsStream().ReadAsync(imageBytes, 0, imageBytes.Length); string data = Convert.ToBase64String(imageBytes); data = "data:image/" + extension + ";base64," + data; promise.Resolve(data); } } else { promise.Reject(ErrorUnableToSnapshot, "Unsupported result: " + result + ". Try one of: file | base64 | data-uri"); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); promise.Reject(ErrorUnableToSnapshot, "Failed to capture view snapshot"); } }
public void Execute(NativeViewHierarchyManager nativeViewHierarchyManager) { _action(); }