Пример #1
0
 public async Task SaveToFileAsync(IStorageFile storageFile, string password, PasswordKeeper passwordKeeper)
 {
     using (var stream = await storageFile.OpenTransactedWriteAsync())
     {
         using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(passwordKeeper.SaveToJson())))
         {
             EncryptAndDecrypt.Encrypt(mem, password, stream.Stream.AsStreamForWrite());
         }
         await stream.CommitAsync();
     }
 }
        public static async Task SaveAsAsync(this WriteableBitmap image, Guid format, IStorageFile file)
        {
            // bytes...
            var bytes = await image.GetPixelBytesAsync();

            // save...
            using (var stream = await file.OpenTransactedWriteAsync())
            {
                // encoder...
                var encoder = await BitmapEncoder.CreateAsync(format, stream.Stream);
                encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, (uint)image.PixelWidth, (uint)image.PixelHeight,
                    96, 96, bytes);
                await encoder.FlushAsync();
            }
        }
Пример #3
0
        public static async Task SaveAsAsync(this WriteableBitmap image, Guid format, IStorageFile file)
        {
            // bytes...
            var bytes = await image.GetPixelBytesAsync();

            // save...
            using (var stream = await file.OpenTransactedWriteAsync())
            {
                // encoder...
                var encoder = await BitmapEncoder.CreateAsync(format, stream.Stream);

                encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, (uint)image.PixelWidth, (uint)image.PixelHeight,
                                     96, 96, bytes);
                await encoder.FlushAsync();
            }
        }
        //Stream的写入操作
        //文件的Stream其实就是文件内的信息,所以再用Stream来写入文件的数据时,直接保存Stream的信息就可以,并不需要再调用文件的对象进行保存
        public async void StreamWrite(IStorageFile file, string content)
        {
            int size = Int32.MaxValue;

            using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync())
            {
                using (DataWriter dataWriter = new DataWriter(transaction.Stream))
                {
                    //文件相关的信息,可以根据文件的规则来进行写入
                    dataWriter.WriteInt32(size);
                    dataWriter.WriteString(content);

                    transaction.Stream.Size = await dataWriter.StoreAsync();

                    //保存Stream数据
                    await transaction.CommitAsync();
                }
            }
        }