コード例 #1
0
        public async Task Load()
        {
            var folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");

            var file = await folder.GetFileAsync("ColorTable.xml");

            var stream = await file.OpenReadAsync();

            using (var stm = WindowsRuntimeStreamExtensions.AsStream(stream))
            {
                XDocument doc = XDocument.Load(stm);
                foreach (XElement color in doc.Root.Elements())
                {
                    string name       = (string)color.Attribute("Name");
                    string colorValue = (string)color.Attribute("Value");
                    if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(colorValue))
                    {
                        NamedColor c = ParseColor(colorValue);
                        c.Name    = name;
                        map[name] = c;
                        orderedList.Add(c);
                    }
                }
            }
        }
コード例 #2
0
ファイル: Utility.cs プロジェクト: Daoting/dt
        /// <summary>
        /// hdt 新增,提供报表中图表的导出
        /// </summary>
        /// <param name="p_bmp"></param>
        /// <returns></returns>
        public static Stream GetBmpStream(RenderTargetBitmap p_bmp)
        {
            Task <IBuffer> taskBuf = taskBuf = p_bmp.GetPixelsAsync().AsTask();

            taskBuf.Wait();
            byte[] data = taskBuf.Result.ToArray();

            InMemoryRandomAccessStream ms          = new InMemoryRandomAccessStream();
            Task <BitmapEncoder>       taskEncoder = BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms).AsTask();

            taskEncoder.Wait();

            BitmapEncoder encoder = taskEncoder.Result;
            float         dpi     = DisplayInformation.GetForCurrentView().LogicalDpi;

            encoder.SetPixelData(
                BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Ignore,
                (uint)p_bmp.PixelWidth,
                (uint)p_bmp.PixelHeight,
                dpi,
                dpi,
                data);
            encoder.FlushAsync().AsTask().Wait();

            return(WindowsRuntimeStreamExtensions.AsStream(ms));
        }
コード例 #3
0
        /// <summary>
        /// Fixes 3D printing file stream.
        /// </summary>
        /// <param name="inputStream">The input file to be fixed as a Stream object.</param>
        /// <returns>The fixed file as a Stream.</returns>
        public async Task <Stream> FixAsync(Stream inputStream)
        {
            InputStream = inputStream;

            // 1. LoadModelFromPackageAsync accepts IRandomAccessStream and uses stream cloning internally
            // 2. WindowsRuntimeStreamExtensions.AsRandomStream converts Stream to IRandomAccessStream, but the resulting stream doesn't support cloning
            // 3. InMemoryRandomAccessStream does support cloning. So we needed a way to go from Stream to InMemoryRandomAccessStream
            // 4. Solution: Copy Stream to MemoryStream. Write to InMemoryRandomAccessStream via WriteAsync, which accepts IBuffer
            //    To get IBuffer, we first need to get the memoryStream Bytes with ToArray() and then get IBuffer using the
            //    System.Runtime.InteropServices.WindowsRuntime AsBuffer90 extension method.
            //    We then pass the InMemoryRandomAccessStream object to  LoadModelFromPackageAsync.

            using var memoryStream = new MemoryStream();
            await InputStream.CopyToAsync(memoryStream);

            using InMemoryRandomAccessStream memoryRandomAccessStream = new InMemoryRandomAccessStream();
            await memoryRandomAccessStream.WriteAsync(memoryStream.ToArray().AsBuffer());

            var package = new Printing3D3MFPackage();
            var model   = await package.LoadModelFromPackageAsync(memoryRandomAccessStream);

            await model.RepairAsync();

            await package.SaveModelToPackageAsync(model);

            return(WindowsRuntimeStreamExtensions.AsStream(await package.SaveAsync()));
        }
コード例 #4
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            var fileToUpload = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Content/template.xlsx"));

            var fileStream = await fileToUpload.OpenReadAsync();

            App.OneDriveService.RequestViewModel.Api.FileStream = WindowsRuntimeStreamExtensions.AsStream(fileStream);
        }
コード例 #5
0
        public static async Task <IBook> GetBookFromFile(Windows.Storage.IStorageFile file)
        {
            if (file == null)
            {
                return(null);
            }
            else if (Path.GetExtension(file.Path).ToLower() == ".pdf")
            {
                var book = new Books.Pdf.PdfBook();
                try
                {
                    await book.Load(file);
                }
                catch { return(null); }
                if (book.PageCount <= 0)
                {
                    return(null);
                }
                return(book);
            }
            else if (new string[] { ".zip", ".cbz" }.Contains(Path.GetExtension(file.Path).ToLower()))
            {
                var book = new Books.Cbz.CbzBook();
                try
                {
                    await book.LoadAsync(WindowsRuntimeStreamExtensions.AsStream(await file.OpenReadAsync()));
                }
                catch
                {
                    return(null);
                }
                if (book.PageCount <= 0)
                {
                    return(null);
                }
                return(book);
            }
            else if (new string[] { ".rar", ".cbr", ".7z", ".cb7" }.Contains(Path.GetExtension(file.Path).ToLower()))
            {
                var book = new Books.Compressed.CompressedBook();
                try
                {
                    await book.LoadAsync(WindowsRuntimeStreamExtensions.AsStream(await file.OpenReadAsync()));
                }
                catch
                {
                    return(null);
                }
                if (book.PageCount <= 0)
                {
                    return(null);
                }
                return(book);
            }

            return(null);
        }
        public async static Task <Microsoft.OneDrive.Item> UploadFile()
        {
            var filename = TestHelpers.GetFilename();

            Microsoft.OneDrive.Item item;
            var fileToUpload = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Content/template.xlsx"));

            using (var fileStream = await fileToUpload.OpenReadAsync())
            {
                item = await App.OneDriveService.UploadFileAsync("", filename, WindowsRuntimeStreamExtensions.AsStream(fileStream));
            }
            return(item);
        }
コード例 #7
0
        public RequestBuilder DownloadTo(Windows.Storage.IStorageFile file)
        {
            this.success = (headers, result) =>
            {
                long?length = null;
                if (headers.AllKeys.Contains("Content-Length"))
                {
                    length = long.Parse(headers["Content-Length"]);
                }

                var handle = file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite).AsTask().Result;

                ProgressCallbackHelper operation = result.CopyToProgress(WindowsRuntimeStreamExtensions.AsStream(handle), length);
                operation.Completed += (totalbytes) => { handle.Dispose(); };
            };
            return(this);
        }
コード例 #8
0
 public static StreamWriter AppendText(string path, Encoding encoding)
 {
     try
     {
         IAsyncOperation <StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(path).CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.OpenIfExists);
         WindowsRuntimeSystemExtensions.AsTask <StorageFile>(fileAsync).Wait();
         IAsyncOperation <IRandomAccessStream> source = fileAsync.GetResults().OpenAsync(FileAccessMode.ReadWrite);
         WindowsRuntimeSystemExtensions.AsTask <IRandomAccessStream>(source).Wait();
         FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults();
         randomAccessStream.Seek(randomAccessStream.Size);
         return(encoding == null ? new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream)) : new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream), encoding));
     }
     catch (Exception ex)
     {
         throw File.GetRethrowException(ex);
     }
 }
コード例 #9
0
        public async Task UploadFile()
        {
            // Arrange
            var filename = TestHelpers.GetFilename();

            // Act
            Microsoft.OneDrive.Item item;
            var fileToUpload = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Content/template.xlsx"));

            using (var fileStream = await fileToUpload.OpenReadAsync())
            {
                item = await App.OneDriveService.UploadFileAsync("", filename, WindowsRuntimeStreamExtensions.AsStream(fileStream));
            }
            // Assert
            Assert.AreNotEqual(string.Empty, item.Id, "Item Id is blank");
            Assert.AreEqual(filename, item.Name, $"Filename is not {filename}");
            Assert.AreEqual(31616, item.Size, $"File size is not 31616");
        }
コード例 #10
0
        /// <summary>
        /// Fixes 3D printing inputFile and returns path to fixed file.
        /// </summary>
        /// <param name="inputFile">The absolute path to the file to be fixed.</param>
        /// <param name="outputFile">The absolute path to the output fixed file. Defaults to appending '_fixed' to the InputFile file name.</param>
        /// <returns>The absolute path to the fixed file.</returns>
        public async Task <string> FixAsync(string inputFile, string outputFile = "")
        {
            InputFile  = inputFile;
            OutputFile = outputFile;

            var package = new Printing3D3MFPackage();

            using var stream = await FileRandomAccessStream.OpenAsync(InputFile, FileAccessMode.ReadWrite);

            var model = await package.LoadModelFromPackageAsync(stream);

            await model.RepairAsync();

            await package.SaveModelToPackageAsync(model);

            using var outstream = WindowsRuntimeStreamExtensions.AsStream(await package.SaveAsync());
            using var outfile   = File.Create(OutputFile);

            outstream.Seek(0, SeekOrigin.Begin);
            outstream.CopyTo(outfile);

            return(OutputFile);
        }
コード例 #11
0
 public static System.IO.Stream CreateFileStream(string path, ES2Settings.ES2FileMode filemode, int bufferSize)
 {
     if (filemode == ES2Settings.ES2FileMode.Create)
     {
         return(WindowsRuntimeStreamExtensions.AsStream(GetWriteStream(CreateStorageFile(path))));
     }
     else if (filemode == ES2Settings.ES2FileMode.Append)
     {
         return(WindowsRuntimeStreamExtensions.AsStream(GetAppendStream(OpenOrCreateStorageFile(path))));
     }
     else // ES2FileMode.Open
     {
         if (!Exists(path))
         {
             throw new FileNotFoundException();
         }
         StorageFile file = OpenStorageFile(path);
         if (file == null)
         {
             throw new FileNotFoundException();
         }
         return(WindowsRuntimeStreamExtensions.AsStream(GetReadStream(OpenStorageFile(path))));
     }
 }