0

WinRT を使用して、既存のイメージをそのサイズの 50% にスケーリングしようとしています。イメージをローカル フォルダーにコピーしますが、サイズは変更しません。

var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
    openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
    openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
    openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

    openPicker.pickSingleFileAsync().then(function (file) {
        file.copyAsync(Windows.Storage.ApplicationData.current.localFolder, file.name)
        .then(function (file) {
            return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
        })
        .then(function (stream) {
            return Windows.Graphics.Imaging.BitmapDecoder.createAsync(stream);
        })
        .then(function (decoder) {
            fileStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
            return Windows.Graphics.Imaging.BitmapEncoder.createForTranscodingAsync(fileStream, decoder);
        })
    .then(function (encoder) {
        encoder.bitmapTransform.scaledWidth = 50;
        encoder.bitmapTransform.scaledHeight = 50;
        return encoder.flushAsync();
    })
    .then(function () {
        fileStream.close();
    })
    .done(function () {
        // use image in the program
    });
});
4

1 に答える 1

1

あなたはほとんどそれを持っています-あなたが見逃している1つのステップは、fileStream(メモリ内にあります)をストリーム(ファイルに接続されている)にコピーする必要があるということです。それ以外の場合は、そのエンコードされた (およびサイズ変更された) メモリ ストリームを捨てているだけです。基本的に、flushAsync と fileStream.close の間に次のようなものを貼り付けます。

}).then(function () {
    // Overwrite the contents of the file with the updated image stream.
    fileStream.seek(0);
    stream.seek(0);
    stream.size = 0;
    return Windows.Storage.Streams.RandomAccessStream.copyAsync(fileStream, stream);

また、fileStream.close に加えて、stream.close を呼び出すことも忘れないでください。(コードを明確にするために、fileStream と stream の代わりに memStream と fileStream を使用することをお勧めします。)

完全に動作する例については、SDK の Simple Imaging サンプルのシナリオ 2 を参照してください。

于 2013-11-04T17:49:39.657 に答える