コード例 #1
0
        public static async Task <ImageFile> FileToImageFileAsync(IStorageFile storageFile, bool readStream = true)
        {
            Stream imageStream = null;

            if (readStream)
            {
                IRandomAccessStreamWithContentType RandomAccessStream = await storageFile?.OpenReadAsync();

                imageStream = RandomAccessStream?.AsStreamForWrite();
            }
            ImageProperties imageProp;
            int             width    = 0;
            int             height   = 0;
            string          filepath = String.Empty;

            if (storageFile is StorageFile sFile)
            {
                if (sFile.Properties != null)
                {
                    imageProp = await sFile.Properties.GetImagePropertiesAsync();

                    width  = (int)imageProp.Width;
                    height = (int)imageProp.Height;
                }
                //path can be empty if from the file picker dialog
                //a image path from the internet was enterted
                if (String.IsNullOrEmpty(storageFile.Path))
                {
                    filepath = sFile.FolderRelativeId;
                }
                else
                {
                    filepath = storageFile.Path;
                }
            }

            if (String.IsNullOrEmpty(filepath))
            {
                filepath = storageFile.Path;
            }

            FileInfo  fileInfo  = String.IsNullOrEmpty(filepath) ? null : new FileInfo(filepath);
            ImageFile imageFile = new ImageFile(filepath, imageStream, width, height, fileInfo);

            imageFile.Tag        = storageFile;
            imageFile.IsReadOnly = (Windows.Storage.FileAttributes.ReadOnly & storageFile.Attributes) == Windows.Storage.FileAttributes.ReadOnly;
            return(imageFile);
        }