//private double AspectRatio = 1.91d; //private void ComboAspectRatioSelectionChanged(object sender, SelectionChangedEventArgs e) //{ // try // { // if(ComboAspectRatio.SelectedIndex != -1) // { // switch(ComboAspectRatio.SelectedIndex) // { // case 0: // AspectRatio = 1.91d; // ImageCropper.AspectRatio = AspectRatio; // break; // case 1: // AspectRatio = 1d; // ImageCropper.AspectRatio = AspectRatio; // break; // case 2: // AspectRatio = 0.8d; // ImageCropper.AspectRatio = AspectRatio; // break; // } // } // } // catch { } //} private async void CropButtonClick(object sender, RoutedEventArgs e) { try { Helper.CreateCachedFolder(); Helper.CreateCachedFolder(); Helper.CreateCachedFolder(); if (IsVideo) { using (var fileStream = await ThumbnailFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None)) await ImageCropper.SaveAsync(fileStream, BitmapFileFormat.Jpeg); ThumbnailFile = await new PhotoHelper().SaveToImage(ThumbnailFile, false); var decoder = await BitmapDecoder.CreateAsync(await ThumbnailFile.OpenReadAsync()); VideoBitmapDecoder = decoder; CurrentCroppedRectForVideo = ImageCropper.CurrentCroppedRect; ShowImagePreview(ThumbnailFile); UploadButton.IsEnabled = true; } else { var cacheFolder = await SessionHelper.LocalFolder.GetFolderAsync("Cache"); var file = await cacheFolder.CreateFileAsync(15.GenerateRandomStringStatic() + ".jpg"); //await ImageCropper.CroppedImage.SaveAsync(file); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None)) await ImageCropper.SaveAsync(fileStream, BitmapFileFormat.Jpeg); FileToUpload = await new PhotoHelper().SaveToImage(file, false, false); //CropGrid.Visibility = Visibility.Collapsed; ShowImagePreview(FileToUpload); UploadButton.IsEnabled = true; } //using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None)) // await ImageCropper.SaveAsync(fileStream, BitmapFileFormat.Jpeg); //ThumbnailFile = await new PhotoHelper().SaveToImage(file, false); ////CropGrid.Visibility = Visibility.Collapsed; //ShowImagePreview(ThumbnailFile); //var converter = new VideoConverter(); //var decoder = await BitmapDecoder.CreateAsync(await ThumbnailFile.OpenReadAsync()); //var converted = await converter.ConvertFiles(new List<StorageFile> { FileToUpload }, false, new Size(decoder.PixelWidth, decoder.PixelHeight)); } catch (Exception ex) { ex.PrintException("CropButtonClick"); } }
private async void CropButtonClick(object sender, RoutedEventArgs e) { try { if (!CanCrop) { Helper.ShowNotify("Please wait a few more seconds...."); return; } Helper.CreateCachedFolder(); Helper.CreateCachedFolder(); Helper.CreateCachedFolder(); if (IsVideo) { using (var fileStream = await ThumbnailFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None)) await ImageCropper.SaveAsync(fileStream, BitmapFileFormat.Jpeg); ThumbnailFile = await new PhotoHelper().SaveToImage(ThumbnailFile, false); var decoder = await BitmapDecoder.CreateAsync(await ThumbnailFile.OpenReadAsync()); BitmapDecoder = decoder; CurrentCroppedRectForVideo = ImageCropper.CurrentCroppedRect; ShowImagePreview(ThumbnailFile); } else { var cacheFolder = await SessionHelper.LocalFolder.GetFolderAsync("Cache"); var file = await cacheFolder.CreateFileAsync(15.GenerateRandomStringStatic() + ".jpg"); //await ImageCropper.CroppedImage.SaveAsync(file); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None)) await ImageCropper.SaveAsync(fileStream, BitmapFileFormat.Jpeg); FileToUpload = await new PhotoHelper().SaveToImage(file, false, false); var decoder = await BitmapDecoder.CreateAsync(await FileToUpload.OpenReadAsync()); BitmapDecoder = decoder; //CropGrid.Visibility = Visibility.Collapsed; ShowImagePreview(FileToUpload); } Editing = false; } catch (Exception ex) { ex.PrintException("CropButtonClick"); } }
public async Task SetFileAsync(StorageFile file) { try { Editing = true; CanCrop = false; LoadingUc.Start(); if (file.IsVideo()) { VideoButton.Visibility = Visibility.Visible; IsVideo = true; FileToUpload = file; ThumbnailFile = await file.GetSnapshotFromD3D(TimeSpan.Zero, true); ThumbnailFile = await new PhotoHelper().SaveToImage(ThumbnailFile, false); double width = 0, height = 0; var decoder = await BitmapDecoder.CreateAsync(await ThumbnailFile.OpenReadAsync()); width = decoder.PixelWidth; height = decoder.PixelHeight; var wRatio = AspectRatioHelper.GetAspectRatioForMedia(width, height); var hRatio = AspectRatioHelper.GetAspectRatioForMedia(height, width); MainCanvas.Visibility = Visibility.Collapsed; AspectRatioSlider.Value = wRatio; ImageCropper.AspectRatio = wRatio; ImageCropper.CropShape = CropShape.Rectangular; await ImageCropper.LoadImageFromFile(ThumbnailFile); CropGrid.Visibility = Visibility.Visible; await Task.Delay(1500); ImageCropper.AspectRatio = DefaultAspectRatio; AspectRatioSlider.Value = DefaultAspectRatio; CanCrop = true; LoadingUc.Stop(); } else { VideoButton.Visibility = Visibility.Collapsed; IsVideo = false; ThumbnailFile = null; BitmapDecoder = null; var editNeeded = false; double width = 0, height = 0; var decoder = await BitmapDecoder.CreateAsync(await file.OpenReadAsync()); width = decoder.PixelWidth; height = decoder.PixelHeight; var wRatio = AspectRatioHelper.GetAspectRatioForMedia(width, height); var hRatio = AspectRatioHelper.GetAspectRatioForMedia(height, width); if (wRatio > 1.91 && wRatio < 0.8) { editNeeded = true; } else { if (hRatio > 1.91 && hRatio < 0.8) { editNeeded = true; } } if (height > width) { editNeeded = true; } //in paeini comment bod FileToUpload = await new PhotoHelper().SaveToImage(file, false); if (!editNeeded) { CropGrid.Visibility = Visibility.Visible; MainCanvas.Visibility = Visibility.Collapsed; AspectRatioSlider.Value = wRatio; ImageCropper.AspectRatio = wRatio; ImageCropper.CropShape = CropShape.Rectangular; await ImageCropper.LoadImageFromFile(file); } else { CropGrid.Visibility = Visibility.Visible; MainCanvas.Visibility = Visibility.Collapsed; AspectRatioSlider.Value = DefaultAspectRatio; ImageCropper.AspectRatio = DefaultAspectRatio; ImageCropper.CropShape = CropShape.Rectangular; await ImageCropper.LoadImageFromFile(file); } //await Task.Delay(1500); CanCrop = true; LoadingUc.Stop(); } } catch { } }
async void ImportFile(StorageFile file) { try { if (Path.GetExtension(file.Path).ToLower() == ".mp4" || Path.GetExtension(file.Path).ToLower() == ".mkv") { IsVideo = true; FileToUpload = file; ThumbnailFile = await file.GetSnapshotFromD3D(TimeSpan.Zero, true); ThumbnailFile = await new PhotoHelper().SaveToImage(ThumbnailFile, false); double width = 0, height = 0; var decoder = await BitmapDecoder.CreateAsync(await ThumbnailFile.OpenReadAsync()); width = decoder.PixelWidth; height = decoder.PixelHeight; var wRatio = AspectRatioHelper.GetAspectRatioForMedia(width, height); var hRatio = AspectRatioHelper.GetAspectRatioForMedia(height, width); MainCanvas.Visibility = /*ImageView.Visibility =*/ Visibility.Collapsed; UploadButton.IsEnabled = false; AspectRatioSlider.Value = wRatio; ImageCropper.AspectRatio = wRatio; ImageCropper.CropShape = CropShape.Rectangular; await ImageCropper.LoadImageFromFile(ThumbnailFile); AspectRatioSlider.Value = DefaultAspectRatio; ImageCropper.AspectRatio = DefaultAspectRatio; //await Task.Delay(2500); //using (var fileStream = await ThumbnailFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None)) // await ImageCropper.SaveAsync(fileStream, BitmapFileFormat.Jpeg); //ThumbnailFile = await new PhotoHelper().SaveToImage(ThumbnailFile, false); //decoder = await BitmapDecoder.CreateAsync(await ThumbnailFile.OpenReadAsync()); //VideoBitmapDecoder = decoder; //CurrentCroppedRectForVideo = ImageCropper.CurrentCroppedRect; //ShowImagePreview(ThumbnailFile); //CropGrid.Visibility = Visibility.Collapsed; //MainCanvas.Visibility = /*ImageView.Visibility =*/ Visibility.Visible; //UploadButton.IsEnabled = true; var editNeeded = false; if (wRatio > 1.91 && wRatio < 0.8) { editNeeded = true; } else { if (hRatio > 1.91 && hRatio < 0.8) { editNeeded = true; } } if (height > width) { editNeeded = true; } if (!editNeeded) { FileToUpload = await new PhotoHelper().SaveToImage(file, false); CropGrid.Visibility = Visibility.Visible; CropGrid.Opacity = 1; MainCanvas.Visibility = /*ImageView.Visibility =*/ Visibility.Collapsed; UploadButton.IsEnabled = false; AspectRatioSlider.Value = wRatio; ImageCropper.AspectRatio = wRatio; ImageCropper.CropShape = CropShape.Rectangular; await ImageCropper.LoadImageFromFile(file); } else { CropGrid.Opacity = 1; CropGrid.Visibility = Visibility.Visible; MainCanvas.Visibility = /*ImageView.Visibility =*/ Visibility.Collapsed; UploadButton.IsEnabled = false; AspectRatioSlider.Value = DefaultAspectRatio; ImageCropper.AspectRatio = DefaultAspectRatio; ImageCropper.CropShape = CropShape.Rectangular; await ImageCropper.LoadImageFromFile(file); } } else { IsVideo = false; ThumbnailFile = null; VideoBitmapDecoder = null; var editNeeded = false; double width = 0, height = 0; var decoder = await BitmapDecoder.CreateAsync(await file.OpenReadAsync()); width = decoder.PixelWidth; height = decoder.PixelHeight; var wRatio = AspectRatioHelper.GetAspectRatioForMedia(width, height); var hRatio = AspectRatioHelper.GetAspectRatioForMedia(height, width); if (wRatio > 1.91 && wRatio < 0.8) { editNeeded = true; } else { if (hRatio > 1.91 && hRatio < 0.8) { editNeeded = true; } } if (height > width) { editNeeded = true; } if (!editNeeded) { //CropGrid.Visibility = Visibility.Collapsed; //ImageView.Visibility = Visibility.Visible; //UploadButton.IsEnabled = true; //ShowImagePreview(file); //in paeini comment bod FileToUpload = await new PhotoHelper().SaveToImage(file, false); CropGrid.Visibility = Visibility.Visible; CropGrid.Opacity = 1; MainCanvas.Visibility = /*ImageView.Visibility =*/ Visibility.Collapsed; UploadButton.IsEnabled = false; AspectRatioSlider.Value = wRatio; ImageCropper.AspectRatio = wRatio; ImageCropper.CropShape = CropShape.Rectangular; await ImageCropper.LoadImageFromFile(file); } else { CropGrid.Opacity = 1; CropGrid.Visibility = Visibility.Visible; MainCanvas.Visibility = /*ImageView.Visibility =*/ Visibility.Collapsed; UploadButton.IsEnabled = false; //Helper.ShowNotify("Your photo is not in a acceptable aspect ratio." + // "\r\nYou need to crop it and then you can upload it.", 4500); AspectRatioSlider.Value = DefaultAspectRatio; ImageCropper.AspectRatio = DefaultAspectRatio; ImageCropper.CropShape = CropShape.Rectangular; await ImageCropper.LoadImageFromFile(file); } //using (var source = new StorageFileImageSource(file)) //using (var renderer = new JpegRenderer(source, JpegOutputColorMode.Yuv420, OutputOption.PreserveAspectRatio)) //{ // var info = await source.GetInfoAsync(); // width = info.ImageSize.Width; // height = info.ImageSize.Height; //} } } catch { } }