English 中文(简体)
Closing stream after using BitmapEncoder with WinJS Metro app
原标题:

In a Windows 8 Metro application written in JS I open a file, get the stream, write some image data to it using the promise - .then pattern. It works fine - the file is successfully saved to the file system, except after using the BitmapEncoder to flush the stream to the file, the stream is still open. ie; I can t access the file until I kill the application, but the stream variable is out of scope for me to reference, so I can t close() it. Is there something comparable to the C# using statement that could be used?

...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.
            };
最佳回答

This simple imaging sample from Microsoft might help. Copied below.

It looks like, in your case, you need to declare the stream before the chain of then calls, make sure you don t name-collide with your parameter to your function accepting the stream (note the part where they do _stream = stream), and add a then call to close the stream.

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; 
    }); 
} 
问题回答

暂无回答




相关问题
Will WPF be relevant with Windows 8? [closed]

With windows 8 demonstrating native HTML 5 apps . The first thing that I think of is whether WPF has a place in this HTML 5 world. It kind of seems like Silverlight will stick around but I must ask ...

Metro styled Silverlight Applications

Is there any guidance available on developing Metro styled silverlight applications? How does one go about designing the UI to match the metro look and feel of WP7. Going by the way windows 8 s UI &...

Best Alternative for Converting VB6 to run under Windows 8

I originally (many years ago) wrote my applications using VB6 on the assumption that it was a strategic Microsoft product and so I would be able to run them as long as I wanted. However with Windows 8 ...

VB6 Running on Windows 8?

Assuming that vb6 does not run on Windows 8 because the VB6 run time libraries are not shipped with Windows 8, would it not be possible to install the VB6 run time library on Windows 8 and then then ...

How to load any XAP file with the Windows Phone 7 emulator

Does anyone know if it is possible to load a windows phone 7 XAP file into the emulator without the solution or source files? In other words, can I send somebody just a XAP file so that they can test ...

热门标签