diff options
4 files changed, 38 insertions, 50 deletions
diff --git a/ApartmentManager/ApartmentManager/ApartmentManager.csproj b/ApartmentManager/ApartmentManager/ApartmentManager.csproj index 8726694..e628635 100644 --- a/ApartmentManager/ApartmentManager/ApartmentManager.csproj +++ b/ApartmentManager/ApartmentManager/ApartmentManager.csproj @@ -106,7 +106,6 @@ <Compile Include="Handler\LoginHandler.cs" /> <Compile Include="Handler\ApartmentHandler.cs" /> <Compile Include="Model\Apartment.cs" /> - <Compile Include="Persistency\ImgurPhotoUploader.cs" /> <Compile Include="Singletons\BoardMemberCatalogSingleton.cs" /> <Compile Include="Model\Defect.cs" /> <Compile Include="Model\NavMenuItem.cs" /> diff --git a/ApartmentManager/ApartmentManager/Common/ByteArrayToImageConverter.cs b/ApartmentManager/ApartmentManager/Common/ByteArrayToImageConverter.cs index e1d2400..64526a1 100644 --- a/ApartmentManager/ApartmentManager/Common/ByteArrayToImageConverter.cs +++ b/ApartmentManager/ApartmentManager/Common/ByteArrayToImageConverter.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media.Imaging; @@ -31,5 +32,14 @@ namespace ApartmentManager.Common { throw new NotImplementedException(); } + + public async Task<object> ConvertToByte(object value, Type targetType, object parameter, string language) + { + IRandomAccessStream random = await RandomAccessStreamReference.CreateFromFile((StorageFile)value).OpenReadAsync(); + Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random); + Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync(); + byte[] bytes = pixelData.DetachPixelData(); + return bytes; + } } } diff --git a/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs b/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs index ded9732..79b1682 100644 --- a/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs @@ -9,6 +9,9 @@ using ApartmentManager.Model; using ApartmentManager.Persistency; using ApartmentManager.ViewModel; using Newtonsoft.Json; +using Windows.Storage; +using ApartmentManager.Common; +using Windows.Storage.Pickers; namespace ApartmentManager.Handler { @@ -141,7 +144,31 @@ namespace ApartmentManager.Handler { try { - //ApartmentViewModel.NewResident.Picture = await ImgurPhotoUploader.UploadPhotoAsync(); + //Create new file picker + FileOpenPicker fp = new FileOpenPicker() + { + SuggestedStartLocation = PickerLocationId.PicturesLibrary, + ViewMode = PickerViewMode.Thumbnail, + CommitButtonText = "Upload" + }; + fp.FileTypeFilter.Add(".jpg"); + fp.FileTypeFilter.Add(".jpeg"); + fp.FileTypeFilter.Add(".png"); + fp.FileTypeFilter.Add(".gif"); + fp.FileTypeFilter.Add(".apng"); + fp.FileTypeFilter.Add(".tiff"); + fp.FileTypeFilter.Add(".tif"); + fp.FileTypeFilter.Add(".bmp"); + fp.FileTypeFilter.Add(".pdf"); + fp.FileTypeFilter.Add(".xcf"); + fp.FileTypeFilter.Add(".webp"); + + //Get image file with picker + StorageFile file = await fp.PickSingleFileAsync(); + + ByteArrayToImageConverter converter = new ByteArrayToImageConverter(); + ApartmentViewModel.NewResident.Picture = (byte[]) await converter.ConvertToByte(file, typeof(System.Byte), null, ""); + var tmp = ApartmentViewModel.NewResident; ApartmentViewModel.NewResident = new Resident(); ApartmentViewModel.NewResident = tmp; diff --git a/ApartmentManager/ApartmentManager/Persistency/ImgurPhotoUploader.cs b/ApartmentManager/ApartmentManager/Persistency/ImgurPhotoUploader.cs deleted file mode 100644 index 7e9ebf2..0000000 --- a/ApartmentManager/ApartmentManager/Persistency/ImgurPhotoUploader.cs +++ /dev/null @@ -1,48 +0,0 @@ -using Imgur.API.Authentication.Impl; -using Imgur.API.Endpoints.Impl; -using Imgur.API.Models; -using System; -using System.IO; -using System.Threading.Tasks; -using Windows.Storage; -using Windows.Storage.Pickers; - -namespace ApartmentManager.Persistency -{ - public static class ImgurPhotoUploader - { - /// <summary> - /// Opens file picker, uploads chosen image and returns link to it. - /// </summary> - public async static Task<string> UploadPhotoAsync() - { - //Create new file picker - FileOpenPicker fp = new FileOpenPicker() - { - SuggestedStartLocation = PickerLocationId.PicturesLibrary, - ViewMode = PickerViewMode.Thumbnail, - CommitButtonText = "Upload" - }; - fp.FileTypeFilter.Add(".jpg"); - fp.FileTypeFilter.Add(".jpeg"); - fp.FileTypeFilter.Add(".png"); - fp.FileTypeFilter.Add(".gif"); - fp.FileTypeFilter.Add(".apng"); - fp.FileTypeFilter.Add(".tiff"); - fp.FileTypeFilter.Add(".tif"); - fp.FileTypeFilter.Add(".bmp"); - fp.FileTypeFilter.Add(".pdf"); - fp.FileTypeFilter.Add(".xcf"); - fp.FileTypeFilter.Add(".webp"); - - //Get image file with picker - StorageFile file = await fp.PickSingleFileAsync(); - - //Upload to Imgur and return link - var client = new ImgurClient("7b05a61ed8df74f", "ade6f79163e19f92f852bc553bbe399d7d4218fe"); - var endpoint = new ImageEndpoint(client); - IImage image = await endpoint.UploadImageStreamAsync(await file.OpenStreamForReadAsync()); - return image.Link; - } - } -}
\ No newline at end of file |