2012-12-26 52 views
0

我试图将图像文件复制到本地存储并调整大小(以便在活动图块中显示它)。我主要是在这里修补,这是我的第一个Windows应用商店应用程序和所有。到目前为止,这是我:在Windows应用商店应用中调整图像大小

var fileStream = null; 
currentPhoto.copyAsync(Windows.Storage.ApplicationData.current.localFolder, currentPhoto.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 = 100; 
     encoder.bitmapTransform.scaledHeight = 100; 
     return encoder.flushAsync(); 
    }) 
    .then(function() { 
     fileStream.close(); 
    }) 
    .done(function() { 
     // do tile update 
    }); 

我会计算一个合适的宽高比后,我得到这个角色的工作,现在是100×100精细的测试。调试时我注意到编码器正在检测到它是一个JPG文件。但是,如果我插入到函数链中调用读取已保存到本地存储的文件,那么我看到它没有被调整大小。所以,自然而然,活动磁贴更新会忽略它,因为它太大。

我在尝试调整图像大小时错过了一个步骤吗?或者也许有更简单的方法来实现这一点?

回答

2

上面的代码应该按预期调整图像大小。但是你并没有在本地存储中调整图像大小。您只是将原始图像复制到本地存储,从那里打开它,然后将图像调整到内存中的流中,当然,如果不修改代码,这是不可见的。

与未成年人修改您的代码,你可以保存调整图像到本地存储,如果这就是你追求的:

var decoder = null; 
var fileStream = null; 
filePicker.pickSingleFileAsync() 
    .then(function(file) { 
     return file.openAsync(Windows.Storage.FileAccessMode.readWrite); 
    }) 
    .then(function(stream) { 
     return Windows.Graphics.Imaging.BitmapDecoder.createAsync(stream); 
    }) 
    .then(function(dec) { 
     decoder = dec; 
     return Windows.Storage.ApplicationData.current.localFolder.createFileAsync("out.jpg"); 
    }) 
    .then(function(file) { 
     return file.openAsync(Windows.Storage.FileAccessMode.readWrite); 
    }) 
    .then(function (stream) { 
     fileStream = stream; 
     return Windows.Graphics.Imaging.BitmapEncoder.createForTranscodingAsync(stream, decoder); 
    }) 
    .then(function(encoder) { 
     encoder.bitmapTransform.scaledWidth = 100; 
     encoder.bitmapTransform.scaledHeight = 100; 
     return encoder.flushAsync(); 
    }) 
    .then(function() { 
     fileStream.close(); 
    }); 

如果你尝试一下,你会看到大小out.jpg在本地创建存储。

相关问题