2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2021-07-29 15:22:51 +12:00
|
|
|
|
Copyright (c) 2007-2021 ShareX Team
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
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 2
|
|
|
|
|
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, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.IO;
|
2015-12-30 00:26:22 +13:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public static class ClipboardHelpers
|
|
|
|
|
{
|
2020-09-14 06:00:49 +12:00
|
|
|
|
public const string FORMAT_PNG = "PNG";
|
|
|
|
|
public const string FORMAT_17 = "Format17";
|
|
|
|
|
|
2019-12-13 02:06:57 +13:00
|
|
|
|
private const int RetryTimes = 20;
|
|
|
|
|
private const int RetryDelay = 100;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
private static readonly object ClipboardLock = new object();
|
|
|
|
|
|
|
|
|
|
private static bool CopyData(IDataObject data, bool copy = true)
|
|
|
|
|
{
|
|
|
|
|
if (data != null)
|
|
|
|
|
{
|
|
|
|
|
lock (ClipboardLock)
|
|
|
|
|
{
|
|
|
|
|
Clipboard.SetDataObject(data, copy, RetryTimes, RetryDelay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool Clear()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IDataObject data = new DataObject();
|
|
|
|
|
CopyData(data, false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard clear failed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool CopyText(string text)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(text))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IDataObject data = new DataObject();
|
|
|
|
|
string dataFormat;
|
|
|
|
|
|
|
|
|
|
if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 5)
|
|
|
|
|
{
|
|
|
|
|
dataFormat = DataFormats.Text;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dataFormat = DataFormats.UnicodeText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.SetData(dataFormat, false, text);
|
|
|
|
|
return CopyData(data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard copy text failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 08:45:17 +12:00
|
|
|
|
public static bool CopyImage(Image img, string fileName = null)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (img != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-08-17 02:54:43 +12:00
|
|
|
|
if (HelpersOptions.UseAlternativeClipboardCopyImage)
|
|
|
|
|
{
|
2020-08-17 08:45:17 +12:00
|
|
|
|
return CopyImageAlternative2(img, fileName);
|
2020-08-17 02:54:43 +12:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 22:14:39 +13:00
|
|
|
|
if (HelpersOptions.DefaultCopyImageFillBackground)
|
2015-11-11 10:07:13 +13:00
|
|
|
|
{
|
|
|
|
|
return CopyImageDefaultFillBackground(img, Color.White);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CopyImageDefault(img);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard copy image failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 10:07:13 +13:00
|
|
|
|
private static bool CopyImageDefault(Image img)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-01-01 20:37:04 +13:00
|
|
|
|
IDataObject dataObject = new DataObject();
|
|
|
|
|
dataObject.SetData(DataFormats.Bitmap, true, img);
|
|
|
|
|
|
|
|
|
|
return CopyData(dataObject);
|
2015-11-11 10:07:13 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool CopyImageDefaultFillBackground(Image img, Color background)
|
|
|
|
|
{
|
|
|
|
|
using (Bitmap bmp = img.CreateEmptyBitmap(PixelFormat.Format24bppRgb))
|
|
|
|
|
using (Graphics g = Graphics.FromImage(bmp))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-11-11 10:07:13 +13:00
|
|
|
|
g.Clear(background);
|
|
|
|
|
g.DrawImage(img, 0, 0, img.Width, img.Height);
|
|
|
|
|
|
2016-01-01 20:37:04 +13:00
|
|
|
|
IDataObject dataObject = new DataObject();
|
|
|
|
|
dataObject.SetData(DataFormats.Bitmap, true, bmp);
|
|
|
|
|
|
|
|
|
|
return CopyData(dataObject);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
2015-11-11 10:07:13 +13:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-11-11 10:07:13 +13:00
|
|
|
|
private static bool CopyImageAlternative(Image img)
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream msPNG = new MemoryStream())
|
2016-01-01 21:12:05 +13:00
|
|
|
|
using (MemoryStream msBMP = new MemoryStream())
|
|
|
|
|
using (MemoryStream msDIB = new MemoryStream())
|
2016-01-01 20:37:04 +13:00
|
|
|
|
{
|
2016-01-01 21:12:05 +13:00
|
|
|
|
IDataObject dataObject = new DataObject();
|
|
|
|
|
|
2016-01-01 20:37:04 +13:00
|
|
|
|
img.Save(msPNG, ImageFormat.Png);
|
2020-08-17 02:54:43 +12:00
|
|
|
|
dataObject.SetData(FORMAT_PNG, false, msPNG);
|
2016-01-01 20:37:04 +13:00
|
|
|
|
|
2015-11-11 10:07:13 +13:00
|
|
|
|
img.Save(msBMP, ImageFormat.Bmp);
|
|
|
|
|
msBMP.CopyStreamTo(msDIB, 14, (int)msBMP.Length - 14);
|
2016-01-01 20:37:04 +13:00
|
|
|
|
dataObject.SetData(DataFormats.Dib, true, msDIB);
|
|
|
|
|
|
2016-01-01 21:12:05 +13:00
|
|
|
|
return CopyData(dataObject);
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 08:45:17 +12:00
|
|
|
|
private static bool CopyImageAlternative2(Image img, string fileName = null)
|
2020-08-17 02:54:43 +12:00
|
|
|
|
{
|
2020-08-17 04:43:16 +12:00
|
|
|
|
using (Bitmap bmpNonTransparent = img.CreateEmptyBitmap(PixelFormat.Format24bppRgb))
|
2020-08-17 02:54:43 +12:00
|
|
|
|
using (MemoryStream msPNG = new MemoryStream())
|
|
|
|
|
using (MemoryStream msDIB = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
IDataObject dataObject = new DataObject();
|
|
|
|
|
|
2020-08-17 04:43:16 +12:00
|
|
|
|
using (Graphics g = Graphics.FromImage(bmpNonTransparent))
|
|
|
|
|
{
|
|
|
|
|
g.Clear(Color.White);
|
|
|
|
|
g.DrawImage(img, 0, 0, img.Width, img.Height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataObject.SetData(DataFormats.Bitmap, true, bmpNonTransparent);
|
2020-08-17 02:54:43 +12:00
|
|
|
|
|
|
|
|
|
img.Save(msPNG, ImageFormat.Png);
|
|
|
|
|
dataObject.SetData(FORMAT_PNG, false, msPNG);
|
|
|
|
|
|
|
|
|
|
byte[] dibData = ClipboardHelpersEx.ConvertToDib(img);
|
|
|
|
|
msDIB.Write(dibData, 0, dibData.Length);
|
|
|
|
|
dataObject.SetData(DataFormats.Dib, false, msDIB);
|
|
|
|
|
|
2020-08-17 08:45:17 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(fileName))
|
|
|
|
|
{
|
|
|
|
|
string htmlFragment = GenerateHTMLFragment($"<img src=\"{fileName}\"/>");
|
|
|
|
|
dataObject.SetData(DataFormats.Html, htmlFragment);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 02:54:43 +12:00
|
|
|
|
return CopyData(dataObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:53:49 +13:00
|
|
|
|
public static bool CopyFile(string path)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
return CopyFile(new string[] { path });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool CopyFile(string[] paths)
|
|
|
|
|
{
|
|
|
|
|
if (paths != null && paths.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-01-01 20:37:04 +13:00
|
|
|
|
IDataObject dataObject = new DataObject();
|
|
|
|
|
dataObject.SetData(DataFormats.FileDrop, true, paths);
|
|
|
|
|
|
|
|
|
|
return CopyData(dataObject);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard copy file failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 22:59:00 +13:00
|
|
|
|
public static bool CopyImageFromFile(string path)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-22 12:22:43 +13:00
|
|
|
|
using (Bitmap bmp = ImageHelpers.LoadImage(path))
|
2019-12-12 22:59:00 +13:00
|
|
|
|
{
|
2020-08-17 08:45:17 +12:00
|
|
|
|
string fileName = Path.GetFileName(path);
|
|
|
|
|
return CopyImage(bmp, fileName);
|
2019-12-12 22:59:00 +13:00
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2019-12-12 22:59:00 +13:00
|
|
|
|
DebugHelper.WriteException(e, "Clipboard copy image from file failed.");
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 22:59:00 +13:00
|
|
|
|
public static bool CopyTextFromFile(string path)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-12 22:59:00 +13:00
|
|
|
|
string text = File.ReadAllText(path, Encoding.UTF8);
|
|
|
|
|
return CopyText(text);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2019-12-12 22:59:00 +13:00
|
|
|
|
DebugHelper.WriteException(e, "Clipboard copy text from file failed.");
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-30 00:26:22 +13:00
|
|
|
|
|
2020-03-24 08:33:26 +13:00
|
|
|
|
public static Bitmap GetImage(bool checkContainsImage = false)
|
2015-12-30 00:26:22 +13:00
|
|
|
|
{
|
2015-12-30 00:58:21 +13:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lock (ClipboardLock)
|
|
|
|
|
{
|
2019-12-12 22:59:00 +13:00
|
|
|
|
if (!checkContainsImage || Clipboard.ContainsImage())
|
|
|
|
|
{
|
2021-04-06 06:45:52 +12:00
|
|
|
|
if (HelpersOptions.UseAlternativeClipboardGetImage)
|
|
|
|
|
{
|
|
|
|
|
return GetImageAlternative2();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 08:33:26 +13:00
|
|
|
|
return (Bitmap)Clipboard.GetImage();
|
2019-12-12 22:59:00 +13:00
|
|
|
|
}
|
2015-12-30 00:58:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard get image failed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Image GetImageAlternative()
|
|
|
|
|
{
|
|
|
|
|
IDataObject dataObject = Clipboard.GetDataObject();
|
|
|
|
|
|
|
|
|
|
if (dataObject != null)
|
2015-12-30 00:26:22 +13:00
|
|
|
|
{
|
|
|
|
|
string[] dataFormats = dataObject.GetFormats(false);
|
|
|
|
|
|
2016-01-19 10:52:47 +13:00
|
|
|
|
if (dataFormats.Contains(FORMAT_PNG))
|
2015-12-30 00:26:22 +13:00
|
|
|
|
{
|
2016-01-19 10:52:47 +13:00
|
|
|
|
using (MemoryStream ms = dataObject.GetData(FORMAT_PNG) as MemoryStream)
|
2015-12-30 00:26:22 +13:00
|
|
|
|
{
|
2016-01-19 10:52:47 +13:00
|
|
|
|
if (ms != null)
|
|
|
|
|
{
|
|
|
|
|
using (Image img = Image.FromStream(ms))
|
|
|
|
|
{
|
|
|
|
|
return (Image)img.Clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-30 00:26:22 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-19 10:52:47 +13:00
|
|
|
|
else
|
2015-12-30 00:26:22 +13:00
|
|
|
|
{
|
2016-01-19 10:52:47 +13:00
|
|
|
|
foreach (string format in new[] { DataFormats.Dib, FORMAT_17 })
|
2015-12-30 00:26:22 +13:00
|
|
|
|
{
|
2016-01-19 10:52:47 +13:00
|
|
|
|
if (dataFormats.Contains(format))
|
2015-12-30 00:26:22 +13:00
|
|
|
|
{
|
2016-01-19 10:52:47 +13:00
|
|
|
|
using (MemoryStream ms = dataObject.GetData(format) as MemoryStream)
|
2015-12-30 00:26:22 +13:00
|
|
|
|
{
|
2016-01-19 10:52:47 +13:00
|
|
|
|
if (ms != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-09-20 19:48:45 +12:00
|
|
|
|
return GetDIBImage(ms);
|
2016-01-19 10:52:47 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-30 00:26:22 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-30 00:58:21 +13:00
|
|
|
|
|
2016-01-13 19:54:36 +13:00
|
|
|
|
if (dataObject.GetDataPresent(DataFormats.Bitmap, true))
|
2016-01-01 20:37:04 +13:00
|
|
|
|
{
|
|
|
|
|
return dataObject.GetData(DataFormats.Bitmap, true) as Image;
|
|
|
|
|
}
|
2015-12-30 00:26:22 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 00:58:21 +13:00
|
|
|
|
return null;
|
2015-12-30 00:26:22 +13:00
|
|
|
|
}
|
2016-01-19 10:52:47 +13:00
|
|
|
|
|
2021-04-06 06:45:52 +12:00
|
|
|
|
private static Bitmap GetImageAlternative2()
|
|
|
|
|
{
|
|
|
|
|
IDataObject dataObject = Clipboard.GetDataObject();
|
|
|
|
|
|
|
|
|
|
if (dataObject != null)
|
|
|
|
|
{
|
|
|
|
|
string[] dataFormats = dataObject.GetFormats(false);
|
|
|
|
|
|
|
|
|
|
if (dataFormats.Contains(FORMAT_PNG))
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream ms = dataObject.GetData(FORMAT_PNG) as MemoryStream)
|
|
|
|
|
{
|
|
|
|
|
if (ms != null)
|
|
|
|
|
{
|
|
|
|
|
using (Bitmap bmp = new Bitmap(ms))
|
|
|
|
|
{
|
|
|
|
|
return ClipboardHelpersEx.CloneImage(bmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (dataFormats.Contains(DataFormats.Dib))
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream ms = dataObject.GetData(DataFormats.Dib) as MemoryStream)
|
|
|
|
|
{
|
|
|
|
|
if (ms != null)
|
|
|
|
|
{
|
|
|
|
|
return ClipboardHelpersEx.ImageFromClipboardDib(ms.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (dataFormats.Contains(FORMAT_17))
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream ms = dataObject.GetData(FORMAT_17) as MemoryStream)
|
|
|
|
|
{
|
|
|
|
|
if (ms != null)
|
|
|
|
|
{
|
|
|
|
|
return ClipboardHelpersEx.DIBV5ToBitmap(ms.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (dataFormats.Contains(DataFormats.Bitmap))
|
|
|
|
|
{
|
|
|
|
|
return dataObject.GetData(DataFormats.Bitmap, true) as Bitmap;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 10:52:47 +13:00
|
|
|
|
private static Image GetDIBImage(MemoryStream ms)
|
|
|
|
|
{
|
|
|
|
|
byte[] dib = ms.ToArray();
|
|
|
|
|
|
|
|
|
|
BITMAPINFOHEADER infoHeader = Helpers.ByteArrayToStructure<BITMAPINFOHEADER>(dib);
|
|
|
|
|
|
|
|
|
|
IntPtr gcHandle = IntPtr.Zero;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
GCHandle handle = GCHandle.Alloc(dib, GCHandleType.Pinned);
|
|
|
|
|
gcHandle = GCHandle.ToIntPtr(handle);
|
|
|
|
|
|
|
|
|
|
if (infoHeader.biSizeImage == 0)
|
|
|
|
|
{
|
|
|
|
|
infoHeader.biSizeImage = (uint)(infoHeader.biWidth * infoHeader.biHeight * (infoHeader.biBitCount >> 3));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 19:22:55 +13:00
|
|
|
|
using (Bitmap bmp = new Bitmap(infoHeader.biWidth, infoHeader.biHeight, -(int)(infoHeader.biSizeImage / infoHeader.biHeight),
|
2016-01-19 10:52:47 +13:00
|
|
|
|
infoHeader.biBitCount == 32 ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb,
|
2018-05-17 01:27:11 +12:00
|
|
|
|
new IntPtr((long)handle.AddrOfPinnedObject() + infoHeader.OffsetToPixels + ((infoHeader.biHeight - 1) * (int)(infoHeader.biSizeImage / infoHeader.biHeight)))))
|
2016-02-16 19:22:55 +13:00
|
|
|
|
{
|
|
|
|
|
return new Bitmap(bmp);
|
|
|
|
|
}
|
2016-01-19 10:52:47 +13:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (gcHandle != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
GCHandle.FromIntPtr(gcHandle).Free();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-13 01:03:22 +13:00
|
|
|
|
|
|
|
|
|
public static string GetText(bool checkContainsText = false)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lock (ClipboardLock)
|
|
|
|
|
{
|
|
|
|
|
if (!checkContainsText || Clipboard.ContainsText())
|
|
|
|
|
{
|
|
|
|
|
return Clipboard.GetText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard get text failed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-12-13 02:06:57 +13:00
|
|
|
|
|
|
|
|
|
public static string[] GetFileDropList(bool checkContainsFileDropList = false)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lock (ClipboardLock)
|
|
|
|
|
{
|
|
|
|
|
if (!checkContainsFileDropList || Clipboard.ContainsFileDropList())
|
|
|
|
|
{
|
|
|
|
|
return Clipboard.GetFileDropList().Cast<string>().ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard get file drop list failed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-08-17 08:45:17 +12:00
|
|
|
|
|
|
|
|
|
private static string GenerateHTMLFragment(string html)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
string header = "Version:0.9\r\nStartHTML:<<<<<<<<<1\r\nEndHTML:<<<<<<<<<2\r\nStartFragment:<<<<<<<<<3\r\nEndFragment:<<<<<<<<<4\r\n";
|
|
|
|
|
string startHTML = "<html>\r\n<body>\r\n";
|
|
|
|
|
string startFragment = "<!--StartFragment-->";
|
|
|
|
|
string endFragment = "<!--EndFragment-->";
|
|
|
|
|
string endHTML = "\r\n</body>\r\n</html>";
|
|
|
|
|
|
|
|
|
|
sb.Append(header);
|
|
|
|
|
|
|
|
|
|
int startHTMLLength = header.Length;
|
|
|
|
|
int startFragmentLength = startHTMLLength + startHTML.Length + startFragment.Length;
|
|
|
|
|
int endFragmentLength = startFragmentLength + Encoding.UTF8.GetByteCount(html);
|
|
|
|
|
int endHTMLLength = endFragmentLength + endFragment.Length + endHTML.Length;
|
|
|
|
|
|
|
|
|
|
sb.Replace("<<<<<<<<<1", startHTMLLength.ToString("D10"));
|
|
|
|
|
sb.Replace("<<<<<<<<<2", endHTMLLength.ToString("D10"));
|
|
|
|
|
sb.Replace("<<<<<<<<<3", startFragmentLength.ToString("D10"));
|
|
|
|
|
sb.Replace("<<<<<<<<<4", endFragmentLength.ToString("D10"));
|
|
|
|
|
|
|
|
|
|
sb.Append(startHTML);
|
|
|
|
|
sb.Append(startFragment);
|
|
|
|
|
sb.Append(html);
|
|
|
|
|
sb.Append(endFragment);
|
|
|
|
|
sb.Append(endHTML);
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2020-10-21 22:50:24 +13:00
|
|
|
|
|
|
|
|
|
public static bool ContainsImage()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Clipboard.ContainsImage();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool ContainsText()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Clipboard.ContainsText();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool ContainsFileDropList()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Clipboard.ContainsFileDropList();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|