本文整理汇总了C#中IStorageFile.OpenAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IStorageFile.OpenAsync方法的具体用法?C# IStorageFile.OpenAsync怎么用?C# IStorageFile.OpenAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IStorageFile
的用法示例。
在下文中一共展示了IStorageFile.OpenAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveToFile
public static async Task SaveToFile(
this WriteableBitmap writeableBitmap,
IStorageFile outputFile,
Guid encoderId)
{
try
{
Stream stream = writeableBitmap.PixelBuffer.AsStream();
byte[] pixels = new byte[(uint)stream.Length];
await stream.ReadAsync(pixels, 0, pixels.Length);
using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)writeableBitmap.PixelWidth,
(uint)writeableBitmap.PixelHeight,
96,
96,
pixels);
await encoder.FlushAsync();
using (var outputStream = writeStream.GetOutputStreamAt(0))
{
await outputStream.FlushAsync();
}
}
}
catch (Exception ex)
{
string s = ex.ToString();
}
}
示例2: Hash
public static async Task<string> Hash(IStorageFile file)
{
string res = string.Empty;
using(var streamReader = await file.OpenAsync(FileAccessMode.Read))
{
var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
var algHash = alg.CreateHash();
using(BinaryReader reader = new BinaryReader(streamReader.AsStream(), Encoding.UTF8))
{
byte[] chunk;
chunk = reader.ReadBytes(CHUNK_SIZE);
while(chunk.Length > 0)
{
algHash.Append(CryptographicBuffer.CreateFromByteArray(chunk));
chunk = reader.ReadBytes(CHUNK_SIZE);
}
}
res = CryptographicBuffer.EncodeToHexString(algHash.GetValueAndReset());
return res;
}
}
示例3: CreateAsync
public static async Task<FlacMediaSourceAdapter> CreateAsync(IStorageFile file)
{
var fileStream = await file.OpenAsync(FileAccessMode.Read);
var adapter = new FlacMediaSourceAdapter();
adapter.Initialize(fileStream);
return adapter;
}
示例4: Save_as
public async void Save_as(IStorageFile file, Excuse excuseToWrite)
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
using(Stream outputStream = stream.AsStreamForWrite())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(Excuse));
serializer.WriteObject(outputStream, excuseToWrite);
}
OnPropertyChanged("excuseToWrite");
}
示例5: GetImageFromFileAsync
public static async Task<BitmapImage> GetImageFromFileAsync(IStorageFile file)
{
IRandomAccessStream fileStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
// Set the image source to the selected bitmap.
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(fileStream);
return bitmapImage;
}
示例6: OpenFile
public async Task<Excuse> OpenFile(IStorageFile file, Excuse excuseToRead)
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
using(Stream inputStream = stream.AsStreamForRead())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(Excuse));
excuseToRead = serializer.ReadObject(inputStream) as Excuse;
}
return excuseToRead;
}
示例7: ReadFromFile
public static async Task<byte[]> ReadFromFile(IStorageFile file)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
var reader = new DataReader(stream.GetInputStreamAt(0));
var streamSize = (uint) stream.Size;
await reader.LoadAsync(streamSize);
var buffer = new byte[streamSize];
reader.ReadBytes(buffer);
return buffer;
}
示例8: LoadPwDatabase
public async Task LoadPwDatabase(IStorageFile pwDatabaseFile, IList<IUserKey> userKeys, IProgress<double> percentComplete)
{
StorageFile = pwDatabaseFile;
var factory = new KdbReaderFactory(_encryptionEngine,
_keyTransformer,
_hasher,
_gzipStreamFactory);
var file = await pwDatabaseFile.OpenAsync(FileAccessMode.Read);
Stream kdbDataReader = file.AsStream();
this.PwDatabase = await factory.LoadAsync(kdbDataReader, userKeys, percentComplete);
}
示例9: ReadGuyAsync
public async void ReadGuyAsync()
{
if (String.IsNullOrEmpty(Path))
return;
latestGuyFile = await StorageFile.GetFileFromPathAsync(Path);
using (IRandomAccessStream stream =
await latestGuyFile.OpenAsync(FileAccessMode.Read))
using (Stream inputStream = stream.AsStreamForRead())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(Guy));
NewGuy = serializer.ReadObject(inputStream) as Guy;
}
OnPropertyChanged("NewGuy");
OnPropertyChanged("LatestGuyFile");
}
示例10: ReadTextAsync
/// <summary>
/// Reads the contents of the specified file and returns text.
/// </summary>
/// <param name="file">The file to read.</param>
public static async Task<string> ReadTextAsync(IStorageFile file)
{
using (var fs = await file.OpenAsync(FileAccessMode.Read))
{
using (var inStream = fs.GetInputStreamAt(0))
{
using (var reader = new DataReader(inStream))
{
await reader.LoadAsync((uint)fs.Size);
string data = reader.ReadString((uint)fs.Size);
reader.DetachStream();
return data;
}
}
}
}
示例11: SaveToStorageFile
/// <summary>
/// Saves the given <see cref="WriteableBitmap" /> to a specified storage file.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <param name="storageFile">The storage file.</param>
public static async Task SaveToStorageFile(this WriteableBitmap writeableBitmap, IStorageFile storageFile)
{
using (var writestream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, writestream);
using (var pixelStream = writeableBitmap.PixelBuffer.AsStream())
{
var pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
(uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, pixels);
await encoder.FlushAsync();
}
}
}
示例12: WriteTextAsync
/// <summary>
/// Writes text to the specified file.
/// </summary>
/// <param name="file">The file that the text is written to.</param>
/// <param name="contents">The text to write.</param>
public static async Task WriteTextAsync(IStorageFile file, string contents)
{
using (var fs = await file.OpenAsync(FileAccessMode.ReadWrite))
{
using (var outStream = fs.GetOutputStreamAt(0))
{
using (var dataWriter = new DataWriter(outStream))
{
if (contents != null)
{
dataWriter.WriteString(contents);
}
await dataWriter.StoreAsync();
dataWriter.DetachStream();
}
await outStream.FlushAsync();
}
}
}
示例13: ResizeAndSaveAs
internal static async Task ResizeAndSaveAs(IStorageFile source, IStorageFile destination,
int targetDimension)
{
// open the file...
using(var sourceStream = await source.OpenReadAsync())
{
// step one, get a decoder...
var decoder = await BitmapDecoder.CreateAsync(sourceStream);
// step two, create somewhere to put it...
using(var destinationStream = await destination.OpenAsync(FileAccessMode.ReadWrite))
{
// step three, create an encoder...
var encoder = await BitmapEncoder.CreateForTranscodingAsync(destinationStream, decoder);
// how big is it?
uint width = decoder.PixelWidth;
uint height = decoder.PixelHeight;
decimal ratio = (decimal)width / (decimal)height;
// orientation?
bool portrait = width < height;
// step four, configure it...
if (portrait)
{
encoder.BitmapTransform.ScaledHeight = (uint)targetDimension;
encoder.BitmapTransform.ScaledWidth = (uint)((decimal)targetDimension * ratio);
}
else
{
encoder.BitmapTransform.ScaledWidth = (uint)targetDimension;
encoder.BitmapTransform.ScaledHeight = (uint)((decimal)targetDimension / ratio);
}
// step five, write it...
await encoder.FlushAsync();
}
}
}
示例14: WriteGuyAsync
public async void WriteGuyAsync(Guy guyToWrite)
{
IStorageFolder guysFolder =
await KnownFolders.DocumentsLibrary.CreateFolderAsync("Guys",
CreationCollisionOption.OpenIfExists);
latestGuyFile =
await guysFolder.CreateFileAsync(guyToWrite.Name + ".xml",
CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream =
await latestGuyFile.OpenAsync(FileAccessMode.ReadWrite))
using (Stream outputStream = stream.AsStreamForWrite())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(Guy));
serializer.WriteObject(outputStream, guyToWrite);
}
Path = latestGuyFile.Path;
OnPropertyChanged("Path");
OnPropertyChanged("LatestGuyFile");
}
示例15: LoadImage
private static async Task<BitmapImage> LoadImage(IStorageFile file)
{
var bitmapImage = new BitmapImage();
var stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);
bitmapImage.SetSource(stream);
return bitmapImage;
}