diff --git a/src/Greenshot.Addons/Extensions/ClipboardBitmapExtensions.cs b/src/Greenshot.Addons/Extensions/ClipboardBitmapExtensions.cs index f7712ac81..907acc860 100644 --- a/src/Greenshot.Addons/Extensions/ClipboardBitmapExtensions.cs +++ b/src/Greenshot.Addons/Extensions/ClipboardBitmapExtensions.cs @@ -64,24 +64,6 @@ namespace Greenshot.Addons.Extensions ".gif" }; - /// - /// Place the bitmap on the clipboard - /// - /// IClipboardAccessToken - /// ISurface - /// SurfaceOutputSettings specifying how to output the surface - public static void SetAsBitmap(this IClipboardAccessToken clipboardAccessToken, ISurface surface, SurfaceOutputSettings outputSettings) - { - using (var bitmapStream = new MemoryStream()) - { - ImageOutput.SaveToStream(surface, bitmapStream, outputSettings); - bitmapStream.Seek(0, SeekOrigin.Begin); - // Set the stream - var clipboardFormat = ClipboardFormatExtensions.MapFormatToId(outputSettings.Format.ToString().ToUpperInvariant()); - clipboardAccessToken.SetAsStream(clipboardFormat, bitmapStream); - } - } - /// /// Is there a Bitmap on the clipboard? /// @@ -101,6 +83,46 @@ namespace Greenshot.Addons.Extensions .Any(); } + /// + /// Get a DIB from the Clipboard + /// + /// + /// Bitmap or null + public static Bitmap GetAsDeviceIndependendBitmap(this IClipboardAccessToken clipboardAccessToken) + { + var formats = clipboardAccessToken.AvailableFormats().ToList(); + if (!formats.Contains(StandardClipboardFormats.Bitmap.AsString())) + { + return null; + } + + var format17Bytes = clipboardAccessToken.GetAsBytes(StandardClipboardFormats.Bitmap.AsString()); + var infoHeader = BinaryStructHelper.FromByteArray(format17Bytes); + if (infoHeader.IsDibV5) + { + Log.Warn().WriteLine("Getting DIBV5 (format 17) when requesting DIB"); + return null; + } + + // Bitmap version older than 5 + var fileHeaderSize = Marshal.SizeOf(typeof(BitmapFileHeader)); + var fileHeader = BitmapFileHeader.Create(infoHeader); + var fileHeaderBytes = BinaryStructHelper.ToByteArray(fileHeader); + + using (var bitmapStream = new MemoryStream()) + { + bitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize); + bitmapStream.Write(format17Bytes, 0, format17Bytes.Length); + bitmapStream.Seek(0, SeekOrigin.Begin); + var image = BitmapHelper.FromStream(bitmapStream); + if (image != null) + { + return image; + } + } + return null; + } + /// /// A special format 17 bitmap reader /// @@ -151,43 +173,21 @@ namespace Greenshot.Addons.Extensions } /// - /// Get a DIB from the Clipboard + /// Place the bitmap on the clipboard /// - /// - /// Bitmap or null - public static Bitmap GetAsDeviceIndependendBitmap(this IClipboardAccessToken clipboardAccessToken) + /// IClipboardAccessToken + /// ISurface + /// SurfaceOutputSettings specifying how to output the surface + public static void SetAsBitmap(this IClipboardAccessToken clipboardAccessToken, ISurface surface, SurfaceOutputSettings outputSettings) { - var formats = clipboardAccessToken.AvailableFormats().ToList(); - if (!formats.Contains(StandardClipboardFormats.Bitmap.AsString())) - { - return null; - } - - var format17Bytes = clipboardAccessToken.GetAsBytes(StandardClipboardFormats.Bitmap.AsString()); - var infoHeader = BinaryStructHelper.FromByteArray(format17Bytes); - if (infoHeader.IsDibV5) - { - Log.Warn().WriteLine("Getting DIBV5 (format 17) when requesting DIB"); - return null; - } - - // Bitmap version older than 5 - var fileHeaderSize = Marshal.SizeOf(typeof(BitmapFileHeader)); - var fileHeader = BitmapFileHeader.Create(infoHeader); - var fileHeaderBytes = BinaryStructHelper.ToByteArray(fileHeader); - using (var bitmapStream = new MemoryStream()) { - bitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize); - bitmapStream.Write(format17Bytes, 0, format17Bytes.Length); + ImageOutput.SaveToStream(surface, bitmapStream, outputSettings); bitmapStream.Seek(0, SeekOrigin.Begin); - var image = BitmapHelper.FromStream(bitmapStream); - if (image != null) - { - return image; - } + // Set the stream + var clipboardFormat = ClipboardFormatExtensions.MapFormatToId(outputSettings.Format.ToString().ToUpperInvariant()); + clipboardAccessToken.SetAsStream(clipboardFormat, bitmapStream); } - return null; } /// @@ -200,7 +200,7 @@ namespace Greenshot.Addons.Extensions // Create the stream for the clipboard using (var dibV5Stream = new MemoryStream()) { - var outputSettings = new SurfaceOutputSettings(OutputFormats.png, 100, false); + var outputSettings = new SurfaceOutputSettings(OutputFormats.bmp, 100, false); bool dispose = ImageOutput.CreateBitmapFromSurface(surface, outputSettings, out var bitmapToSave); // Create the BITMAPINFOHEADER var header = BitmapInfoHeader.Create(bitmapToSave.Width, bitmapToSave.Height, 32); @@ -223,7 +223,8 @@ namespace Greenshot.Addons.Extensions var bitmapBytes = BitmapToByteArray(bitmapToSave); // Write to the stream dibV5Stream.Write(bitmapBytes, 0, bitmapBytes.Length); - + // Reset the stream to the beginning so it can be written + dibV5Stream.Seek(0, SeekOrigin.Begin); // Set the DIBv5 to the clipboard DataObject clipboardAccessToken.SetAsStream("Format17", dibV5Stream); if (dispose) diff --git a/src/Greenshot/Destinations/ClipboardDestination.cs b/src/Greenshot/Destinations/ClipboardDestination.cs index ceca392f3..72c66f823 100644 --- a/src/Greenshot/Destinations/ClipboardDestination.cs +++ b/src/Greenshot/Destinations/ClipboardDestination.cs @@ -26,10 +26,14 @@ using System; using System.Drawing; using System.Windows.Forms; +using Dapplo.Windows.Clipboard; using Greenshot.Addons; using Greenshot.Addons.Components; using Greenshot.Addons.Core; +using Greenshot.Addons.Core.Enums; +using Greenshot.Addons.Extensions; using Greenshot.Addons.Interfaces; +using Greenshot.Addons.Interfaces.Plugin; using Greenshot.Configuration; #endregion @@ -60,7 +64,13 @@ namespace Greenshot.Destinations var exportInformation = new ExportInformation(Designation, Description); try { - ClipboardHelper.SetClipboardData(surface); + using (var clipboardAccessToken = ClipboardNative.Access()) + { + clipboardAccessToken.ClearContents(); + + //clipboardAccessToken.SetAsDeviceIndependendBitmap(surface); + clipboardAccessToken.SetAsFormat17(surface); + } exportInformation.ExportMade = true; } catch (Exception)