从Imagesource获取流

时间:2016-10-14 08:40:50

标签: c# xamarin.forms

我想从StreamImageSource获取Stream。 我用手机相机拍了一张照片,并在表格上显示了一些预览。因此,我将图片流加载到ImageSource中。

现在我想回到Stream ...但是我的方法总是得到“无法访问封闭的流”的异常

    private static async Task<Stream> GetStreamFromImageSourceAsync(StreamImageSource imageSource, CancellationToken cancellationToken = default(CancellationToken))
    {
        if (imageSource.Stream != null)
        {
            return await imageSource.Stream(cancellationToken);
        }
        return null;
    }

这是来电例程

var s = await GetStreamFromImageSourceAsync((StreamImageSource)item.Source);                        
s.Position = 0;

在这里,我得到了一个例外,我无法从封闭的流中读取

谢谢你们......

1 个答案:

答案 0 :(得分:1)

使用Stream来显示预览后,它就会关闭,您无法再使用它了。

您可以做的是,一旦您从相机获取Stream,请将其复制到public class App extends Application { TableView tableScanSystemForSnapshots = new TableView<>(); private ObservableList<Snapshot> snapshots = FXCollections.observableArrayList(); private FilteredList<Snapshot> filteredListSnapshots = new FilteredList<>(snapshots, s -> true); public App() { } @Override public void start(Stage primaryStage) throws Exception { //Table View Snapshots tableScanSystemForSnapshots.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); //make multiple selection in TableView possible //Initialize Columns TableColumn<Snapshot, String> colVmName = new TableColumn<>("VM Name"); TableColumn<Snapshot, String> colSnapshotName = new TableColumn<>("Snapshot Name"); TableColumn<Snapshot, String> colSnapshotDescription = new TableColumn<>("Description"); TableColumn<Snapshot, String> colCreatedTime = new TableColumn<>("created Time"); TableColumn<Snapshot, String> colCreatedBy = new TableColumn<>("created by"); //Initialize ValueFactory - name has to be the same as in Class: Snapshot colVmName.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("vmName")); colSnapshotName.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("snapshotName")); colSnapshotDescription.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("description")); colCreatedTime.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("createdTime")); colCreatedBy.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("createdBy")); //TextField Filter TextField txtFilter = new TextField(); txtFilter.textProperty().addListener(((observable, oldValue, newValue) -> { filteredListSnapshots.setPredicate(s -> { if(newValue == null||newValue.isEmpty()){ return true; } //Compare String lowerCaseFilter = newValue.toLowerCase(); if (s.getVmName().toLowerCase().contains(lowerCaseFilter)){return true;} //Filter matches VM Name if (s.getSnapshotName().toLowerCase().contains(lowerCaseFilter)){return true;} //Filter matches Snapshot Name return false; // Does not match }); SortedList<Snapshot> sortedData = new SortedList<Snapshot>(filteredListSnapshots); sortedData.comparatorProperty().bind(tableScanSystemForSnapshots.comparatorProperty()); tableScanSystemForSnapshots.setItems(sortedData); })); MemoryStream可以使用并随意重复使用。