mirror of
https://github.com/greenshot/greenshot
synced 2025-07-30 03:30:02 -07:00
Code consolidation, trying to get all IDataObject relevant code into the ClipboardHelper. This might solve some Drag & Drop or Copy/Paste issues.
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2397 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
635ee507f8
commit
b7a604d93b
2 changed files with 317 additions and 230 deletions
|
@ -724,33 +724,19 @@ namespace Greenshot.Drawing {
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region DragDrop
|
#region DragDrop
|
||||||
private List<string> GetFilenames(DragEventArgs e) {
|
|
||||||
List<string> filenames = new List<string>();
|
|
||||||
string[] dropFileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
|
|
||||||
if (dropFileNames != null && dropFileNames.Length > 0) {
|
|
||||||
foreach(string filename in dropFileNames) {
|
|
||||||
LOG.Debug("Found filename: " + filename);
|
|
||||||
string ext=Path.GetExtension(filename).ToLower();
|
|
||||||
if ((ext==".jpg") || (ext==".jpeg") ||(ext==".tiff") || (ext==".gif") || (ext==".png") || (ext==".bmp") || (ext==".ico") ||(ext==".wmf")) {
|
|
||||||
filenames.Add(filename);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filenames;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDragEnter(object sender, DragEventArgs e) {
|
private void OnDragEnter(object sender, DragEventArgs e) {
|
||||||
if(LOG.IsDebugEnabled) {
|
if(LOG.IsDebugEnabled) {
|
||||||
LOG.Debug("DragEnter got following formats: ");
|
LOG.Debug("DragEnter got following formats: ");
|
||||||
foreach(string format in e.Data.GetFormats()) {
|
foreach(string format in ClipboardHelper.GetFormats(e.Data)) {
|
||||||
LOG.Debug(format);
|
LOG.Debug(format);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (draggingInProgress || (e.AllowedEffect & DragDropEffects.Copy) != DragDropEffects.Copy) {
|
if (draggingInProgress || (e.AllowedEffect & DragDropEffects.Copy) != DragDropEffects.Copy) {
|
||||||
e.Effect=DragDropEffects.None;
|
e.Effect=DragDropEffects.None;
|
||||||
} else {
|
} else {
|
||||||
List<string> filenames = GetFilenames(e);
|
List<string> filenames = ClipboardHelper.GetImageFilenames(e.Data);
|
||||||
if ((filenames != null && filenames.Count > 0) || e.Data.GetDataPresent("DragImageBits") || e.Data.GetDataPresent(DataFormats.Bitmap, true) || e.Data.GetDataPresent(DataFormats.EnhancedMetafile, true)) {
|
if ((filenames != null && filenames.Count > 0) || ClipboardHelper.ContainsImage(e.Data) || ClipboardHelper.ContainsFormat(e.Data, "DragImageBits")) {
|
||||||
e.Effect=DragDropEffects.Copy;
|
e.Effect=DragDropEffects.Copy;
|
||||||
} else {
|
} else {
|
||||||
e.Effect=DragDropEffects.None;
|
e.Effect=DragDropEffects.None;
|
||||||
|
@ -764,10 +750,10 @@ namespace Greenshot.Drawing {
|
||||||
/// <param name="sender"></param>
|
/// <param name="sender"></param>
|
||||||
/// <param name="e"></param>
|
/// <param name="e"></param>
|
||||||
private void OnDragDrop(object sender, DragEventArgs e) {
|
private void OnDragDrop(object sender, DragEventArgs e) {
|
||||||
List<string> filenames = GetFilenames(e);
|
List<string> filenames = ClipboardHelper.GetImageFilenames(e.Data);
|
||||||
Point mouse = this.PointToClient(new Point(e.X, e.Y));
|
Point mouse = this.PointToClient(new Point(e.X, e.Y));
|
||||||
if (e.Data.GetDataPresent("Text")) {
|
if (e.Data.GetDataPresent("Text")) {
|
||||||
string possibleUrl = (string)e.Data.GetData("Text");
|
string possibleUrl = ClipboardHelper.GetText(e.Data);
|
||||||
// Test if it's an url and try to download the image so we have it in the original form
|
// Test if it's an url and try to download the image so we have it in the original form
|
||||||
if (possibleUrl != null && possibleUrl.StartsWith("http")) {
|
if (possibleUrl != null && possibleUrl.StartsWith("http")) {
|
||||||
using (Bitmap image = NetworkHelper.DownloadImage(possibleUrl)) {
|
using (Bitmap image = NetworkHelper.DownloadImage(possibleUrl)) {
|
||||||
|
@ -1381,7 +1367,9 @@ namespace Greenshot.Drawing {
|
||||||
/// Paste all the elements that are on the clipboard
|
/// Paste all the elements that are on the clipboard
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void PasteElementFromClipboard() {
|
public void PasteElementFromClipboard() {
|
||||||
List<string> formats = ClipboardHelper.GetFormats();
|
IDataObject clipboard = ClipboardHelper.GetDataObject();
|
||||||
|
|
||||||
|
List<string> formats = ClipboardHelper.GetFormats(clipboard);
|
||||||
if (formats == null || formats.Count == 0) {
|
if (formats == null || formats.Count == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1393,7 +1381,7 @@ namespace Greenshot.Drawing {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formats.Contains(typeof(DrawableContainerList).FullName)) {
|
if (formats.Contains(typeof(DrawableContainerList).FullName)) {
|
||||||
DrawableContainerList dcs = (DrawableContainerList)ClipboardHelper.GetClipboardData(typeof(DrawableContainerList));
|
DrawableContainerList dcs = (DrawableContainerList)ClipboardHelper.GetFromDataObject(clipboard, typeof(DrawableContainerList));
|
||||||
if (dcs != null) {
|
if (dcs != null) {
|
||||||
dcs.Parent = this;
|
dcs.Parent = this;
|
||||||
dcs.MoveBy(10,10);
|
dcs.MoveBy(10,10);
|
||||||
|
@ -1402,16 +1390,16 @@ namespace Greenshot.Drawing {
|
||||||
DeselectAllElements();
|
DeselectAllElements();
|
||||||
SelectElements(dcs);
|
SelectElements(dcs);
|
||||||
}
|
}
|
||||||
} else if (ClipboardHelper.ContainsImage()) {
|
} else if (ClipboardHelper.ContainsImage(clipboard)) {
|
||||||
using (Image clipboardImage = ClipboardHelper.GetImage()) {
|
using (Image clipboardImage = ClipboardHelper.GetImage(clipboard)) {
|
||||||
if (clipboardImage != null) {
|
if (clipboardImage != null) {
|
||||||
DeselectAllElements();
|
DeselectAllElements();
|
||||||
IBitmapContainer bitmapContainer = AddBitmapContainer(clipboardImage as Bitmap, 0, 0);
|
IBitmapContainer bitmapContainer = AddBitmapContainer(clipboardImage as Bitmap, 0, 0);
|
||||||
SelectElement(bitmapContainer);
|
SelectElement(bitmapContainer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (ClipboardHelper.ContainsText()) {
|
} else if (ClipboardHelper.ContainsText(clipboard)) {
|
||||||
string text = ClipboardHelper.GetText();
|
string text = ClipboardHelper.GetText(clipboard);
|
||||||
if (text != null) {
|
if (text != null) {
|
||||||
DeselectAllElements();
|
DeselectAllElements();
|
||||||
ITextContainer textContainer = AddTextContainer(text, HorizontalAlignment.Center, VerticalAlignment.CENTER,
|
ITextContainer textContainer = AddTextContainer(text, HorizontalAlignment.Center, VerticalAlignment.CENTER,
|
||||||
|
|
|
@ -121,7 +121,7 @@ EndSelection:<<<<<<<4
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The SetDataObject that will lock/try/catch clipboard operations making it save and not show exceptions.
|
/// The SetDataObject will lock/try/catch clipboard operations making it save and not show exceptions.
|
||||||
/// The bool "copy" is used to decided if the information stays on the clipboard after exit.
|
/// The bool "copy" is used to decided if the information stays on the clipboard after exit.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ido"></param>
|
/// <param name="ido"></param>
|
||||||
|
@ -152,137 +152,16 @@ EndSelection:<<<<<<<4
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Safe wrapper for Clipboard.ContainsText
|
/// The GetDataObject will lock/try/catch clipboard operations making it save and not show exceptions.
|
||||||
/// Created for Bug #3432313
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>boolean if there is text on the clipboard</returns>
|
public static IDataObject GetDataObject() {
|
||||||
public static bool ContainsText() {
|
|
||||||
lock (clipboardLockObject) {
|
lock (clipboardLockObject) {
|
||||||
int retryCount = 2;
|
int retryCount = 2;
|
||||||
while (retryCount >= 0) {
|
while (retryCount >= 0) {
|
||||||
try {
|
try {
|
||||||
return Clipboard.ContainsText();
|
return Clipboard.GetDataObject();
|
||||||
} catch (Exception ee) {
|
|
||||||
if (retryCount == 0) {
|
|
||||||
string messageText = null;
|
|
||||||
string clipboardOwner = GetClipboardOwner();
|
|
||||||
if (clipboardOwner != null) {
|
|
||||||
messageText = Language.GetFormattedString("clipboard_inuse", clipboardOwner);
|
|
||||||
} else {
|
|
||||||
messageText = Language.GetString("clipboard_error");
|
|
||||||
}
|
|
||||||
LOG.Error(messageText, ee);
|
|
||||||
} else {
|
|
||||||
Thread.Sleep(100);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
--retryCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Safe wrapper for Clipboard.ContainsImage
|
|
||||||
/// Created for Bug #3432313
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>boolean if there is an image on the clipboard</returns>
|
|
||||||
public static bool ContainsImage() {
|
|
||||||
lock (clipboardLockObject) {
|
|
||||||
int retryCount = 2;
|
|
||||||
while (retryCount >= 0) {
|
|
||||||
try {
|
|
||||||
return Clipboard.ContainsImage();
|
|
||||||
} catch (Exception ee) {
|
|
||||||
if (retryCount == 0) {
|
|
||||||
string messageText = null;
|
|
||||||
string clipboardOwner = GetClipboardOwner();
|
|
||||||
if (clipboardOwner != null) {
|
|
||||||
messageText = Language.GetFormattedString("clipboard_inuse", clipboardOwner);
|
|
||||||
} else {
|
|
||||||
messageText = Language.GetString("clipboard_error");
|
|
||||||
}
|
|
||||||
LOG.Error(messageText, ee);
|
|
||||||
} else {
|
|
||||||
Thread.Sleep(100);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
--retryCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Safe wrapper for Clipboard.GetImage
|
|
||||||
/// Created for Bug #3432313
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>Image if there is an image on the clipboard</returns>
|
|
||||||
public static Image GetImage() {
|
|
||||||
lock (clipboardLockObject) {
|
|
||||||
int retryCount = 2;
|
|
||||||
while (retryCount >= 0) {
|
|
||||||
try {
|
|
||||||
// Try to get the best format, currently we support PNG and "others"
|
|
||||||
IList<string> formats = GetFormats();
|
|
||||||
if (formats.Contains("PNG")) {
|
|
||||||
try {
|
|
||||||
Object pngObject = GetClipboardData("PNG");
|
|
||||||
if (pngObject is MemoryStream) {
|
|
||||||
MemoryStream png_stream = pngObject as MemoryStream;
|
|
||||||
if (png_stream != null && png_stream.Length > 0) {
|
|
||||||
using (Image tmpImage = Image.FromStream(png_stream)) {
|
|
||||||
return ImageHelper.Clone(tmpImage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception pngEx) {
|
|
||||||
LOG.Error("Problem retrieving PNG from clipboard.", pngEx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// the DIB readed should solve the issue reported here: https://sourceforge.net/projects/greenshot/forums/forum/676083/topic/6354353/index/page/1
|
|
||||||
try {
|
|
||||||
// If the EnableSpecialDIBClipboardReader flag in the config is set, use the code from:
|
|
||||||
// http://www.thomaslevesque.com/2009/02/05/wpf-paste-an-image-from-the-clipboard/
|
|
||||||
// to read the DeviceIndependentBitmap from the clipboard, this might fix bug 3576125
|
|
||||||
if (config.EnableSpecialDIBClipboardReader && formats.Contains(DataFormats.Dib)) {
|
|
||||||
MemoryStream ms = GetClipboardData(DataFormats.Dib) as MemoryStream;
|
|
||||||
if (ms != null && ms.Length > 0) {
|
|
||||||
byte[] dibBuffer = new byte[ms.Length];
|
|
||||||
ms.Read(dibBuffer, 0, dibBuffer.Length);
|
|
||||||
BitmapInfoHeader infoHeader = BinaryStructHelper.FromByteArray<BitmapInfoHeader>(dibBuffer);
|
|
||||||
// Only use this code, when the biCommpression != 0 (BI_RGB)
|
|
||||||
if (infoHeader.biCompression != 0) {
|
|
||||||
int fileHeaderSize = Marshal.SizeOf(typeof(BitmapFileHeader));
|
|
||||||
uint infoHeaderSize = infoHeader.biSize;
|
|
||||||
int fileSize = (int)(fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage);
|
|
||||||
|
|
||||||
BitmapFileHeader fileHeader = new BitmapFileHeader();
|
|
||||||
fileHeader.bfType = BitmapFileHeader.BM;
|
|
||||||
fileHeader.bfSize = fileSize;
|
|
||||||
fileHeader.bfReserved1 = 0;
|
|
||||||
fileHeader.bfReserved2 = 0;
|
|
||||||
fileHeader.bfOffBits = (int)(fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4);
|
|
||||||
|
|
||||||
byte[] fileHeaderBytes = BinaryStructHelper.ToByteArray<BitmapFileHeader>(fileHeader);
|
|
||||||
|
|
||||||
using (MemoryStream msBitmap = new MemoryStream()) {
|
|
||||||
msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
|
|
||||||
msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
|
|
||||||
msBitmap.Seek(0, SeekOrigin.Begin);
|
|
||||||
return Image.FromStream(msBitmap);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception dibEx) {
|
|
||||||
LOG.Error("Problem retrieving DIB from clipboard.", dibEx);
|
|
||||||
}
|
|
||||||
return Clipboard.GetImage();
|
|
||||||
} catch (Exception ee) {
|
} catch (Exception ee) {
|
||||||
if (retryCount == 0) {
|
if (retryCount == 0) {
|
||||||
string messageText = null;
|
string messageText = null;
|
||||||
|
@ -305,33 +184,181 @@ EndSelection:<<<<<<<4
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Safe wrapper for Clipboard.GetText
|
/// Wrapper for Clipboard.ContainsText, Created for Bug #3432313
|
||||||
/// Created for Bug #3432313
|
/// </summary>
|
||||||
|
/// <returns>boolean if there is text on the clipboard</returns>
|
||||||
|
public static bool ContainsText() {
|
||||||
|
IDataObject clipboardData = GetDataObject();
|
||||||
|
return ContainsText(clipboardData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test if the IDataObject contains Text
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataObject"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool ContainsText(IDataObject dataObject) {
|
||||||
|
if (dataObject != null) {
|
||||||
|
if (dataObject.GetDataPresent(DataFormats.Text) || dataObject.GetDataPresent(DataFormats.UnicodeText)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Wrapper for Clipboard.ContainsImage, specialized for Greenshot, Created for Bug #3432313
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>boolean if there is an image on the clipboard</returns>
|
||||||
|
public static bool ContainsImage() {
|
||||||
|
IDataObject clipboardData = GetDataObject();
|
||||||
|
return ContainsImage(clipboardData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check if the IDataObject has an image
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataObject"></param>
|
||||||
|
/// <returns>true if an image is there</returns>
|
||||||
|
public static bool ContainsImage(IDataObject dataObject) {
|
||||||
|
if (dataObject != null) {
|
||||||
|
if (dataObject.GetDataPresent(DataFormats.Bitmap)
|
||||||
|
|| dataObject.GetDataPresent(DataFormats.Dib)
|
||||||
|
|| dataObject.GetDataPresent(DataFormats.Tiff)
|
||||||
|
|| dataObject.GetDataPresent(DataFormats.EnhancedMetafile)
|
||||||
|
|| dataObject.GetDataPresent("PNG")
|
||||||
|
|| dataObject.GetDataPresent("JPG")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
List<string> imageFiles = GetImageFilenames(dataObject);
|
||||||
|
if (imageFiles != null && imageFiles.Count > 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Simple helper to check the stream
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="memoryStream"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static bool isValidStream(MemoryStream memoryStream) {
|
||||||
|
return memoryStream != null && memoryStream.Length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Wrapper for Clipboard.GetImage, Created for Bug #3432313
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Image if there is an image on the clipboard</returns>
|
||||||
|
public static Image GetImage() {
|
||||||
|
IDataObject clipboardData = GetDataObject();
|
||||||
|
return GetImage(clipboardData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get an Image from the IDataObject
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataObject"></param>
|
||||||
|
/// <returns>Image or null</returns>
|
||||||
|
public static Image GetImage(IDataObject dataObject) {
|
||||||
|
if (dataObject != null) {
|
||||||
|
IList<string> formats = GetFormats(dataObject);
|
||||||
|
try {
|
||||||
|
MemoryStream imageStream = null;
|
||||||
|
|
||||||
|
if (!isValidStream(imageStream) && formats.Contains("PNG")) {
|
||||||
|
imageStream = GetFromDataObject(dataObject, "PNG") as MemoryStream;
|
||||||
|
}
|
||||||
|
if (!isValidStream(imageStream) && formats.Contains("JPG")) {
|
||||||
|
imageStream = GetFromDataObject(dataObject, "JPG") as MemoryStream;
|
||||||
|
}
|
||||||
|
if (!isValidStream(imageStream) && formats.Contains(DataFormats.Tiff)) {
|
||||||
|
imageStream = GetFromDataObject(dataObject, DataFormats.Tiff) as MemoryStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isValidStream(imageStream)) {
|
||||||
|
try {
|
||||||
|
using (Image tmpImage = Image.FromStream(imageStream)) {
|
||||||
|
return ImageHelper.Clone(tmpImage);
|
||||||
|
}
|
||||||
|
} catch (Exception streamImageEx) {
|
||||||
|
LOG.Error("Problem retrieving Image from clipboard.", streamImageEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the DIB readed should solve the issue reported here: https://sourceforge.net/projects/greenshot/forums/forum/676083/topic/6354353/index/page/1
|
||||||
|
try {
|
||||||
|
// If the EnableSpecialDIBClipboardReader flag in the config is set, use the code from:
|
||||||
|
// http://www.thomaslevesque.com/2009/02/05/wpf-paste-an-image-from-the-clipboard/
|
||||||
|
// to read the DeviceIndependentBitmap from the clipboard, this might fix bug 3576125
|
||||||
|
if (config.EnableSpecialDIBClipboardReader && formats.Contains(DataFormats.Dib)) {
|
||||||
|
MemoryStream dibStream = GetClipboardData(DataFormats.Dib) as MemoryStream;
|
||||||
|
if (isValidStream(dibStream)) {
|
||||||
|
byte[] dibBuffer = new byte[dibStream.Length];
|
||||||
|
dibStream.Read(dibBuffer, 0, dibBuffer.Length);
|
||||||
|
BitmapInfoHeader infoHeader = BinaryStructHelper.FromByteArray<BitmapInfoHeader>(dibBuffer);
|
||||||
|
// Only use this code, when the biCommpression != 0 (BI_RGB)
|
||||||
|
if (infoHeader.biCompression != 0) {
|
||||||
|
int fileHeaderSize = Marshal.SizeOf(typeof(BitmapFileHeader));
|
||||||
|
uint infoHeaderSize = infoHeader.biSize;
|
||||||
|
int fileSize = (int)(fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage);
|
||||||
|
|
||||||
|
BitmapFileHeader fileHeader = new BitmapFileHeader();
|
||||||
|
fileHeader.bfType = BitmapFileHeader.BM;
|
||||||
|
fileHeader.bfSize = fileSize;
|
||||||
|
fileHeader.bfReserved1 = 0;
|
||||||
|
fileHeader.bfReserved2 = 0;
|
||||||
|
fileHeader.bfOffBits = (int)(fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4);
|
||||||
|
|
||||||
|
byte[] fileHeaderBytes = BinaryStructHelper.ToByteArray<BitmapFileHeader>(fileHeader);
|
||||||
|
|
||||||
|
using (MemoryStream bitmapStream = new MemoryStream()) {
|
||||||
|
bitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize);
|
||||||
|
bitmapStream.Write(dibBuffer, 0, dibBuffer.Length);
|
||||||
|
bitmapStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
using (Image tmpImage = Image.FromStream(bitmapStream)) {
|
||||||
|
return ImageHelper.Clone(tmpImage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
List<string> imageFiles = GetImageFilenames(dataObject);
|
||||||
|
if (imageFiles != null) {
|
||||||
|
foreach (string imageFile in imageFiles) {
|
||||||
|
if (File.Exists(imageFile)) {
|
||||||
|
using (Stream imageFileStream = File.OpenRead(imageFile)) {
|
||||||
|
return Image.FromStream(imageFileStream, true, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception dibEx) {
|
||||||
|
LOG.Error("Problem retrieving DIB from clipboard.", dibEx);
|
||||||
|
}
|
||||||
|
return Clipboard.GetImage();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
LOG.Error("Problem retrieving Image from clipboard.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Wrapper for Clipboard.GetText created for Bug #3432313
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>string if there is text on the clipboard</returns>
|
/// <returns>string if there is text on the clipboard</returns>
|
||||||
public static string GetText() {
|
public static string GetText() {
|
||||||
lock (clipboardLockObject) {
|
return GetText(GetDataObject());
|
||||||
int retryCount = 2;
|
}
|
||||||
while (retryCount >= 0) {
|
|
||||||
try {
|
/// <summary>
|
||||||
return Clipboard.GetText();
|
/// Get Text from the DataObject
|
||||||
} catch (Exception ee) {
|
/// </summary>
|
||||||
if (retryCount == 0) {
|
/// <returns>string if there is text on the clipboard</returns>
|
||||||
string messageText = null;
|
public static string GetText(IDataObject dataObject) {
|
||||||
string clipboardOwner = GetClipboardOwner();
|
if (ContainsText(dataObject)) {
|
||||||
if (clipboardOwner != null) {
|
return (String)dataObject.GetData(DataFormats.Text);
|
||||||
messageText = Language.GetFormattedString("clipboard_inuse", clipboardOwner);
|
|
||||||
} else {
|
|
||||||
messageText = Language.GetString("clipboard_error");
|
|
||||||
}
|
|
||||||
LOG.Error(messageText, ee);
|
|
||||||
} else {
|
|
||||||
Thread.Sleep(100);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
--retryCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -390,34 +417,43 @@ EndSelection:<<<<<<<4
|
||||||
|
|
||||||
// This will work for Office and most other applications
|
// This will work for Office and most other applications
|
||||||
//ido.SetData(DataFormats.Bitmap, true, image);
|
//ido.SetData(DataFormats.Bitmap, true, image);
|
||||||
|
|
||||||
MemoryStream bmpStream = null;
|
MemoryStream dibStream = null;
|
||||||
MemoryStream imageStream = null;
|
|
||||||
MemoryStream pngStream = null;
|
MemoryStream pngStream = null;
|
||||||
try {
|
try {
|
||||||
// Create PNG stream
|
try {
|
||||||
if (config.ClipboardFormats.Contains(ClipboardFormat.PNG)) {
|
// Create PNG stream
|
||||||
pngStream = new MemoryStream();
|
if (config.ClipboardFormats.Contains(ClipboardFormat.PNG)) {
|
||||||
// PNG works for e.g. Powerpoint
|
pngStream = new MemoryStream();
|
||||||
SurfaceOutputSettings pngOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);
|
// PNG works for e.g. Powerpoint
|
||||||
ImageOutput.SaveToStream(surface, pngStream, pngOutputSettings);
|
SurfaceOutputSettings pngOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);
|
||||||
pngStream.Seek(0, SeekOrigin.Begin);
|
ImageOutput.SaveToStream(surface, pngStream, pngOutputSettings);
|
||||||
// Set the PNG stream
|
pngStream.Seek(0, SeekOrigin.Begin);
|
||||||
ido.SetData("PNG", false, pngStream);
|
// Set the PNG stream
|
||||||
|
ido.SetData("PNG", false, pngStream);
|
||||||
|
}
|
||||||
|
} catch (Exception pngEX) {
|
||||||
|
LOG.Error("Error creating PNG for the Clipboard.", pngEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.ClipboardFormats.Contains(ClipboardFormat.DIB)) {
|
|
||||||
bmpStream = new MemoryStream();
|
|
||||||
// Save image as BMP
|
|
||||||
SurfaceOutputSettings bmpOutputSettings = new SurfaceOutputSettings(OutputFormat.bmp, 100, false);
|
|
||||||
ImageOutput.SaveToStream(surface, bmpStream, bmpOutputSettings);
|
|
||||||
|
|
||||||
imageStream = new MemoryStream();
|
try {
|
||||||
// Copy the source, but skip the "BITMAPFILEHEADER" which has a size of 14
|
if (config.ClipboardFormats.Contains(ClipboardFormat.DIB)) {
|
||||||
imageStream.Write(bmpStream.GetBuffer(), BITMAPFILEHEADER_LENGTH, (int) bmpStream.Length - BITMAPFILEHEADER_LENGTH);
|
using (MemoryStream tmpBmpStream = new MemoryStream()) {
|
||||||
|
// Save image as BMP
|
||||||
|
SurfaceOutputSettings bmpOutputSettings = new SurfaceOutputSettings(OutputFormat.bmp, 100, false);
|
||||||
|
ImageOutput.SaveToStream(surface, tmpBmpStream, bmpOutputSettings);
|
||||||
|
|
||||||
// Set the DIB to the clipboard DataObject
|
dibStream = new MemoryStream();
|
||||||
ido.SetData(DataFormats.Dib, true, imageStream);
|
// Copy the source, but skip the "BITMAPFILEHEADER" which has a size of 14
|
||||||
|
dibStream.Write(tmpBmpStream.GetBuffer(), BITMAPFILEHEADER_LENGTH, (int)tmpBmpStream.Length - BITMAPFILEHEADER_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the DIB to the clipboard DataObject
|
||||||
|
ido.SetData(DataFormats.Dib, true, dibStream);
|
||||||
|
}
|
||||||
|
} catch (Exception dibEx) {
|
||||||
|
LOG.Error("Error creating DIB for the Clipboard.", dibEx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the HTML
|
// Set the HTML
|
||||||
|
@ -447,14 +483,9 @@ EndSelection:<<<<<<<4
|
||||||
pngStream = null;
|
pngStream = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bmpStream != null) {
|
if (dibStream != null) {
|
||||||
bmpStream.Dispose();
|
dibStream.Dispose();
|
||||||
bmpStream = null;
|
dibStream = null;
|
||||||
}
|
|
||||||
|
|
||||||
if (imageStream != null) {
|
|
||||||
imageStream.Dispose();
|
|
||||||
imageStream = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -479,33 +510,63 @@ EndSelection:<<<<<<<4
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>List<string> with the current formats</returns>
|
/// <returns>List<string> with the current formats</returns>
|
||||||
public static List<string> GetFormats() {
|
public static List<string> GetFormats() {
|
||||||
|
return GetFormats(GetDataObject());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a list of all formats currently in the IDataObject
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>List<string> with the current formats</returns>
|
||||||
|
public static List<string> GetFormats(IDataObject dataObj) {
|
||||||
string[] formats = null;
|
string[] formats = null;
|
||||||
|
|
||||||
lock (clipboardLockObject) {
|
if (dataObj != null) {
|
||||||
try {
|
formats = dataObj.GetFormats();
|
||||||
IDataObject dataObj = Clipboard.GetDataObject();
|
|
||||||
if (dataObj != null) {
|
|
||||||
formats = dataObj.GetFormats();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.Error("Error in GetFormats.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (formats != null) {
|
if (formats != null) {
|
||||||
return new List<string>(formats);
|
return new List<string>(formats);
|
||||||
}
|
}
|
||||||
return new List<string>();
|
return new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check if there is currently something in the dataObject which has the supplied format
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataObject">IDataObject</param>
|
||||||
|
/// <param name="format">string with format</param>
|
||||||
|
/// <returns>true if one the format is found</returns>
|
||||||
|
public static bool ContainsFormat(string format) {
|
||||||
|
return ContainsFormat(GetDataObject(), new string[]{format});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check if there is currently something on the clipboard which has the supplied format
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="format">string with format</param>
|
||||||
|
/// <returns>true if one the format is found</returns>
|
||||||
|
public static bool ContainsFormat(IDataObject dataObject, string format) {
|
||||||
|
return ContainsFormat(dataObject, new string[] { format });
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check if there is currently something on the clipboard which has one of the supplied formats
|
/// Check if there is currently something on the clipboard which has one of the supplied formats
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="formats">string[] with formats</param>
|
/// <param name="formats">string[] with formats</param>
|
||||||
/// <returns>true if one of the formats was found</returns>
|
/// <returns>true if one of the formats was found</returns>
|
||||||
public static bool ContainsFormat(string[] formats) {
|
public static bool ContainsFormat(string[] formats) {
|
||||||
|
return ContainsFormat(GetDataObject(), formats);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check if there is currently something on the clipboard which has one of the supplied formats
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataObject">IDataObject</param>
|
||||||
|
/// <param name="formats">string[] with formats</param>
|
||||||
|
/// <returns>true if one of the formats was found</returns>
|
||||||
|
public static bool ContainsFormat(IDataObject dataObject, string[] formats) {
|
||||||
bool formatFound = false;
|
bool formatFound = false;
|
||||||
List<string> currentFormats = GetFormats();
|
List<string> currentFormats = GetFormats(dataObject);
|
||||||
if (currentFormats == null || currentFormats.Count == 0 ||formats == null || formats.Length == 0) {
|
if (currentFormats == null || currentFormats.Count == 0 || formats == null || formats.Length == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
foreach (string format in formats) {
|
foreach (string format in formats) {
|
||||||
|
@ -527,25 +588,63 @@ EndSelection:<<<<<<<4
|
||||||
return GetClipboardData(format);
|
return GetClipboardData(format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get Object for format from IDataObject
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataObj">IDataObject</param>
|
||||||
|
/// <param name="type">Type to get</param>
|
||||||
|
/// <returns>object from IDataObject</returns>
|
||||||
|
public static Object GetFromDataObject(IDataObject dataObj, Type type) {
|
||||||
|
if (type != null) {
|
||||||
|
return GetFromDataObject(dataObj, type.FullName);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get ImageFilenames from the IDataObject
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataObject">IDataObject</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static List<string> GetImageFilenames(IDataObject dataObject) {
|
||||||
|
List<string> filenames = new List<string>();
|
||||||
|
string[] dropFileNames = (string[])dataObject.GetData(DataFormats.FileDrop);
|
||||||
|
if (dropFileNames != null && dropFileNames.Length > 0) {
|
||||||
|
foreach (string filename in dropFileNames) {
|
||||||
|
LOG.Debug("Found filename: " + filename);
|
||||||
|
string ext = Path.GetExtension(filename).ToLower();
|
||||||
|
if ((ext == ".jpg") || (ext == ".jpeg") || (ext == ".tiff") || (ext == ".gif") || (ext == ".png") || (ext == ".bmp") || (ext == ".ico") || (ext == ".wmf")) {
|
||||||
|
filenames.Add(filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filenames;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get Object for format from IDataObject
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataObj">IDataObject</param>
|
||||||
|
/// <param name="format">format to get</param>
|
||||||
|
/// <returns>object from IDataObject</returns>
|
||||||
|
public static Object GetFromDataObject(IDataObject dataObj, string format) {
|
||||||
|
if (dataObj != null) {
|
||||||
|
try {
|
||||||
|
return dataObj.GetData(format);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.Error("Error in GetClipboardData.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get Object for format from the clipboard
|
/// Get Object for format from the clipboard
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="format">format to get</param>
|
/// <param name="format">format to get</param>
|
||||||
/// <returns>object from clipboard</returns>
|
/// <returns>object from clipboard</returns>
|
||||||
public static Object GetClipboardData(string format) {
|
public static Object GetClipboardData(string format) {
|
||||||
Object obj = null;
|
return GetFromDataObject(GetDataObject(), format);
|
||||||
|
|
||||||
lock (clipboardLockObject) {
|
|
||||||
try {
|
|
||||||
IDataObject dataObj = Clipboard.GetDataObject();
|
|
||||||
if(dataObj != null && dataObj.GetDataPresent(format)) {
|
|
||||||
obj = dataObj.GetData(format);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.Error("Error in GetClipboardData.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue