mirror of
https://github.com/greenshot/greenshot
synced 2025-08-21 22:13: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
|
||||
dibStream = new MemoryStream();
|
||||
var dibBytes = ((Bitmap)imageToSave).ConvertToDib();
|
||||
dibStream.Write(dibBytes,0, dibBytes.Length);
|
||||
|
||||
// Set the DIB to the clipboard DataObject
|
||||
dataObject.SetData(DataFormats.Dib, false, dibStream);
|
||||
if (!FileFormatHandlerRegistry.TrySaveToStream((Bitmap)imageToSave, dibStream, DataFormats.Dib))
|
||||
{
|
||||
dibStream.Dispose();
|
||||
dibStream = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Set the DIB to the clipboard DataObject
|
||||
dataObject.SetData(DataFormats.Dib, false, dibStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception dibEx)
|
||||
|
|
|
@ -31,7 +31,7 @@ using Greenshot.Base.Interfaces.Drawing;
|
|||
namespace Greenshot.Base.Core.FileFormatHandlers
|
||||
{
|
||||
/// <summary>
|
||||
/// THis is the default .NET bitmap file format handler
|
||||
/// This is the default .NET bitmap file format handler
|
||||
/// </summary>
|
||||
public class DefaultFileFormatHandler : IFileFormatHandler
|
||||
{
|
||||
|
@ -56,7 +56,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
|||
return false;
|
||||
}
|
||||
|
||||
return OurExtensions.Contains(extension);
|
||||
return OurExtensions.Contains(extension?.ToLowerInvariant());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
@ -1,48 +1,100 @@
|
|||
/*
|
||||
* Greenshot - a free and open source screenshot tool
|
||||
* Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom
|
||||
*
|
||||
*
|
||||
* For more information see: https://getgreenshot.org/
|
||||
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
|
||||
*
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using Greenshot.Base.Interfaces;
|
||||
using Greenshot.Base.Interfaces.Drawing;
|
||||
using Greenshot.Base.UnmanagedHelpers;
|
||||
|
||||
namespace Greenshot.Base.Core
|
||||
namespace Greenshot.Base.Core.FileFormatHandlers
|
||||
{
|
||||
/// <summary>
|
||||
/// Though Greenshot implements the specs for the DIB image format,
|
||||
/// 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>
|
||||
/// This handles creating a DIB (Device Independent Bitmap) on the clipboard
|
||||
/// </summary>
|
||||
internal static class DibHelper
|
||||
public class DibFileFormatHandler : IFileFormatHandler
|
||||
{
|
||||
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>
|
||||
/// Converts the Bitmap to a Device Independent Bitmap format of type BITFIELDS.
|
||||
/// </summary>
|
||||
/// <param name="sourceBitmap">Bitmap to convert to DIB</param>
|
||||
/// <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));
|
||||
|
|
@ -20,6 +20,8 @@
|
|||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Greenshot.Base.Interfaces;
|
||||
|
||||
|
@ -27,7 +29,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
|||
{
|
||||
public static class FileFormatHandlerRegistry
|
||||
{
|
||||
public static readonly IList<IFileFormatHandler> FileFormatHandlers = new List<IFileFormatHandler>();
|
||||
public static IList<IFileFormatHandler> FileFormatHandlers { get; } = new List<IFileFormatHandler>();
|
||||
|
||||
static FileFormatHandlerRegistry()
|
||||
{
|
||||
|
@ -40,5 +42,29 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
|||
{
|
||||
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 OurExtensions.Contains(extension);
|
||||
return OurExtensions.Contains(extension?.ToLowerInvariant());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
|||
return false;
|
||||
}
|
||||
|
||||
return OurExtensions.Contains(extension);
|
||||
return OurExtensions.Contains(extension?.ToLowerInvariant());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
@ -55,7 +55,7 @@ namespace Greenshot.Base.Core.FileFormatHandlers
|
|||
return false;
|
||||
}
|
||||
|
||||
return OurExtensions.Contains(extension);
|
||||
return OurExtensions.Contains(extension?.ToLowerInvariant());
|
||||
}
|
||||
/// <inheritdoc />
|
||||
public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue