【问题标题】:Image is not scaling using WinJS图像未使用 WinJS 缩放
【发布时间】:2013-11-13 21:18:40
【问题描述】:

我正在尝试使用 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
    });
});

【问题讨论】:

    标签: javascript windows-runtime winjs image-scaling


    【解决方案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 在您的代码中清晰。)

    有关完全可操作的示例,请参阅Simple Imaging sample in the SDK 的场景 2。

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2011-05-12
      相关资源
      最近更新 更多