mirror of
https://github.com/greenshot/greenshot
synced 2025-08-24 07:06:23 -07:00
Migrated the DibHelper to the DibFileFormatHandler
This commit is contained in:
parent
a0b3d22f9b
commit
135efe3704
7 changed files with 105 additions and 21 deletions
|
@ -867,11 +867,17 @@ EndSelection:<<<<<<<4
|
||||||
{
|
{
|
||||||
// Create the stream for the clipboard
|
// Create the stream for the clipboard
|
||||||
dibStream = new MemoryStream();
|
dibStream = new MemoryStream();
|
||||||
var dibBytes = ((Bitmap)imageToSave).ConvertToDib();
|
|
||||||
dibStream.Write(dibBytes,0, dibBytes.Length);
|
|
||||||
|
|
||||||
// Set the DIB to the clipboard DataObject
|
if (!FileFormatHandlerRegistry.TrySaveToStream((Bitmap)imageToSave, dibStream, DataFormats.Dib))
|
||||||
dataObject.SetData(DataFormats.Dib, false, dibStream);
|
{
|
||||||
|
dibStream.Dispose();
|
||||||
|
dibStream = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Set the DIB to the clipboard DataObject
|
||||||
|
dataObject.SetData(DataFormats.Dib, false, dibStream);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception dibEx)
|
catch (Exception dibEx)
|
||||||
|
|
|
@ -31,7 +31,7 @@ using Greenshot.Base.Interfaces.Drawing;
|
||||||
namespace Greenshot.Base.Core.FileFormatHandlers
|
namespace Greenshot.Base.Core.FileFormatHandlers
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// THis is the default .NET bitmap file format handler
|
/// This is the default .NET bitmap file format handler
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DefaultFileFormatHandler : IFileFormatHandler
|
public class DefaultFileFormatHandler : IFileFormatHandler
|
||||||
{
|
{
|
||||||
|
@ -56,7 +56,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OurExtensions.Contains(extension);
|
return OurExtensions.Contains(extension?.ToLowerInvariant());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
|
@ -1,48 +1,100 @@
|
||||||
/*
|
/*
|
||||||
* Greenshot - a free and open source screenshot tool
|
* Greenshot - a free and open source screenshot tool
|
||||||
* Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom
|
* Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom
|
||||||
*
|
*
|
||||||
* For more information see: https://getgreenshot.org/
|
* For more information see: https://getgreenshot.org/
|
||||||
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
|
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 1 of the License, or
|
* the Free Software Foundation, either version 1 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using Greenshot.Base.Interfaces;
|
||||||
|
using Greenshot.Base.Interfaces.Drawing;
|
||||||
using Greenshot.Base.UnmanagedHelpers;
|
using Greenshot.Base.UnmanagedHelpers;
|
||||||
|
|
||||||
namespace Greenshot.Base.Core
|
namespace Greenshot.Base.Core.FileFormatHandlers
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Though Greenshot implements the specs for the DIB image format,
|
/// This handles creating a DIB (Device Independent Bitmap) on the clipboard
|
||||||
/// it seems to cause a lot of issues when using the clipboard.
|
|
||||||
/// There is some research done about the DIB on the clipboard, this code is based upon the information
|
|
||||||
/// <a href="https://stackoverflow.com/questions/44177115/copying-from-and-to-clipboard-loses-image-transparency">here</a>
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static class DibHelper
|
public class DibFileFormatHandler : IFileFormatHandler
|
||||||
{
|
{
|
||||||
private const double DpiToPelsPerMeter = 39.3701;
|
private const double DpiToPelsPerMeter = 39.3701;
|
||||||
|
private static readonly string [] OurExtensions = { "dib" };
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IEnumerable<string> SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction)
|
||||||
|
{
|
||||||
|
if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream)
|
||||||
|
{
|
||||||
|
return Enumerable.Empty<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return OurExtensions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(extension) || fileFormatHandlerAction != FileFormatHandlerActions.SaveToStream)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return OurExtensions.Contains(extension.ToLowerInvariant());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension)
|
||||||
|
{
|
||||||
|
return int.MaxValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension)
|
||||||
|
{
|
||||||
|
var dibBytes = ConvertToDib(bitmap);
|
||||||
|
destination.Write(dibBytes, 0, dibBytes.Length);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface surface)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts the Bitmap to a Device Independent Bitmap format of type BITFIELDS.
|
/// Converts the Bitmap to a Device Independent Bitmap format of type BITFIELDS.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sourceBitmap">Bitmap to convert to DIB</param>
|
/// <param name="sourceBitmap">Bitmap to convert to DIB</param>
|
||||||
/// <returns>byte{} with the image converted to DIB</returns>
|
/// <returns>byte{} with the image converted to DIB</returns>
|
||||||
public static byte[] ConvertToDib(this Bitmap sourceBitmap)
|
private static byte[] ConvertToDib(Bitmap sourceBitmap)
|
||||||
{
|
{
|
||||||
if (sourceBitmap == null) throw new ArgumentNullException(nameof(sourceBitmap));
|
if (sourceBitmap == null) throw new ArgumentNullException(nameof(sourceBitmap));
|
||||||
|
|
|
@ -20,6 +20,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Greenshot.Base.Interfaces;
|
using Greenshot.Base.Interfaces;
|
||||||
|
|
||||||
|
@ -27,7 +29,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
||||||
{
|
{
|
||||||
public static class FileFormatHandlerRegistry
|
public static class FileFormatHandlerRegistry
|
||||||
{
|
{
|
||||||
public static readonly IList<IFileFormatHandler> FileFormatHandlers = new List<IFileFormatHandler>();
|
public static IList<IFileFormatHandler> FileFormatHandlers { get; } = new List<IFileFormatHandler>();
|
||||||
|
|
||||||
static FileFormatHandlerRegistry()
|
static FileFormatHandlerRegistry()
|
||||||
{
|
{
|
||||||
|
@ -40,5 +42,29 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
||||||
{
|
{
|
||||||
return FileFormatHandlers.SelectMany(ffh => ffh.SupportedExtensions(fileFormatHandlerAction)).Distinct();
|
return FileFormatHandlers.SelectMany(ffh => ffh.SupportedExtensions(fileFormatHandlerAction)).Distinct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This wrapper method for TrySaveToStream will do:
|
||||||
|
/// Find all the IFileFormatHandler which support the action for the supplied extension.
|
||||||
|
/// Take the first, to call the TrySaveToStream on.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bitmap">Bitmap</param>
|
||||||
|
/// <param name="destination">Stream</param>
|
||||||
|
/// <param name="extension">string</param>
|
||||||
|
/// <returns>bool</returns>
|
||||||
|
public static bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension)
|
||||||
|
{
|
||||||
|
var fileFormatHandler = FileFormatHandlers
|
||||||
|
.Where(ffh => ffh.Supports(FileFormatHandlerActions.LoadFromStream, extension))
|
||||||
|
.OrderBy(ffh => ffh.PriorityFor(FileFormatHandlerActions.LoadFromStream, extension))
|
||||||
|
.FirstOrDefault();
|
||||||
|
|
||||||
|
if (fileFormatHandler == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileFormatHandler.TrySaveToStream(bitmap, destination, extension);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OurExtensions.Contains(extension);
|
return OurExtensions.Contains(extension?.ToLowerInvariant());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OurExtensions.Contains(extension);
|
return OurExtensions.Contains(extension?.ToLowerInvariant());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
|
@ -55,7 +55,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OurExtensions.Contains(extension);
|
return OurExtensions.Contains(extension?.ToLowerInvariant());
|
||||||
}
|
}
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension)
|
public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue