Exemplo n.º 1
0
        /// <summary>
        /// 保存远程url地址图片,到本地
        /// </summary>
        /// <param name="uri">远程地址</param>
        /// <param name="filename">保存的名称</param>
        /// <param name="folder">保存位置枚举</param>
        /// <returns></returns>
        internal async static Task SaveImageFromUrl(string uri, string filename, StorageFolder folder)
        {
            var rass = RandomAccessStreamReference.CreateFromUri(new Uri(uri));
            IRandomAccessStream inputStream = await rass.OpenReadAsync();

            Stream input = WindowsRuntimeStreamExtensions.AsStreamForRead(inputStream.GetInputStreamAt(0));

            try
            {
                //获取图片扩展名的Guid
                StorageFile outputFile = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

                using (IRandomAccessStream outputStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    Stream output = WindowsRuntimeStreamExtensions.AsStreamForWrite(outputStream.GetOutputStreamAt(0));
                    await input.CopyToAsync(output);

                    output.Dispose();
                    input.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 流转换成本地图片
        /// </summary>
        /// <param name="inputstream"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        internal async static Task StreamToFileName(Stream inputstream, string filename)
        {
            try
            {
                StorageFolder folder     = KnownFolders.SavedPictures;
                StorageFile   outputFile = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

                using (IRandomAccessStream output = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    Stream outputstream = WindowsRuntimeStreamExtensions.AsStreamForWrite(output.GetOutputStreamAt(0));
                    await inputstream.CopyToAsync(outputstream);

                    outputstream.Dispose();
                    inputstream.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 3
0
        public static Stream OpenFileForWriting(string uri)
        {
            string             fileName = Path.GetFileName(uri);
            Task <StorageFile> task1    = WindowsRuntimeSystemExtensions.AsTask <StorageFile>(FileHelper.GetFolderForPathOrURI(uri).CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting));

            task1.Wait();
            if (task1.Status != TaskStatus.RanToCompletion)
            {
                throw new Exception("Failed to open the file");
            }
            Task <IRandomAccessStream> task2 = WindowsRuntimeSystemExtensions.AsTask <IRandomAccessStream>(task1.Result.OpenAsync(FileAccessMode.ReadWrite));

            task2.Wait();
            if (task2.Status != TaskStatus.RanToCompletion)
            {
                throw new Exception("Failed to open the file");
            }
            else
            {
                return(WindowsRuntimeStreamExtensions.AsStreamForWrite((IOutputStream)task2.Result));
            }
        }