Fixed error handling in the ClipboardHelper.GetImage

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2389 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-12-17 13:04:10 +00:00
commit 94b6f9d115

View file

@ -230,20 +230,28 @@ EndSelection:<<<<<<<4
// Try to get the best format, currently we support PNG and "others" // Try to get the best format, currently we support PNG and "others"
IList<string> formats = GetFormats(); IList<string> formats = GetFormats();
if (formats.Contains("PNG")) { if (formats.Contains("PNG")) {
try {
Object pngObject = GetClipboardData("PNG"); Object pngObject = GetClipboardData("PNG");
if (pngObject is MemoryStream) { if (pngObject is MemoryStream) {
MemoryStream png_stream = pngObject as MemoryStream; MemoryStream png_stream = pngObject as MemoryStream;
if (png_stream != null && png_stream.Length > 0) {
using (Image tmpImage = Image.FromStream(png_stream)) { using (Image tmpImage = Image.FromStream(png_stream)) {
return ImageHelper.Clone(tmpImage); 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: // 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/ // 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 // to read the DeviceIndependentBitmap from the clipboard, this might fix bug 3576125
if (config.EnableSpecialDIBClipboardReader && formats.Contains("DeviceIndependentBitmap")) { if (config.EnableSpecialDIBClipboardReader && formats.Contains(DataFormats.Dib)) {
MemoryStream ms = GetClipboardData("DeviceIndependentBitmap") as MemoryStream; MemoryStream ms = GetClipboardData(DataFormats.Dib) as MemoryStream;
if (ms != null) { if (ms != null && ms.Length > 0) {
byte[] dibBuffer = new byte[ms.Length]; byte[] dibBuffer = new byte[ms.Length];
ms.Read(dibBuffer, 0, dibBuffer.Length); ms.Read(dibBuffer, 0, dibBuffer.Length);
BitmapInfoHeader infoHeader = BinaryStructHelper.FromByteArray<BitmapInfoHeader>(dibBuffer); BitmapInfoHeader infoHeader = BinaryStructHelper.FromByteArray<BitmapInfoHeader>(dibBuffer);
@ -271,6 +279,9 @@ EndSelection:<<<<<<<4
} }
} }
} }
} catch (Exception dibEx) {
LOG.Error("Problem retrieving DIB from clipboard.", dibEx);
}
return Clipboard.GetImage(); return Clipboard.GetImage();
} catch (Exception ee) { } catch (Exception ee) {
if (retryCount == 0) { if (retryCount == 0) {