public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { ExportInformation exportInformation = new ExportInformation(Designation, Description); SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(); outputSettings.PreventGreenshotFormat(); if (_presetCommand != null) { if (!config.RunInbackground.ContainsKey(_presetCommand)) { config.RunInbackground.Add(_presetCommand, true); } bool runInBackground = config.RunInbackground[_presetCommand]; string fullPath = captureDetails.Filename; if (fullPath == null) { fullPath = ImageOutput.SaveNamedTmpFile(surface, captureDetails, outputSettings); } string output; string error; if (runInBackground) { Thread commandThread = new Thread(delegate() { CallExternalCommand(exportInformation, fullPath, out output, out error); ProcessExport(exportInformation, surface); }) { Name = "Running " + _presetCommand, IsBackground = true }; commandThread.SetApartmentState(ApartmentState.STA); commandThread.Start(); exportInformation.ExportMade = true; } else { CallExternalCommand(exportInformation, fullPath, out output, out error); ProcessExport(exportInformation, surface); } } return(exportInformation); }
protected override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { var exportInformation = new ExportInformation(Designation, Description); var outputSettings = new SurfaceOutputSettings(); outputSettings.PreventGreenshotFormat(); if (_presetCommand != null) { if (!Config.RunInbackground.ContainsKey(_presetCommand)) { Config.RunInbackground.Add(_presetCommand, true); } var runInBackground = Config.RunInbackground[_presetCommand]; var fullPath = captureDetails.Filename; if (fullPath == null) { fullPath = ImageOutput.SaveNamedTmpFile(surface, captureDetails, outputSettings); } if (runInBackground) { var commandThread = new Thread(() => { CallExternalCommand(exportInformation, fullPath, out _, out _); ProcessExport(exportInformation, surface); }) { Name = "Running " + _presetCommand, IsBackground = true }; commandThread.SetApartmentState(ApartmentState.STA); commandThread.Start(); exportInformation.ExportMade = true; } else { CallExternalCommand(exportInformation, fullPath, out _, out _); ProcessExport(exportInformation, surface); } } return(exportInformation); }
/// <summary> /// Share the screenshot with a windows app /// </summary> /// <param name="manuallyInitiated"></param> /// <param name="surface"></param> /// <param name="captureDetails"></param> /// <returns>ExportInformation</returns> public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { var exportInformation = new ExportInformation(Designation, Description); try { var handle = PluginUtils.Host.GreenshotForm.Handle; var exportTarget = Task.Run(async() => { var taskCompletionSource = new TaskCompletionSource <string>(); using (var imageStream = new MemoryRandomAccessStream()) using (var logoStream = new MemoryRandomAccessStream()) using (var thumbnailStream = new MemoryRandomAccessStream()) { var outputSettings = new SurfaceOutputSettings(); outputSettings.PreventGreenshotFormat(); // Create capture for export ImageOutput.SaveToStream(surface, imageStream, outputSettings); imageStream.Position = 0; Log.Info("Created RandomAccessStreamReference for the image"); var imageRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(imageStream); RandomAccessStreamReference thumbnailRandomAccessStreamReference; RandomAccessStreamReference logoRandomAccessStreamReference; // Create thumbnail using (var tmpImageForThumbnail = surface.GetImageForExport()) { using (var thumbnail = ImageHelper.CreateThumbnail(tmpImageForThumbnail, 240, 160)) { ImageOutput.SaveToStream(thumbnail, null, thumbnailStream, outputSettings); thumbnailStream.Position = 0; thumbnailRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(thumbnailStream); Log.Info("Created RandomAccessStreamReference for the thumbnail"); } } // Create logo using (var logo = GreenshotResources.getGreenshotIcon().ToBitmap()) { using (var logoThumbnail = ImageHelper.CreateThumbnail(logo, 30, 30)) { ImageOutput.SaveToStream(logoThumbnail, null, logoStream, outputSettings); logoStream.Position = 0; logoRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(logoStream); Log.Info("Created RandomAccessStreamReference for the logo"); } } string applicationName = null; var dataTransferManagerHelper = new DataTransferManagerHelper(handle); dataTransferManagerHelper.DataTransferManager.TargetApplicationChosen += (dtm, args) => { Log.InfoFormat("Trying to share with {0}", args.ApplicationName); applicationName = args.ApplicationName; }; var filename = FilenameHelper.GetFilename(OutputFormat.png, captureDetails); var storageFile = await StorageFile.CreateStreamedFileAsync(filename, async streamedFileDataRequest => { // Information on how was found here: https://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/ Log.DebugFormat("Creating deferred file {0}", filename); try { using (var deferredStream = streamedFileDataRequest.AsStreamForWrite()) { await imageStream.CopyToAsync(deferredStream).ConfigureAwait(false); await imageStream.FlushAsync().ConfigureAwait(false); } // Signal that the stream is ready streamedFileDataRequest.Dispose(); } catch (Exception) { streamedFileDataRequest.FailAndClose(StreamedFileFailureMode.Incomplete); } // Signal transfer ready to the await down below taskCompletionSource.TrySetResult(applicationName); }, imageRandomAccessStreamReference).AsTask().ConfigureAwait(false); dataTransferManagerHelper.DataTransferManager.DataRequested += (sender, args) => { var deferral = args.Request.GetDeferral(); args.Request.Data.OperationCompleted += (dp, eventArgs) => { Log.DebugFormat("OperationCompleted: {0}, shared with", eventArgs.Operation); taskCompletionSource.TrySetResult(applicationName); }; var dataPackage = args.Request.Data; dataPackage.Properties.Title = captureDetails.Title; dataPackage.Properties.ApplicationName = "Greenshot"; dataPackage.Properties.Description = "Share a screenshot"; dataPackage.Properties.Thumbnail = thumbnailRandomAccessStreamReference; dataPackage.Properties.Square30x30Logo = logoRandomAccessStreamReference; dataPackage.Properties.LogoBackgroundColor = Color.FromArgb(0xff, 0x3d, 0x3d, 0x3d); dataPackage.SetStorageItems(new List <IStorageItem> { storageFile }); dataPackage.SetBitmap(imageRandomAccessStreamReference); dataPackage.Destroyed += (dp, o) => { Log.Debug("Destroyed."); }; deferral.Complete(); }; dataTransferManagerHelper.ShowShareUi(); return(await taskCompletionSource.Task.ConfigureAwait(false)); } }).Result; if (string.IsNullOrWhiteSpace(exportTarget)) { exportInformation.ExportMade = false; } else { exportInformation.ExportMade = true; exportInformation.DestinationDescription = exportTarget; } } catch (Exception ex) { exportInformation.ExportMade = false; exportInformation.ErrorMessage = ex.Message; } ProcessExport(exportInformation, surface); return(exportInformation); }
/// <summary> /// Share the surface by using the Share-UI of Windows 10 /// </summary> /// <param name="shareInfo">ShareInfo</param> /// <param name="handle">IntPtr with the handle for the hosting window</param> /// <param name="surface">ISurface with the bitmap to share</param> /// <param name="captureDetails">ICaptureDetails</param> /// <returns>Task with string, which describes the application which was used to share with</returns> private async Task Share(ShareInfo shareInfo, IntPtr handle, ISurface surface, ICaptureDetails captureDetails) { using (var imageStream = new MemoryRandomAccessStream()) using (var logoStream = new MemoryRandomAccessStream()) using (var thumbnailStream = new MemoryRandomAccessStream()) { var outputSettings = new SurfaceOutputSettings(); outputSettings.PreventGreenshotFormat(); // Create capture for export ImageOutput.SaveToStream(surface, imageStream, outputSettings); imageStream.Position = 0; Log.Debug().WriteLine("Created RandomAccessStreamReference for the image"); var imageRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(imageStream); // Create thumbnail RandomAccessStreamReference thumbnailRandomAccessStreamReference; using (var tmpImageForThumbnail = surface.GetBitmapForExport()) using (var thumbnail = tmpImageForThumbnail.CreateThumbnail(240, 160)) { ImageOutput.SaveToStream(thumbnail, null, thumbnailStream, outputSettings); thumbnailStream.Position = 0; thumbnailRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(thumbnailStream); Log.Debug().WriteLine("Created RandomAccessStreamReference for the thumbnail"); } // Create logo RandomAccessStreamReference logoRandomAccessStreamReference; using (var logo = GreenshotResources.GetGreenshotIcon().ToBitmap()) using (var logoThumbnail = logo.CreateThumbnail(30, 30)) { ImageOutput.SaveToStream(logoThumbnail, null, logoStream, outputSettings); logoStream.Position = 0; logoRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(logoStream); Log.Info().WriteLine("Created RandomAccessStreamReference for the logo"); } var dataTransferManagerHelper = new DataTransferManagerHelper(handle); dataTransferManagerHelper.DataTransferManager.ShareProvidersRequested += (sender, args) => { shareInfo.AreShareProvidersRequested = true; Log.Debug().WriteLine("Share providers requested: {0}", string.Join(",", args.Providers.Select(p => p.Title))); }; dataTransferManagerHelper.DataTransferManager.TargetApplicationChosen += (dtm, args) => { shareInfo.ApplicationName = args.ApplicationName; Log.Debug().WriteLine("TargetApplicationChosen: {0}", args.ApplicationName); }; var filename = FilenameHelper.GetFilename(OutputFormats.png, captureDetails); var storageFile = await StorageFile.CreateStreamedFileAsync(filename, async streamedFileDataRequest => { shareInfo.IsDeferredFileCreated = true; // Information on the "how" was found here: https://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/ Log.Debug().WriteLine("Creating deferred file {0}", filename); try { using (var deferredStream = streamedFileDataRequest.AsStreamForWrite()) { await imageStream.CopyToAsync(deferredStream).ConfigureAwait(false); await imageStream.FlushAsync().ConfigureAwait(false); } // Signal that the stream is ready streamedFileDataRequest.Dispose(); } catch (Exception) { streamedFileDataRequest.FailAndClose(StreamedFileFailureMode.Incomplete); } }, imageRandomAccessStreamReference).AsTask().ConfigureAwait(false); dataTransferManagerHelper.DataTransferManager.DataRequested += (dataTransferManager, dataRequestedEventArgs) => { var deferral = dataRequestedEventArgs.Request.GetDeferral(); try { shareInfo.IsDataRequested = true; Log.Debug().WriteLine("DataRequested with operation {0}", dataRequestedEventArgs.Request.Data.RequestedOperation); var dataPackage = dataRequestedEventArgs.Request.Data; dataPackage.OperationCompleted += (dp, eventArgs) => { Log.Debug().WriteLine("OperationCompleted: {0}, shared with", eventArgs.Operation); shareInfo.CompletedWithOperation = eventArgs.Operation; shareInfo.AcceptedFormat = eventArgs.AcceptedFormatId; shareInfo.ShareTask.TrySetResult(true); }; dataPackage.Destroyed += (dp, o) => { shareInfo.IsDestroyed = true; Log.Debug().WriteLine("Destroyed"); shareInfo.ShareTask.TrySetResult(true); }; dataPackage.ShareCompleted += (dp, shareCompletedEventArgs) => { shareInfo.IsShareCompleted = true; Log.Debug().WriteLine("ShareCompleted"); shareInfo.ShareTask.TrySetResult(true); }; dataPackage.Properties.Title = captureDetails.Title; dataPackage.Properties.ApplicationName = "Greenshot"; dataPackage.Properties.Description = "Share a screenshot"; dataPackage.Properties.Thumbnail = thumbnailRandomAccessStreamReference; dataPackage.Properties.Square30x30Logo = logoRandomAccessStreamReference; dataPackage.Properties.LogoBackgroundColor = Color.FromArgb(0xff, 0x3d, 0x3d, 0x3d); dataPackage.SetStorageItems(new[] { storageFile }); dataPackage.SetBitmap(imageRandomAccessStreamReference); } finally { deferral.Complete(); Log.Debug().WriteLine("Called deferral.Complete()"); } }; dataTransferManagerHelper.ShowShareUi(); Log.Debug().WriteLine("ShowShareUi finished."); await shareInfo.ShareTask.Task.ConfigureAwait(false); } }