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
This commit is contained in:
RKrom 2013-01-05 22:45:47 +00:00
commit 5a93c10c79
2 changed files with 59 additions and 47 deletions

View file

@ -56,7 +56,7 @@ namespace Greenshot {
private Surface surface; private Surface surface;
private GreenshotPlugin.Controls.GreenshotToolStripButton[] toolbarButtons; 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 originalBoldCheckState = false;
private bool originalItalicCheckState = false; private bool originalItalicCheckState = false;
@ -891,7 +891,7 @@ namespace Greenshot {
this.duplicateToolStripMenuItem.Enabled = actionAllowedForSelection; this.duplicateToolStripMenuItem.Enabled = actionAllowedForSelection;
// check dependencies for the Clipboard // 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.btnPaste.Enabled = hasClipboard && !controlsDisabledDueToConfirmable;
this.pasteToolStripMenuItem.Enabled = hasClipboard && !controlsDisabledDueToConfirmable; this.pasteToolStripMenuItem.Enabled = hasClipboard && !controlsDisabledDueToConfirmable;
} }

View file

@ -43,6 +43,7 @@ namespace GreenshotPlugin.Core {
private static readonly string FORMAT_FILECONTENTS = "FileContents"; private static readonly string FORMAT_FILECONTENTS = "FileContents";
private static readonly string FORMAT_JPG = "JPG"; private static readonly string FORMAT_JPG = "JPG";
private static readonly string FORMAT_PNG = "PNG"; private static readonly string FORMAT_PNG = "PNG";
private static readonly string FORMAT_GIF = "GIF";
private static IntPtr nextClipboardViewer = IntPtr.Zero; private static IntPtr nextClipboardViewer = IntPtr.Zero;
// Template for the HTML Text on the clipboard // Template for the HTML Text on the clipboard
// see: http://msdn.microsoft.com/en-us/library/ms649015%28v=vs.85%29.aspx // 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.Tiff)
|| dataObject.GetDataPresent(DataFormats.EnhancedMetafile) || dataObject.GetDataPresent(DataFormats.EnhancedMetafile)
|| dataObject.GetDataPresent(FORMAT_PNG) || dataObject.GetDataPresent(FORMAT_PNG)
|| dataObject.GetDataPresent(FORMAT_JPG)) { || dataObject.GetDataPresent(FORMAT_JPG)
|| dataObject.GetDataPresent(FORMAT_GIF)) {
return true; return true;
} }
List<string> imageFiles = GetImageFilenames(dataObject); List<string> imageFiles = GetImageFilenames(dataObject);
@ -307,45 +309,26 @@ EndSelection:<<<<<<<4
/// <param name="dataObject"></param> /// <param name="dataObject"></param>
/// <returns>Image or null</returns> /// <returns>Image or null</returns>
private static Image GetImage(IDataObject dataObject) { private static Image GetImage(IDataObject dataObject) {
Image returnImage = null;
if (dataObject != null) { if (dataObject != null) {
IList<string> formats = GetFormats(dataObject); IList<string> formats = GetFormats(dataObject);
try { try {
MemoryStream imageStream = null;
if (formats.Contains(FORMAT_PNG)) { if (formats.Contains(FORMAT_PNG)) {
imageStream = GetFromDataObject(dataObject, FORMAT_PNG) as MemoryStream; returnImage = GetImageFormat(FORMAT_PNG, dataObject);
if (isValidStream(imageStream)) { if (returnImage != null) {
try { return returnImage;
using (Image tmpImage = Image.FromStream(imageStream)) {
return ImageHelper.Clone(tmpImage);
}
} catch (Exception streamImageEx) {
LOG.Error("Problem retrieving PNG image from clipboard.", streamImageEx);
}
} }
} }
if (formats.Contains(FORMAT_JPG)) { if (formats.Contains(FORMAT_JPG)) {
imageStream = GetFromDataObject(dataObject, FORMAT_JPG) as MemoryStream; returnImage = GetImageFormat(FORMAT_JPG, dataObject);
if (isValidStream(imageStream)) { if (returnImage != null) {
try { return returnImage;
using (Image tmpImage = Image.FromStream(imageStream)) {
return ImageHelper.Clone(tmpImage);
}
} catch (Exception streamImageEx) {
LOG.Error("Problem retrieving JPG image from clipboard.", streamImageEx);
}
} }
} }
if (formats.Contains(DataFormats.Tiff)) { if (formats.Contains(DataFormats.Tiff)) {
imageStream = GetFromDataObject(dataObject, DataFormats.Tiff) as MemoryStream; returnImage = GetImageFormat(DataFormats.Tiff, dataObject);
if (isValidStream(imageStream)) { if (returnImage != null) {
try { return returnImage;
using (Image tmpImage = Image.FromStream(imageStream)) {
return ImageHelper.Clone(tmpImage);
}
} catch (Exception streamImageEx) {
LOG.Error("Problem retrieving TIFF image from clipboard.", streamImageEx);
}
} }
} }
@ -362,6 +345,7 @@ EndSelection:<<<<<<<4
BitmapInfoHeader infoHeader = BinaryStructHelper.FromByteArray<BitmapInfoHeader>(dibBuffer); BitmapInfoHeader infoHeader = BinaryStructHelper.FromByteArray<BitmapInfoHeader>(dibBuffer);
// Only use this code, when the biCommpression != 0 (BI_RGB) // Only use this code, when the biCommpression != 0 (BI_RGB)
if (infoHeader.biCompression != 0) { if (infoHeader.biCompression != 0) {
LOG.InfoFormat("Using special DIB format reader for biCompression {0}", infoHeader.biCompression);
int fileHeaderSize = Marshal.SizeOf(typeof(BitmapFileHeader)); int fileHeaderSize = Marshal.SizeOf(typeof(BitmapFileHeader));
uint infoHeaderSize = infoHeader.biSize; uint infoHeaderSize = infoHeader.biSize;
int fileSize = (int)(fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage); int fileSize = (int)(fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage);
@ -380,31 +364,59 @@ EndSelection:<<<<<<<4
bitmapStream.Write(dibBuffer, 0, dibBuffer.Length); bitmapStream.Write(dibBuffer, 0, dibBuffer.Length);
bitmapStream.Seek(0, SeekOrigin.Begin); bitmapStream.Seek(0, SeekOrigin.Begin);
using (Image tmpImage = Image.FromStream(bitmapStream)) { 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) { } catch (Exception dibEx) {
LOG.Error("Problem retrieving DIB from clipboard.", 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) { } catch (Exception ex) {
LOG.Error("Problem retrieving Image from clipboard.", 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;
}
/// <summary>
/// Helper method to try to get an image in the specified format from the dataObject
/// </summary>
/// <param name="format">string with the format</param>
/// <param name="dataObject">IDataObject</param>
/// <returns>Image or null</returns>
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; return null;
} }