Refactored code to use SafeHandle where possible, this should fix potential resource leaks and make the code more clear.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2429 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-01-14 13:45:41 +00:00
parent 9288fa8212
commit 201ee7082e
7 changed files with 365 additions and 233 deletions

View file

@ -493,7 +493,7 @@ EndSelection:<<<<<<<4
/// </summary>
private const int BITMAPFILEHEADER_LENGTH = 14;
public static void SetClipboardData(ISurface surface) {
DataObject ido = new DataObject();
DataObject dataObject = new DataObject();
// This will work for Office and most other applications
//ido.SetData(DataFormats.Bitmap, true, image);
@ -515,7 +515,7 @@ EndSelection:<<<<<<<4
ImageOutput.SaveToStream(imageToSave, null, pngStream, pngOutputSettings);
pngStream.Seek(0, SeekOrigin.Begin);
// Set the PNG stream
ido.SetData(FORMAT_PNG, false, pngStream);
dataObject.SetData(FORMAT_PNG, false, pngStream);
}
} catch (Exception pngEX) {
LOG.Error("Error creating PNG for the Clipboard.", pngEX);
@ -534,7 +534,7 @@ EndSelection:<<<<<<<4
}
// Set the DIB to the clipboard DataObject
ido.SetData(DataFormats.Dib, true, dibStream);
dataObject.SetData(DataFormats.Dib, true, dibStream);
}
} catch (Exception dibEx) {
LOG.Error("Error creating DIB for the Clipboard.", dibEx);
@ -544,7 +544,7 @@ EndSelection:<<<<<<<4
if (config.ClipboardFormats.Contains(ClipboardFormat.HTML)) {
string tmpFile = ImageOutput.SaveToTmpFile(surface, new SurfaceOutputSettings(OutputFormat.png, 100, false), null);
string html = getHTMLString(surface, tmpFile);
ido.SetText(html, TextDataFormat.Html);
dataObject.SetText(html, TextDataFormat.Html);
} else if (config.ClipboardFormats.Contains(ClipboardFormat.HTMLDATAURL)) {
string html;
using (MemoryStream tmpPNGStream = new MemoryStream()) {
@ -560,18 +560,18 @@ EndSelection:<<<<<<<4
}
html = getHTMLDataURLString(surface, tmpPNGStream);
}
ido.SetText(html, TextDataFormat.Html);
dataObject.SetText(html, TextDataFormat.Html);
}
} finally {
// we need to use the SetDataOject before the streams are closed otherwise the buffer will be gone!
// Check if Bitmap is wanted
if (config.ClipboardFormats.Contains(ClipboardFormat.BITMAP)) {
ido.SetImage(imageToSave);
dataObject.SetImage(imageToSave);
// Place the DataObject to the clipboard
SetDataObject(ido, true);
SetDataObject(dataObject, true);
} else {
// Place the DataObject to the clipboard
SetDataObject(ido, true);
SetDataObject(dataObject, true);
}
if (pngStream != null) {