private async void OnUploadImage(object sender, RoutedEventArgs e) { var filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; filePicker.FileTypeFilter.Add(".jpg"); filePicker.FileTypeFilter.Add(".png"); StorageFile file = await filePicker.PickSingleFileAsync(); if (file == null) { return; } var stream = await file.OpenAsync(FileAccessMode.Read); var image = new BitmapImage(); image.SetSource(stream); image.ImageOpened += async(sender1, e1) => { if (image.PixelHeight > image.PixelWidth) { image.DecodePixelHeight = 900; } else { image.DecodePixelWidth = 900; } stream.Seek(0); var imageStorage = new MenuCardImageStorage(); var storage = new MenuCardStorage(); info.ImageFileName = string.Format("{0}.jpg", Guid.NewGuid().ToString()); await imageStorage.WriteImageAsync(stream, info.ImageFileName); }; image.ImageFailed += (sender1, e1) => { string s = e1.ErrorMessage; }; info.Image = image; }
private static async Task InitSampleDataAsync() { var storage = new MenuCardStorage(); var imageStorage = new MenuCardImageStorage(); if (await storage.IsRoamingFolderEmpty()) { List <MenuCard> menuCards = MenuCardRepository.GetSampleMenuCards().ToList(); foreach (var card in menuCards) { RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromUri(new Uri(card.ImagePath)); using (IRandomAccessStreamWithContentType stream = await streamRef.OpenReadAsync()) { card.ImagePath = string.Format("{0}.png", Guid.NewGuid()); await imageStorage.WriteImageAsync(stream, card.ImagePath); } } await storage.WriteMenuCardsAsync(menuCards); } }