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
|
2015-08-13 13:07:38 +12:00
|
|
|
|
Copyright (c) 2007-2015 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;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
private const int RetryTimes = 20, RetryDelay = 100;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool CopyImage(Image img)
|
|
|
|
|
{
|
|
|
|
|
if (img != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-11-11 10:07:13 +13:00
|
|
|
|
if (HelpersOptions.UseAlternativeCopyImage)
|
|
|
|
|
{
|
|
|
|
|
return CopyImageAlternative(img);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2015-11-11 10:07:13 +13:00
|
|
|
|
IDataObject data = new DataObject();
|
|
|
|
|
data.SetData(DataFormats.Bitmap, true, img);
|
|
|
|
|
return CopyData(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
IDataObject data = new DataObject();
|
|
|
|
|
data.SetData(DataFormats.Bitmap, true, bmp);
|
|
|
|
|
return CopyData(data);
|
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)
|
|
|
|
|
{
|
|
|
|
|
IDataObject data = new DataObject();
|
|
|
|
|
using (MemoryStream msPNG = new MemoryStream())
|
|
|
|
|
using (MemoryStream msBMP = new MemoryStream())
|
|
|
|
|
using (MemoryStream msDIB = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
img.Save(msPNG, ImageFormat.Png);
|
|
|
|
|
data.SetData("PNG", false, msPNG);
|
|
|
|
|
img.Save(msBMP, ImageFormat.Bmp);
|
|
|
|
|
msBMP.CopyStreamTo(msDIB, 14, (int)msBMP.Length - 14);
|
|
|
|
|
data.SetData(DataFormats.Dib, true, msDIB);
|
|
|
|
|
return CopyData(data);
|
|
|
|
|
}
|
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
|
|
|
|
|
{
|
|
|
|
|
IDataObject data = new DataObject();
|
|
|
|
|
data.SetData(DataFormats.FileDrop, true, paths);
|
|
|
|
|
return CopyData(data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard copy file failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool CopyTextFromFile(string path)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string text = File.ReadAllText(path, Encoding.UTF8);
|
|
|
|
|
return CopyText(text);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard copy text from file failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool CopyImageFromFile(string path)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-11-21 06:59:00 +13:00
|
|
|
|
using (Image img = ImageHelpers.LoadImage(path))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
return CopyImage(img);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Clipboard copy image from file failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|