From 6ec8ecfaa135fc263b9cb52ebd1bf37d77a25d87 Mon Sep 17 00:00:00 2001 From: RKrom Date: Mon, 7 Jan 2013 19:57:53 +0000 Subject: [PATCH] Changes to clipboard code: log statement changes and consolidated some code. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2414 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Drawing/Surface.cs | 2 +- Greenshot/Helpers/CaptureHelper.cs | 28 ++--- GreenshotPlugin/Core/ClipboardHelper.cs | 147 ++++++++++++------------ GreenshotPlugin/Core/WindowCapture.cs | 2 +- 4 files changed, 89 insertions(+), 90 deletions(-) diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index c1799f235..8e0c34bd2 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -407,7 +407,7 @@ namespace Greenshot.Drawing { /// /// public Surface(Image newImage) : this() { - LOG.Debug("Got image with dimensions " + newImage.Width + "," + newImage.Height + " bpp: " + newImage.PixelFormat); + LOG.DebugFormat("Got image with dimensions {0} and format {1}", newImage.Size, newImage.PixelFormat); SetImage(newImage, true); } diff --git a/Greenshot/Helpers/CaptureHelper.cs b/Greenshot/Helpers/CaptureHelper.cs index 491879bab..36acd8fea 100644 --- a/Greenshot/Helpers/CaptureHelper.cs +++ b/Greenshot/Helpers/CaptureHelper.cs @@ -227,12 +227,14 @@ namespace Greenshot.Helpers { conf.CaptureDelay = 0; } - // Allways capture Mousecursor, only show when needed - capture = WindowCapture.CaptureCursor(capture); - capture.CursorVisible = false; - // Check if needed - if (captureMouseCursor && captureMode != CaptureMode.Clipboard && captureMode != CaptureMode.File) { - capture.CursorVisible = conf.CaptureMousepointer; + // Capture Mousecursor if we are not loading from file or clipboard, only show when needed + if (captureMode != CaptureMode.File && captureMode != CaptureMode.Clipboard) { + capture = WindowCapture.CaptureCursor(capture); + if (captureMouseCursor) { + capture.CursorVisible = conf.CaptureMousepointer; + } else { + capture.CursorVisible = false; + } } switch(captureMode) { @@ -296,22 +298,14 @@ namespace Greenshot.Helpers { HandleCapture(); break; case CaptureMode.Clipboard: - Image clipboardImage = null; - string text = "Clipboard"; - if (ClipboardHelper.ContainsImage()) { - clipboardImage = ClipboardHelper.GetImage(); - } + Image clipboardImage = ClipboardHelper.GetImage(); if (clipboardImage != null) { if (capture != null) { capture.Image = clipboardImage; } else { capture = new Capture(clipboardImage); } - string title = ClipboardHelper.GetText(); - if (title == null || title.Trim().Length == 0) { - title = "Clipboard"; - } - capture.CaptureDetails.Title = title; + capture.CaptureDetails.Title = "Clipboard"; capture.CaptureDetails.AddMetaData("source", "Clipboard"); // Force Editor, keep picker if (capture.CaptureDetails.HasDestination(Destinations.PickerDestination.DESIGNATION)) { @@ -324,7 +318,7 @@ namespace Greenshot.Helpers { } HandleCapture(); } else { - MessageBox.Show("Couldn't create bitmap from : " + text); + MessageBox.Show(Language.GetString("clipboard_noimage")); } break; case CaptureMode.File: diff --git a/GreenshotPlugin/Core/ClipboardHelper.cs b/GreenshotPlugin/Core/ClipboardHelper.cs index 2d40d9cc4..a348599f6 100644 --- a/GreenshotPlugin/Core/ClipboardHelper.cs +++ b/GreenshotPlugin/Core/ClipboardHelper.cs @@ -281,6 +281,7 @@ EndSelection:<<<<<<<4 // Get single image, this takes the "best" match tmpImage = GetImage(dataObject); if (tmpImage != null) { + LOG.InfoFormat("Got image from clipboard with size {0} and format {1}", tmpImage.Size, tmpImage.PixelFormat); returnImage = ImageHelper.Clone(tmpImage); // Clean up. tmpImage.Dispose(); @@ -296,6 +297,7 @@ EndSelection:<<<<<<<4 LOG.Error("Problem retrieving Image from clipboard.", streamImageEx); } if (returnImage != null) { + LOG.InfoFormat("Got image from clipboard with size {0} and format {1}", tmpImage.Size, tmpImage.PixelFormat); yield return returnImage; } } @@ -312,89 +314,89 @@ EndSelection:<<<<<<<4 Image returnImage = null; if (dataObject != null) { IList formats = GetFormats(dataObject); - try { - if (formats.Contains(FORMAT_PNG)) { - returnImage = GetImageFormat(FORMAT_PNG, dataObject); - if (returnImage != null) { - return returnImage; - } - } - if (formats.Contains(FORMAT_JPG)) { - returnImage = GetImageFormat(FORMAT_JPG, dataObject); - if (returnImage != null) { - return returnImage; - } - } - if (formats.Contains(DataFormats.Tiff)) { - returnImage = GetImageFormat(DataFormats.Tiff, dataObject); + string[] retrieveFormats = {FORMAT_PNG, FORMAT_JPG, DataFormats.Tiff, DataFormats.Dib, FORMAT_FILECONTENTS, "", FORMAT_GIF}; + foreach(string currentFormat in retrieveFormats) { + if (string.IsNullOrEmpty(currentFormat)) { + LOG.Info("Using default .NET Clipboard.GetImage()"); + try { + returnImage = Clipboard.GetImage(); + if (returnImage != null) { + return returnImage; + } else { + LOG.Info("Clipboard.GetImage() didn't return an image."); + } + } catch (Exception ex) { + LOG.Error("Problem retrieving Image via Clipboard.GetImage(): ", ex); + } + } else if (formats.Contains(currentFormat)) { + LOG.InfoFormat("Found {0}, trying to retrieve.", currentFormat); + if (currentFormat == DataFormats.Dib) { + returnImage = GetDIBImage(dataObject); + } else { + returnImage = GetImageFormat(currentFormat, dataObject); + } if (returnImage != null) { return returnImage; } + } else { + LOG.DebugFormat("Couldn't find format {0}.", currentFormat); } + } + } + return null; + } + + /// + /// the DIB readed should solve the issue reported here: https://sourceforge.net/projects/greenshot/forums/forum/676083/topic/6354353/index/page/1 + /// + /// Image + private static Image GetDIBImage(IDataObject dataObejct) { + 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) { + MemoryStream dibStream = GetFromDataObject(dataObejct, DataFormats.Dib) as MemoryStream; + if (isValidStream(dibStream)) { + LOG.Info("Found valid DIB stream, trying to process it."); + byte[] dibBuffer = new byte[dibStream.Length]; + dibStream.Read(dibBuffer, 0, dibBuffer.Length); + 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); - // 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(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); + 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); - 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(fileHeader); - byte[] fileHeaderBytes = BinaryStructHelper.ToByteArray(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)) { - if (tmpImage != null) { - return ImageHelper.Clone(tmpImage); - } - } + 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)) { + if (tmpImage != null) { + return ImageHelper.Clone(tmpImage); } } } + } else { + LOG.InfoFormat("Skipping special DIB format reader for biCompression {0}", infoHeader.biCompression); } - } catch (Exception dibEx) { - LOG.Error("Problem retrieving DIB from clipboard.", dibEx); - } - - // 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; } + } else { + LOG.Info("Skipping special DIB format reader as it's disabled in the configuration."); } + } catch (Exception dibEx) { + LOG.Error("Problem retrieving DIB from clipboard.", dibEx); } return null; } @@ -411,6 +413,7 @@ EndSelection:<<<<<<<4 try { using (Image tmpImage = Image.FromStream(imageStream)) { if (tmpImage != null) { + LOG.InfoFormat("Got image with clipboard format {0} from the clipboard.", format); return ImageHelper.Clone(tmpImage); } } @@ -621,6 +624,7 @@ EndSelection:<<<<<<<4 formats = dataObj.GetFormats(); } if (formats != null) { + LOG.DebugFormat("Got clipboard formats: {0}", String.Join(",", formats)); return new List(formats); } return new List(); @@ -715,7 +719,8 @@ EndSelection:<<<<<<<4 } } } - } catch { + } catch (Exception ex) { + LOG.Warn("Ignoring an issue with getting the dropFilenames from the clipboard: ", ex); } return filenames; } diff --git a/GreenshotPlugin/Core/WindowCapture.cs b/GreenshotPlugin/Core/WindowCapture.cs index 33a205aef..e94adad8b 100644 --- a/GreenshotPlugin/Core/WindowCapture.cs +++ b/GreenshotPlugin/Core/WindowCapture.cs @@ -171,7 +171,7 @@ namespace GreenshotPlugin.Core { value.Dispose(); } } - LOG.Debug("Image is set with the following specifications: " + image.Width + "," + image.Height + " - " + image.PixelFormat); + LOG.DebugFormat("Image is set with the following specifications: {0} - {1}", image.Size, image.PixelFormat); } else { LOG.Debug("Image is removed."); }