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

您几乎拥有它 - 您缺少的一个步骤是您需要将 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。(我建议使用 memStream 和 fileStream 而不是 fileStream 和 stream 在您的代码中清晰。)

有关完全可操作的示例,请参阅SDK中简单成像示例的场景 2 。

于 2013-11-04T17:49:39.657 回答