/// <summary> /// Sends current image data to clipboard. /// </summary> internal async Task SendClipboardAsync() { if (CurrentImageData == null) { return; } try { IsSendingClipboard = true; var image = await ImageManager.ConvertBytesToBitmapSourceAsync(CurrentImageData); var tcs = new TaskCompletionSource <bool>(); var thread = new Thread(() => { try { Clipboard.SetImage(image); tcs.SetResult(true); } catch (Exception ex) { tcs.SetException(ex); } }); thread.SetApartmentState(ApartmentState.STA); // Clipboard class must run in STA thread. thread.Start(); await tcs.Task; } catch (Exception ex) { Debug.WriteLine($"Failed to send image data to clipboard.\r\n{ex}"); } finally { IsSendingClipboard = false; } }
/// <summary> /// Gets a thumbnail of a specified image file in FlashAir card. /// </summary> /// <param name="client">HttpClient</param> /// <param name="remoteFilePath">Remote file path</param> /// <param name="card">FlashAir card information</param> /// <param name="cancellationToken">CancellationToken</param> /// <returns>Thumbnail of image file</returns> internal static async Task <BitmapSource> GetThumbnailAsync(HttpClient client, string remoteFilePath, CardInfo card, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(remoteFilePath)) { throw new ArgumentNullException(nameof(remoteFilePath)); } var remotePath = ComposeRemotePath(FileManagerCommand.GetThumbnail, remoteFilePath); try { var bytes = await DownloadBytesAsync(client, remotePath, card, cancellationToken).ConfigureAwait(false); return(await ImageManager.ConvertBytesToBitmapSourceAsync(bytes).ConfigureAwait(false)); } catch (ImageNotSupportedException) { // This exception should not be thrown because thumbnail data is directly provided by FlashAir card. return(null); } catch (RemoteFileNotFoundException) { // If the format of image file is not JPEG or if there is no Exif standardized thumbnail stored, // StatusCode will be HttpStatusCode.NotFound. Debug.WriteLine("Image file is not JPEG format or does not contain standardized thumbnail."); throw new RemoteFileThumbnailFailedException(remotePath); } catch (RemoteConnectionUnableException rcue) when(rcue.Code == HttpStatusCode.InternalServerError) { // If image file is non-standard JPEG format, StatusCode may be HttpStatusCode.InternalServerError. Debug.WriteLine("Image file is non-standard JPEG format."); throw new RemoteFileThumbnailFailedException(remotePath); } catch { Debug.WriteLine("Failed to get a thumbnail."); throw; } }