简体   繁体   English

winjs图片文件大小

[英]winjs picture file size

I'm trying to get the size (pixel, bites) from a picture in a Win 8 app. 我正在尝试从Win 8应用程序中的图片获取尺寸(像素,咬合)。 I pick the file with the openPicker and I get the file. 我用openPicker选择文件,然后得到文件。 But I can't find the size attributes? 但是我找不到尺寸属性吗? I'll display an error, if the file is to large. 如果文件太大,我将显示一个错误。 Any idea how I can get this info? 知道如何获取此信息吗? Thanks. 谢谢。

After some more hours of searching and trying I've solved it like this: 经过几个小时的搜索和尝试,我已经解决了这个问题:

//add picture
        document.getElementById("btnAddImg").addEventListener('click', function () {
            // Create the picker object and set options
            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.openAsync(Windows.Storage.FileAccessMode.read).done(function (stream) {
                    var fileType = file.contentType;
                    var blob = MSApp.createBlobFromRandomAccessStream(fileType, stream);
                    var fdata = new FormData();
                    fdata.append('upload_field', blob);
                    //more code....
                });
             });
        });

Hope it helps some others. 希望它对其他人有帮助。

You can get this information through the StorageFile object itself, without having to open the file or load it into memory. 您可以通过StorageFile对象本身获取此信息,而不必打开文件或将其加载到内存中。

For convenience, let's say myFile is the StorageFile in question. 为了方便起见,假设myFile是有问题的StorageFile。

For image dimensions, call myFile.properties.getImagePropertiesAsync , the result of this is an ImageProperties object that contains width and height properties. 对于图像尺寸,调用myFile.properties.getImagePropertiesAsync ,其结果是一个ImageProperties对象,其中包含width和height属性。 You can find an example of retrieving ImageProperties in the Simple imaging sample , scenario 1. 您可以在简单成像示例 (方案1)中找到检索ImageProperty的示例

For the file size, call myFile.getBasicPropertiesAsync . 对于文件大小,请调用myFile.getBasicPropertiesAsync The result is a BasicProperties object whose size property has that value. 结果是一个BasicProperties对象,其size属性具有该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM