本文整理汇总了C#中Windows.Storage.StorageFile.OpenAsync方法的典型用法代码示例。如果您正苦于以下问题:C# StorageFile.OpenAsync方法的具体用法?C# StorageFile.OpenAsync怎么用?C# StorageFile.OpenAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.Storage.StorageFile
的用法示例。
在下文中一共展示了StorageFile.OpenAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveToFile
private async Task<Uri> SaveToFile(StorageFile file)
{
Uri uri = null;
if (inkCanvas.InkPresenter.StrokeContainer.GetStrokes().Count > 0)
{
try
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await inkCanvas.InkPresenter.StrokeContainer.SaveAsync(stream);
uri = new Uri("ms-appdata:///local/" + file.Name);
}
}
catch (Exception ex)
{
if (Debugger.IsAttached)
{
Debugger.Break();
}
}
}
return uri;
}
示例2: OnDeferredImageRequestedHandler
private async void OnDeferredImageRequestedHandler(DataProviderRequest providerRequest, StorageFile imageFile)
{
// In this delegate we provide updated Bitmap data using delayed rendering.
if (imageFile != null)
{
// If the delegate is calling any asynchronous operations it needs to acquire the deferral first. This lets the
// system know that you are performing some operations that might take a little longer and that the call to
// SetData could happen after the delegate returns. Once you acquired the deferral object you must call Complete
// on it after your final call to SetData.
DataProviderDeferral deferral = providerRequest.GetDeferral();
InMemoryRandomAccessStream inMemoryStream = new InMemoryRandomAccessStream();
// Make sure to always call Complete when done with the deferral.
try
{
// Decode the image and re-encode it at 50% width and height.
IRandomAccessStream imageStream = await imageFile.OpenAsync(FileAccessMode.Read);
BitmapDecoder imageDecoder = await BitmapDecoder.CreateAsync(imageStream);
BitmapEncoder imageEncoder = await BitmapEncoder.CreateForTranscodingAsync(inMemoryStream, imageDecoder);
imageEncoder.BitmapTransform.ScaledWidth = (uint)(imageDecoder.OrientedPixelWidth * 0.5);
imageEncoder.BitmapTransform.ScaledHeight = (uint)(imageDecoder.OrientedPixelHeight * 0.5);
await imageEncoder.FlushAsync();
providerRequest.SetData(RandomAccessStreamReference.CreateFromStream(inMemoryStream));
}
finally
{
deferral.Complete();
}
}
}
示例3: SaveToFileAsync
async Task<RenderTargetBitmap> SaveToFileAsync(FrameworkElement uielement, StorageFile file)
{
if (file != null)
{
CachedFileManager.DeferUpdates(file);
Guid encoderId = GetBitmapEncoder(file.FileType);
try
{
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
return await CaptureToStreamAsync(uielement, stream, encoderId);
}
}
catch (Exception ex)
{
DisplayMessage(ex.Message);
}
var status = await CachedFileManager.CompleteUpdatesAsync(file);
}
return null;
}
示例4: GetDominantColorFromFile
public static async Task<Color> GetDominantColorFromFile(StorageFile file)
{
//get the file
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
//Create a decoder for the image
var decoder = await BitmapDecoder.CreateAsync(stream);
//Create a transform to get a 1x1 image
var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };
//Get the pixel provider
var pixels = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Ignore,
myTransform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
//Get the bytes of the 1x1 scaled image
var bytes = pixels.DetachPixelData();
//read the color
var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
return myDominantColor;
}
}
示例5: LoadImage
private async Task<BitmapImage> LoadImage(StorageFile file)
{
var image = new BitmapImage();
FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);
image.SetSource(stream);
return image;
}
示例6: Capture_Click
/// <summary>
/// Triggered when the Capture Photo button is clicked by the user
/// </summary>
private async void Capture_Click(object sender, RoutedEventArgs e)
{
// Hide the capture photo button
CaptureButton.Visibility = Visibility.Collapsed;
// Capture current frame from webcam, store it in temporary storage and set the source of a BitmapImage to said photo
currentIdPhotoFile = await webcam.CapturePhoto();
var photoStream = await currentIdPhotoFile.OpenAsync(FileAccessMode.ReadWrite);
BitmapImage idPhotoImage = new BitmapImage();
await idPhotoImage.SetSourceAsync(photoStream);
// Set the soruce of the photo control the new BitmapImage and make the photo control visible
IdPhotoControl.Source = idPhotoImage;
IdPhotoControl.Visibility = Visibility.Visible;
// Collapse the webcam feed or disabled feed grid. Make the enter user name grid visible.
WebcamFeed.Visibility = Visibility.Collapsed;
DisabledFeedGrid.Visibility = Visibility.Collapsed;
UserNameGrid.Visibility = Visibility.Visible;
// Dispose photo stream
photoStream.Dispose();
}
示例7: GetImageAsync
public async static Task<BitmapImage> GetImageAsync(StorageFile storageFile)
{
BitmapImage bitmapImage = new BitmapImage();
FileRandomAccessStream stream = (FileRandomAccessStream)await storageFile.OpenAsync(FileAccessMode.Read);
bitmapImage.SetSource(stream);
return bitmapImage;
}
示例8: DisplayImageFileAsync
/// <summary>
/// This function would display the Image
/// </summary>
public async Task DisplayImageFileAsync(StorageFile file)
{
// Display the image in the UI.
BitmapImage src = new BitmapImage();
src.SetSource(await file.OpenAsync(FileAccessMode.Read));
Image1.Source = src;
}
示例9: WriteSurveyPhotoMetaData
/// <summary>
/// Write the location metadata to the image file
/// </summary>
public async static Task WriteSurveyPhotoMetaData(StorageFile file, Geoposition pos, string surveyId = "")
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var encoder = await BitmapEncoder.CreateForTranscodingAsync(stream, decoder);
// Write the gps data
var propertySet = new BitmapPropertySet();
var latitudeNumerator = new BitmapTypedValue(CoordDoubleToIntArray(pos.Coordinate.Latitude), PropertyType.Int32Array);
var latitudeRef = new BitmapTypedValue("N", PropertyType.String);
var longitudeNumerator = new BitmapTypedValue(CoordDoubleToIntArray(pos.Coordinate.Longitude), PropertyType.Int32Array);
var longitudeRef = new BitmapTypedValue("W", PropertyType.String);
propertySet.Add("System.GPS.LatitudeNumerator", latitudeNumerator);
propertySet.Add("System.GPS.LatitudeRef", latitudeRef);
propertySet.Add("System.GPS.LatitudeDenominator", new BitmapTypedValue(new [] { 1, 1, 10000 }, PropertyType.Int32Array));
propertySet.Add("System.GPS.LongitudeNumerator", longitudeNumerator);
propertySet.Add("System.GPS.LongitudeRef", longitudeRef);
propertySet.Add("System.GPS.LongitudeDenominator", new BitmapTypedValue(new[] { 1, 1, 10000 }, PropertyType.Int32Array));
// Write the surveyId data
if (!string.IsNullOrEmpty(surveyId))
{
var surveyIdTyped = new BitmapTypedValue(surveyId, Windows.Foundation.PropertyType.String);
propertySet.Add("System.Comment", surveyIdTyped);
}
await encoder.BitmapProperties.SetPropertiesAsync(propertySet);
await encoder.FlushAsync();
}
}
示例10: Save
/// <summary>
/// Saves the specified bitmap to the specified location.
/// </summary>
/// <param name="source">The source bitmap image.</param>
/// <param name="destination">The destination storage file for the image. JPEG, PNG, GIF, BMP and TIFF formats are supported.</param>
/// <param name="width">The width of the image file.</param>
/// <param name="height">The height of the image file.</param>
/// <returns>True if the bitmap file was successfully saved. False otherwise.</returns>
public async Task<bool> Save(WriteableBitmap source, StorageFile destination, int width, int height)
{
if (source == null || destination == null) return false;
try
{
Guid encoderID = GetEncoderId(destination);
using (IRandomAccessStream stream = await destination.OpenAsync(FileAccessMode.ReadWrite))
{
Stream pixelStream = source.PixelBuffer.AsStream();
byte[] pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderID, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)source.PixelWidth, (uint)source.PixelHeight, Constants.DPI, Constants.DPI, pixels);
if (source.PixelWidth != width || source.PixelHeight != height)
{
encoder.BitmapTransform.ScaledWidth = (uint)width;
encoder.BitmapTransform.ScaledHeight = (uint)height;
}
await encoder.FlushAsync();
}
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return false;
}
示例11: seleccionarImagen
private async void seleccionarImagen(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".png");
file = await picker.PickSingleFileAsync();
BitmapImage image = new BitmapImage();
try
{
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await image.SetSourceAsync(stream);
}
ImageBrush brush = new ImageBrush();
brush.Stretch = Stretch.UniformToFill;
brush.ImageSource = image;
imagen.Fill = brush;
}
catch (Exception exception)
{
}
}
示例12: GameViewModel
protected GameViewModel()
: base()
{
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
MediaPlayer = new MediaElement();
});
lock (locker)
{
if (StopwatchFile == null || AlarmFile == null)
{
LoadFileTask = Task.Run(async () =>
{
StorageFolder folder = await (await Package.Current.InstalledLocation.GetFolderAsync("Assets")).GetFolderAsync("Sounds");
StopwatchFile = await folder.GetFileAsync("Stopwatch.wav");
AlarmFile = await folder.GetFileAsync("Alarm.wav");
CheerFile = await folder.GetFileAsync("Cheer.wav");
AwwFile = await folder.GetFileAsync("Aww.wav");
VictoryFile = await folder.GetFileAsync("Victory.wav");
(await StopwatchFile.OpenAsync(FileAccessMode.Read)).AsStream().CopyTo(StopwatchStream);
(await AlarmFile.OpenAsync(FileAccessMode.Read)).AsStream().CopyTo(AlarmStream);
(await CheerFile.OpenAsync(FileAccessMode.Read)).AsStream().CopyTo(CheerStream);
(await AwwFile.OpenAsync(FileAccessMode.Read)).AsStream().CopyTo(AwwStream);
(await VictoryFile.OpenAsync(FileAccessMode.Read)).AsStream().CopyTo(VictoryStream);
});
}
}
}
示例13: EncodeFromStorageFile
public static async Task<string> EncodeFromStorageFile(StorageFile bitmap)
{
var stream = await bitmap.OpenAsync(Windows.Storage.FileAccessMode.Read);
var decoder = await BitmapDecoder.CreateAsync(stream);
var pixels = await decoder.GetPixelDataAsync();
var bytes = pixels.DetachPixelData();
return await EncodeFromByte(bytes, (uint)decoder.PixelWidth, (uint)decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
}
示例14: ToBase64
public static async Task<string> ToBase64(StorageFile bitmap)
{
var stream = await bitmap.OpenAsync(FileAccessMode.Read);
var decoder = await BitmapDecoder.CreateAsync(stream);
var pixels = await decoder.GetPixelDataAsync();
var bytes = pixels.DetachPixelData();
return await ToBase64(bytes, decoder.PixelWidth, decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
}
示例15: GetImage
static async public Task<BitmapImage> GetImage(StorageFile file)
{
IRandomAccessStream photoStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapImage bmpimg = new BitmapImage();
bmpimg.SetSource(photoStream);
return bmpimg;
}