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
This commit is contained in:
RKrom 2013-01-07 19:57:53 +00:00
parent 12c9e943b2
commit 6ec8ecfaa1
4 changed files with 89 additions and 90 deletions

View file

@ -407,7 +407,7 @@ namespace Greenshot.Drawing {
/// </summary> /// </summary>
/// <param name="newImage"></param> /// <param name="newImage"></param>
public Surface(Image newImage) : this() { 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); SetImage(newImage, true);
} }

View file

@ -227,12 +227,14 @@ namespace Greenshot.Helpers {
conf.CaptureDelay = 0; conf.CaptureDelay = 0;
} }
// Allways capture Mousecursor, only show when needed // Capture Mousecursor if we are not loading from file or clipboard, only show when needed
capture = WindowCapture.CaptureCursor(capture); if (captureMode != CaptureMode.File && captureMode != CaptureMode.Clipboard) {
capture.CursorVisible = false; capture = WindowCapture.CaptureCursor(capture);
// Check if needed if (captureMouseCursor) {
if (captureMouseCursor && captureMode != CaptureMode.Clipboard && captureMode != CaptureMode.File) { capture.CursorVisible = conf.CaptureMousepointer;
capture.CursorVisible = conf.CaptureMousepointer; } else {
capture.CursorVisible = false;
}
} }
switch(captureMode) { switch(captureMode) {
@ -296,22 +298,14 @@ namespace Greenshot.Helpers {
HandleCapture(); HandleCapture();
break; break;
case CaptureMode.Clipboard: case CaptureMode.Clipboard:
Image clipboardImage = null; Image clipboardImage = ClipboardHelper.GetImage();
string text = "Clipboard";
if (ClipboardHelper.ContainsImage()) {
clipboardImage = ClipboardHelper.GetImage();
}
if (clipboardImage != null) { if (clipboardImage != null) {
if (capture != null) { if (capture != null) {
capture.Image = clipboardImage; capture.Image = clipboardImage;
} else { } else {
capture = new Capture(clipboardImage); capture = new Capture(clipboardImage);
} }
string title = ClipboardHelper.GetText(); capture.CaptureDetails.Title = "Clipboard";
if (title == null || title.Trim().Length == 0) {
title = "Clipboard";
}
capture.CaptureDetails.Title = title;
capture.CaptureDetails.AddMetaData("source", "Clipboard"); capture.CaptureDetails.AddMetaData("source", "Clipboard");
// Force Editor, keep picker // Force Editor, keep picker
if (capture.CaptureDetails.HasDestination(Destinations.PickerDestination.DESIGNATION)) { if (capture.CaptureDetails.HasDestination(Destinations.PickerDestination.DESIGNATION)) {
@ -324,7 +318,7 @@ namespace Greenshot.Helpers {
} }
HandleCapture(); HandleCapture();
} else { } else {
MessageBox.Show("Couldn't create bitmap from : " + text); MessageBox.Show(Language.GetString("clipboard_noimage"));
} }
break; break;
case CaptureMode.File: case CaptureMode.File:

View file

@ -281,6 +281,7 @@ EndSelection:<<<<<<<4
// Get single image, this takes the "best" match // Get single image, this takes the "best" match
tmpImage = GetImage(dataObject); tmpImage = GetImage(dataObject);
if (tmpImage != null) { if (tmpImage != null) {
LOG.InfoFormat("Got image from clipboard with size {0} and format {1}", tmpImage.Size, tmpImage.PixelFormat);
returnImage = ImageHelper.Clone(tmpImage); returnImage = ImageHelper.Clone(tmpImage);
// Clean up. // Clean up.
tmpImage.Dispose(); tmpImage.Dispose();
@ -296,6 +297,7 @@ EndSelection:<<<<<<<4
LOG.Error("Problem retrieving Image from clipboard.", streamImageEx); LOG.Error("Problem retrieving Image from clipboard.", streamImageEx);
} }
if (returnImage != null) { if (returnImage != null) {
LOG.InfoFormat("Got image from clipboard with size {0} and format {1}", tmpImage.Size, tmpImage.PixelFormat);
yield return returnImage; yield return returnImage;
} }
} }
@ -312,89 +314,89 @@ EndSelection:<<<<<<<4
Image returnImage = null; Image returnImage = null;
if (dataObject != null) { if (dataObject != null) {
IList<string> formats = GetFormats(dataObject); IList<string> formats = GetFormats(dataObject);
try { string[] retrieveFormats = {FORMAT_PNG, FORMAT_JPG, DataFormats.Tiff, DataFormats.Dib, FORMAT_FILECONTENTS, "", FORMAT_GIF};
if (formats.Contains(FORMAT_PNG)) { foreach(string currentFormat in retrieveFormats) {
returnImage = GetImageFormat(FORMAT_PNG, dataObject); if (string.IsNullOrEmpty(currentFormat)) {
if (returnImage != null) { LOG.Info("Using default .NET Clipboard.GetImage()");
return returnImage; try {
} returnImage = Clipboard.GetImage();
} if (returnImage != null) {
if (formats.Contains(FORMAT_JPG)) { return returnImage;
returnImage = GetImageFormat(FORMAT_JPG, dataObject); } else {
if (returnImage != null) { LOG.Info("Clipboard.GetImage() didn't return an image.");
return returnImage; }
} } catch (Exception ex) {
} LOG.Error("Problem retrieving Image via Clipboard.GetImage(): ", ex);
if (formats.Contains(DataFormats.Tiff)) { }
returnImage = GetImageFormat(DataFormats.Tiff, dataObject); } 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) { if (returnImage != null) {
return returnImage; return returnImage;
} }
} else {
LOG.DebugFormat("Couldn't find format {0}.", currentFormat);
} }
}
}
return null;
}
/// <summary>
/// the DIB readed should solve the issue reported here: https://sourceforge.net/projects/greenshot/forums/forum/676083/topic/6354353/index/page/1
/// </summary>
/// <returns>Image</returns>
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<BitmapInfoHeader>(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 BitmapFileHeader fileHeader = new BitmapFileHeader();
try { fileHeader.bfType = BitmapFileHeader.BM;
// If the EnableSpecialDIBClipboardReader flag in the config is set, use the code from: fileHeader.bfSize = fileSize;
// http://www.thomaslevesque.com/2009/02/05/wpf-paste-an-image-from-the-clipboard/ fileHeader.bfReserved1 = 0;
// to read the DeviceIndependentBitmap from the clipboard, this might fix bug 3576125 fileHeader.bfReserved2 = 0;
if (config.EnableSpecialDIBClipboardReader && formats.Contains(DataFormats.Dib)) { fileHeader.bfOffBits = (int)(fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4);
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) {
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(); byte[] fileHeaderBytes = BinaryStructHelper.ToByteArray<BitmapFileHeader>(fileHeader);
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);
using (MemoryStream bitmapStream = new MemoryStream()) { bitmapStream.Write(dibBuffer, 0, dibBuffer.Length);
bitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize); bitmapStream.Seek(0, SeekOrigin.Begin);
bitmapStream.Write(dibBuffer, 0, dibBuffer.Length); using (Image tmpImage = Image.FromStream(bitmapStream)) {
bitmapStream.Seek(0, SeekOrigin.Begin); if (tmpImage != null) {
using (Image tmpImage = Image.FromStream(bitmapStream)) { return ImageHelper.Clone(tmpImage);
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; return null;
} }
@ -411,6 +413,7 @@ EndSelection:<<<<<<<4
try { try {
using (Image tmpImage = Image.FromStream(imageStream)) { using (Image tmpImage = Image.FromStream(imageStream)) {
if (tmpImage != null) { if (tmpImage != null) {
LOG.InfoFormat("Got image with clipboard format {0} from the clipboard.", format);
return ImageHelper.Clone(tmpImage); return ImageHelper.Clone(tmpImage);
} }
} }
@ -621,6 +624,7 @@ EndSelection:<<<<<<<4
formats = dataObj.GetFormats(); formats = dataObj.GetFormats();
} }
if (formats != null) { if (formats != null) {
LOG.DebugFormat("Got clipboard formats: {0}", String.Join(",", formats));
return new List<string>(formats); return new List<string>(formats);
} }
return new List<string>(); return new List<string>();
@ -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; return filenames;
} }

View file

@ -171,7 +171,7 @@ namespace GreenshotPlugin.Core {
value.Dispose(); 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 { } else {
LOG.Debug("Image is removed."); LOG.Debug("Image is removed.");
} }