mirror of
https://github.com/greenshot/greenshot
synced 2025-08-23 06:36:20 -07:00
Small fix for using the new clipboard API
This commit is contained in:
parent
3950090416
commit
602e2270d8
2 changed files with 63 additions and 52 deletions
|
@ -64,24 +64,6 @@ namespace Greenshot.Addons.Extensions
|
||||||
".gif"
|
".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>
|
/// <summary>
|
||||||
/// Is there a Bitmap on the clipboard?
|
/// Is there a Bitmap on the clipboard?
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -101,6 +83,46 @@ namespace Greenshot.Addons.Extensions
|
||||||
.Any();
|
.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>
|
/// <summary>
|
||||||
/// A special format 17 bitmap reader
|
/// A special format 17 bitmap reader
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -151,44 +173,22 @@ namespace Greenshot.Addons.Extensions
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a DIB from the Clipboard
|
/// Place the bitmap on the clipboard
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="clipboardAccessToken"></param>
|
/// <param name="clipboardAccessToken">IClipboardAccessToken</param>
|
||||||
/// <returns>Bitmap or null</returns>
|
/// <param name="surface">ISurface</param>
|
||||||
public static Bitmap GetAsDeviceIndependendBitmap(this IClipboardAccessToken clipboardAccessToken)
|
/// <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())
|
using (var bitmapStream = new MemoryStream())
|
||||||
{
|
{
|
||||||
bitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize);
|
ImageOutput.SaveToStream(surface, bitmapStream, outputSettings);
|
||||||
bitmapStream.Write(format17Bytes, 0, format17Bytes.Length);
|
|
||||||
bitmapStream.Seek(0, SeekOrigin.Begin);
|
bitmapStream.Seek(0, SeekOrigin.Begin);
|
||||||
var image = BitmapHelper.FromStream(bitmapStream);
|
// Set the stream
|
||||||
if (image != null)
|
var clipboardFormat = ClipboardFormatExtensions.MapFormatToId(outputSettings.Format.ToString().ToUpperInvariant());
|
||||||
{
|
clipboardAccessToken.SetAsStream(clipboardFormat, bitmapStream);
|
||||||
return image;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Place the surface as Format17 bitmap on the clipboard
|
/// Place the surface as Format17 bitmap on the clipboard
|
||||||
|
@ -200,7 +200,7 @@ namespace Greenshot.Addons.Extensions
|
||||||
// Create the stream for the clipboard
|
// Create the stream for the clipboard
|
||||||
using (var dibV5Stream = new MemoryStream())
|
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);
|
bool dispose = ImageOutput.CreateBitmapFromSurface(surface, outputSettings, out var bitmapToSave);
|
||||||
// Create the BITMAPINFOHEADER
|
// Create the BITMAPINFOHEADER
|
||||||
var header = BitmapInfoHeader.Create(bitmapToSave.Width, bitmapToSave.Height, 32);
|
var header = BitmapInfoHeader.Create(bitmapToSave.Width, bitmapToSave.Height, 32);
|
||||||
|
@ -223,7 +223,8 @@ namespace Greenshot.Addons.Extensions
|
||||||
var bitmapBytes = BitmapToByteArray(bitmapToSave);
|
var bitmapBytes = BitmapToByteArray(bitmapToSave);
|
||||||
// Write to the stream
|
// Write to the stream
|
||||||
dibV5Stream.Write(bitmapBytes, 0, bitmapBytes.Length);
|
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
|
// Set the DIBv5 to the clipboard DataObject
|
||||||
clipboardAccessToken.SetAsStream("Format17", dibV5Stream);
|
clipboardAccessToken.SetAsStream("Format17", dibV5Stream);
|
||||||
if (dispose)
|
if (dispose)
|
||||||
|
|
|
@ -26,10 +26,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using Dapplo.Windows.Clipboard;
|
||||||
using Greenshot.Addons;
|
using Greenshot.Addons;
|
||||||
using Greenshot.Addons.Components;
|
using Greenshot.Addons.Components;
|
||||||
using Greenshot.Addons.Core;
|
using Greenshot.Addons.Core;
|
||||||
|
using Greenshot.Addons.Core.Enums;
|
||||||
|
using Greenshot.Addons.Extensions;
|
||||||
using Greenshot.Addons.Interfaces;
|
using Greenshot.Addons.Interfaces;
|
||||||
|
using Greenshot.Addons.Interfaces.Plugin;
|
||||||
using Greenshot.Configuration;
|
using Greenshot.Configuration;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -60,7 +64,13 @@ namespace Greenshot.Destinations
|
||||||
var exportInformation = new ExportInformation(Designation, Description);
|
var exportInformation = new ExportInformation(Designation, Description);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ClipboardHelper.SetClipboardData(surface);
|
using (var clipboardAccessToken = ClipboardNative.Access())
|
||||||
|
{
|
||||||
|
clipboardAccessToken.ClearContents();
|
||||||
|
|
||||||
|
//clipboardAccessToken.SetAsDeviceIndependendBitmap(surface);
|
||||||
|
clipboardAccessToken.SetAsFormat17(surface);
|
||||||
|
}
|
||||||
exportInformation.ExportMade = true;
|
exportInformation.ExportMade = true;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue