From 5a93c10c79af81b69917996ed7cded27bfdf3573 Mon Sep 17 00:00:00 2001 From: RKrom Date: Sat, 5 Jan 2013 22:45:47 +0000 Subject: [PATCH] Fixed some clipboard issues, with these fixes more formats are supported and this makes Greenshot better usable. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2407 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Forms/ImageEditorForm.cs | 4 +- GreenshotPlugin/Core/ClipboardHelper.cs | 102 +++++++++++++----------- 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/Greenshot/Forms/ImageEditorForm.cs b/Greenshot/Forms/ImageEditorForm.cs index 6656820d8..983c0ecf4 100644 --- a/Greenshot/Forms/ImageEditorForm.cs +++ b/Greenshot/Forms/ImageEditorForm.cs @@ -56,7 +56,7 @@ namespace Greenshot { private Surface surface; private GreenshotPlugin.Controls.GreenshotToolStripButton[] toolbarButtons; - private static string[] SUPPORTED_CLIPBOARD_FORMATS = {typeof(string).FullName, "Text", "DeviceIndependentBitmap", "Bitmap", typeof(DrawableContainerList).FullName}; + private static string[] SUPPORTED_CLIPBOARD_FORMATS = {typeof(string).FullName, "Text", typeof(DrawableContainerList).FullName}; private bool originalBoldCheckState = false; private bool originalItalicCheckState = false; @@ -891,7 +891,7 @@ namespace Greenshot { this.duplicateToolStripMenuItem.Enabled = actionAllowedForSelection; // check dependencies for the Clipboard - bool hasClipboard = ClipboardHelper.ContainsFormat(SUPPORTED_CLIPBOARD_FORMATS); + bool hasClipboard = ClipboardHelper.ContainsFormat(SUPPORTED_CLIPBOARD_FORMATS) || ClipboardHelper.ContainsImage(); this.btnPaste.Enabled = hasClipboard && !controlsDisabledDueToConfirmable; this.pasteToolStripMenuItem.Enabled = hasClipboard && !controlsDisabledDueToConfirmable; } diff --git a/GreenshotPlugin/Core/ClipboardHelper.cs b/GreenshotPlugin/Core/ClipboardHelper.cs index 47ddf210b..2d40d9cc4 100644 --- a/GreenshotPlugin/Core/ClipboardHelper.cs +++ b/GreenshotPlugin/Core/ClipboardHelper.cs @@ -43,6 +43,7 @@ namespace GreenshotPlugin.Core { private static readonly string FORMAT_FILECONTENTS = "FileContents"; private static readonly string FORMAT_JPG = "JPG"; private static readonly string FORMAT_PNG = "PNG"; + private static readonly string FORMAT_GIF = "GIF"; private static IntPtr nextClipboardViewer = IntPtr.Zero; // Template for the HTML Text on the clipboard // see: http://msdn.microsoft.com/en-us/library/ms649015%28v=vs.85%29.aspx @@ -221,7 +222,8 @@ EndSelection:<<<<<<<4 || dataObject.GetDataPresent(DataFormats.Tiff) || dataObject.GetDataPresent(DataFormats.EnhancedMetafile) || dataObject.GetDataPresent(FORMAT_PNG) - || dataObject.GetDataPresent(FORMAT_JPG)) { + || dataObject.GetDataPresent(FORMAT_JPG) + || dataObject.GetDataPresent(FORMAT_GIF)) { return true; } List imageFiles = GetImageFilenames(dataObject); @@ -261,7 +263,7 @@ EndSelection:<<<<<<<4 IDataObject clipboardData = GetDataObject(); // Return the first image foreach (Image clipboardImage in GetImages(clipboardData)) { - return clipboardImage; + return clipboardImage; } return null; } @@ -307,45 +309,26 @@ EndSelection:<<<<<<<4 /// /// Image or null private static Image GetImage(IDataObject dataObject) { + Image returnImage = null; if (dataObject != null) { IList formats = GetFormats(dataObject); try { - MemoryStream imageStream = null; - if (formats.Contains(FORMAT_PNG)) { - imageStream = GetFromDataObject(dataObject, FORMAT_PNG) as MemoryStream; - if (isValidStream(imageStream)) { - try { - using (Image tmpImage = Image.FromStream(imageStream)) { - return ImageHelper.Clone(tmpImage); - } - } catch (Exception streamImageEx) { - LOG.Error("Problem retrieving PNG image from clipboard.", streamImageEx); - } + returnImage = GetImageFormat(FORMAT_PNG, dataObject); + if (returnImage != null) { + return returnImage; } } if (formats.Contains(FORMAT_JPG)) { - imageStream = GetFromDataObject(dataObject, FORMAT_JPG) as MemoryStream; - if (isValidStream(imageStream)) { - try { - using (Image tmpImage = Image.FromStream(imageStream)) { - return ImageHelper.Clone(tmpImage); - } - } catch (Exception streamImageEx) { - LOG.Error("Problem retrieving JPG image from clipboard.", streamImageEx); - } + returnImage = GetImageFormat(FORMAT_JPG, dataObject); + if (returnImage != null) { + return returnImage; } } if (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 TIFF image from clipboard.", streamImageEx); - } + returnImage = GetImageFormat(DataFormats.Tiff, dataObject); + if (returnImage != null) { + return returnImage; } } @@ -362,6 +345,7 @@ EndSelection:<<<<<<<4 BitmapInfoHeader infoHeader = BinaryStructHelper.FromByteArray(dibBuffer); // Only use this code, when the biCommpression != 0 (BI_RGB) if (infoHeader.biCompression != 0) { + LOG.InfoFormat("Using special DIB format reader for biCompression {0}", infoHeader.biCompression); int fileHeaderSize = Marshal.SizeOf(typeof(BitmapFileHeader)); uint infoHeaderSize = infoHeader.biSize; int fileSize = (int)(fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage); @@ -380,35 +364,63 @@ EndSelection:<<<<<<<4 bitmapStream.Write(dibBuffer, 0, dibBuffer.Length); bitmapStream.Seek(0, SeekOrigin.Begin); using (Image tmpImage = Image.FromStream(bitmapStream)) { - return ImageHelper.Clone(tmpImage); + if (tmpImage != null) { + return ImageHelper.Clone(tmpImage); + } } } } } } - - // Support for FileContents - imageStream = dataObject.GetData(FORMAT_FILECONTENTS) 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); - } - } } catch (Exception dibEx) { LOG.Error("Problem retrieving DIB from clipboard.", dibEx); } - return Clipboard.GetImage(); + + // Support for FileContents + returnImage = GetImageFormat(FORMAT_FILECONTENTS, dataObject); + if (returnImage != null) { + return returnImage; + } + + returnImage = Clipboard.GetImage(); + if (returnImage != null) { + return returnImage; + } } catch (Exception ex) { LOG.Error("Problem retrieving Image from clipboard.", ex); } + if (formats.Contains(FORMAT_GIF)) { + returnImage = GetImageFormat(FORMAT_GIF, dataObject); + if (returnImage != null) { + return returnImage; + } + } } return null; } + /// + /// Helper method to try to get an image in the specified format from the dataObject + /// + /// string with the format + /// IDataObject + /// Image or null + private static Image GetImageFormat(string format, IDataObject dataObject) { + MemoryStream imageStream = GetFromDataObject(dataObject, format) as MemoryStream; + if (isValidStream(imageStream)) { + try { + using (Image tmpImage = Image.FromStream(imageStream)) { + if (tmpImage != null) { + return ImageHelper.Clone(tmpImage); + } + } + } catch (Exception streamImageEx) { + LOG.Error(string.Format("Problem retrieving {0} from clipboard.", format), streamImageEx); + } + } + return null; + } + /// /// Wrapper for Clipboard.GetText created for Bug #3432313 ///