/// <summary> /// Queues a new image request. The request will be taken care of by an async operation. /// </summary> /// <param name="orgionalRequest">The image request</param> public void QueueImageRequest(ImageManagerRequest orgionalRequest) { // Validate if (String.IsNullOrWhiteSpace(orgionalRequest.Url)) { throw new Exception("You must supply both a ULR and callback"); } ImageManagerRequestInternal serviceRequest = null; // Lock the list lock (m_requestList) { // Add the current request to the back of the list. m_requestList.Add(new ImageManagerRequestInternal(orgionalRequest)); // Check if we should make the request now or not. If we don't // when a request finishes it will pick up the new request. for (int i = 0; i < MAX_RUNNING_IMAGE_REQUESTS; i++) { if (m_requestList.Count > i && !m_requestList[i].isServicing) { serviceRequest = m_requestList[i]; serviceRequest.isServicing = true; break; } } } if (serviceRequest != null) { // Kick off a new thread to service the request. Task.Run(async() => { await ServiceRequest(serviceRequest); }); } }
public ImageManagerRequestInternal(ImageManagerRequest context) { Context = context; }
/// <summary> /// Gets an image and saves it locally. /// </summary> /// <param name="postUrl"></param> public void SaveImageLocally(string postUrl) { try { string imageUrl = GetImageUrl(postUrl); // Fire off a request for the image. Random rand = new Random((int)DateTime.Now.Ticks); ImageManagerRequest request = new ImageManagerRequest() { ImageId = rand.NextDouble().ToString(), Url = imageUrl }; // Set the callback request.OnRequestComplete += async(object sender, ImageManager.ImageManagerResponseEventArgs response) => { try { // If success if (response.Success) { // Get the photos library StorageLibrary myPictures = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures); // Get the save folder StorageFolder saveFolder = myPictures.SaveFolder; // Try to find the saved pictures folder StorageFolder savedPicturesFolder = null; IReadOnlyList <StorageFolder> folders = await saveFolder.GetFoldersAsync(); foreach (StorageFolder folder in folders) { if (folder.DisplayName.Equals("Saved Pictures")) { savedPicturesFolder = folder; } } // If not found create it. if (savedPicturesFolder == null) { savedPicturesFolder = await saveFolder.CreateFolderAsync("Saved Pictures"); } // Write the file. StorageFile file = await savedPicturesFolder.CreateFileAsync($"Baconit Saved Image {DateTime.Now.ToString("MM-dd-yy H.mm.ss")}.jpg"); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { await RandomAccessStream.CopyAndCloseAsync(response.ImageStream.GetInputStreamAt(0), fileStream.GetOutputStreamAt(0)); } // Tell the user m_baconMan.MessageMan.ShowMessageSimple("Image Saved", "You can find the image in the 'Saved Pictures' folder in your photos library."); } } catch (Exception ex) { m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "FailedToSaveImageLocallyCallback", ex); m_baconMan.MessageMan.DebugDia("failed to save image locally in callback", ex); } }; QueueImageRequest(request); } catch (Exception e) { m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "FailedToSaveImageLocally", e); m_baconMan.MessageMan.DebugDia("failed to save image locally", e); } }
/// <summary> /// Queues a new image request. The request will be taken care of by an async operation. /// </summary> /// <param name="orgionalRequest">The image request</param> public void QueueImageRequest(ImageManagerRequest orgionalRequest) { // Validate if(String.IsNullOrWhiteSpace(orgionalRequest.Url)) { throw new Exception("You must supply both a ULR and callback"); } ImageManagerRequestInternal serviceRequest = null; // Lock the list lock(m_requestList) { // Add the current request to the back of the list. m_requestList.Add(new ImageManagerRequestInternal(orgionalRequest)); // Check if we should make the request now or not. If we don't // when a request finishes it will pick up the new request. for (int i = 0; i < MAX_RUNNING_IMAGE_REQUESTS; i++) { if (m_requestList.Count > i && !m_requestList[i].isServicing) { serviceRequest = m_requestList[i]; serviceRequest.isServicing = true; break; } } } if(serviceRequest != null) { // Kick off a new thread to service the request. Task.Run(async () => { await ServiceRequest(serviceRequest); }); } }
/// <summary> /// Gets an image and saves it locally. /// </summary> /// <param name="postUrl"></param> public void SaveImageLocally(string postUrl) { try { string imageUrl = GetImageUrl(postUrl); // Fire off a request for the image. Random rand = new Random((int)DateTime.Now.Ticks); ImageManagerRequest request = new ImageManagerRequest() { ImageId = rand.NextDouble().ToString(), Url = imageUrl }; // Set the callback request.OnRequestComplete += async (object sender, ImageManager.ImageManagerResponseEventArgs response) => { try { // If success if(response.Success) { // Get the photos library StorageLibrary myPictures = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures); // Get the save folder StorageFolder saveFolder = myPictures.SaveFolder; // Try to find the saved pictures folder StorageFolder savedPicturesFolder = null; IReadOnlyList<StorageFolder> folders = await saveFolder.GetFoldersAsync(); foreach(StorageFolder folder in folders) { if (folder.DisplayName.Equals("Saved Pictures")) { savedPicturesFolder = folder; } } // If not found create it. if(savedPicturesFolder == null) { savedPicturesFolder = await saveFolder.CreateFolderAsync("Saved Pictures"); } // Write the file. StorageFile file = await savedPicturesFolder.CreateFileAsync($"Baconit Saved Image {DateTime.Now.ToString("MM-dd-yy H.mm.ss")}.jpg"); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { await RandomAccessStream.CopyAndCloseAsync(response.ImageStream.GetInputStreamAt(0), fileStream.GetOutputStreamAt(0)); } // Tell the user m_baconMan.MessageMan.ShowMessageSimple("Image Saved", "You can find the image in the 'Saved Pictures' folder in your photos library."); } } catch(Exception ex) { m_baconMan.TelemetryMan.ReportUnexpectedEvent(this, "FailedToSaveImageLocallyCallback", ex); m_baconMan.MessageMan.DebugDia("failed to save image locally in callback", ex); } }; QueueImageRequest(request); } catch (Exception e) { m_baconMan.TelemetryMan.ReportUnexpectedEvent(this, "FailedToSaveImageLocally", e); m_baconMan.MessageMan.DebugDia("failed to save image locally", e); } }