2

JSで記述されたWindows8Metroアプリケーションで、ファイルを開き、ストリームを取得し、「promise-.then」パターンを使用していくつかの画像データを書き込みます。正常に動作します。BitmapEncoderを使用してストリームをファイルにフラッシュした後、ストリームが開いたままであることを除いて、ファイルはファイルシステムに正常に保存されます。すなわち; アプリケーションを強制終了するまでファイルにアクセスできませんが、「stream」変数は参照できる範囲外であるため、close()できません。使用できるC#usingステートメントに匹敵するものはありますか?

...then(function (file) {
                return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
            })
.then(function (stream) {
                //Create imageencoder object
                return Imaging.BitmapEncoder.createAsync(Imaging.BitmapEncoder.pngEncoderId, stream);
            })
.then(function (encoder) {
                //Set the pixel data in the encoder ('canvasImage.data' is an existing image stream)
                encoder.setPixelData(Imaging.BitmapPixelFormat.rgba8, Imaging.BitmapAlphaMode.straight, canvasImage.width, canvasImage.height, 96, 96, canvasImage.data);
                //Go do the encoding
                return encoder.flushAsync();
                //file saved successfully, 
                //but stream is still open and the stream variable is out of scope.
            };
4

1 に答える 1

1

Microsoftのこの単純な画像サンプルが役立つかもしれません。以下にコピーしました。

あなたの場合、then呼び出しのチェーンの前にストリームを宣言し、ストリームを受け入れる関数のパラメーターと名前を衝突させないようにし(それらが行う部分に注意してください_stream = stream)、追加する必要があるようです。thenストリームを閉じるために呼び出します。

function scenario2GetImageRotationAsync(file) { 
    var accessMode = Windows.Storage.FileAccessMode.read; 

    // Keep data in-scope across multiple asynchronous methods 
    var stream; 
    var exifRotation;
    return file.openAsync(accessMode).then(function (_stream) { 
        stream = _stream; 
        return Imaging.BitmapDecoder.createAsync(stream); 
    }).then(function (decoder) { 
        // irrelevant stuff to this question
    }).then(function () { 
        if (stream) { 
            stream.close(); 
        } 
        return exifRotation; 
    }); 
} 
于 2012-04-20T14:04:28.523 に答える