Small fix for using the new clipboard API

This commit is contained in:
Robin 2018-06-07 08:25:26 +02:00
commit 602e2270d8
2 changed files with 63 additions and 52 deletions

View file

@ -64,24 +64,6 @@ namespace Greenshot.Addons.Extensions
".gif"
};
/// <summary>
/// Place the bitmap on the clipboard
/// </summary>
/// <param name="clipboardAccessToken">IClipboardAccessToken</param>
/// <param name="surface">ISurface</param>
/// <param name="outputSettings">SurfaceOutputSettings specifying how to output the surface</param>
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);
}
}
/// <summary>
/// Is there a Bitmap on the clipboard?
/// </summary>
@ -101,6 +83,46 @@ namespace Greenshot.Addons.Extensions
.Any();
}
/// <summary>
/// Get a DIB from the Clipboard
/// </summary>
/// <param name="clipboardAccessToken"></param>
/// <returns>Bitmap or null</returns>
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<BitmapInfoHeader>(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;
}
/// <summary>
/// A special format 17 bitmap reader
/// </summary>
@ -151,43 +173,21 @@ namespace Greenshot.Addons.Extensions
}
/// <summary>
/// Get a DIB from the Clipboard
/// Place the bitmap on the clipboard
/// </summary>
/// <param name="clipboardAccessToken"></param>
/// <returns>Bitmap or null</returns>
public static Bitmap GetAsDeviceIndependendBitmap(this IClipboardAccessToken clipboardAccessToken)
/// <param name="clipboardAccessToken">IClipboardAccessToken</param>
/// <param name="surface">ISurface</param>
/// <param name="outputSettings">SurfaceOutputSettings specifying how to output the surface</param>
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<BitmapInfoHeader>(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;
}
/// <summary>
@ -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)

View file

@ -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)