mirror of
https://github.com/greenshot/greenshot
synced 2025-08-20 05:23:24 -07:00
Added SafeHandles, see here: http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396335.aspx to some of the Greenshot code. Not all possible handles have been converted, this should be done to prevent more possible memory/resource leaks.
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2427 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
e07278bc04
commit
502abed36a
5 changed files with 87 additions and 42 deletions
|
@ -21,6 +21,8 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace GreenshotPlugin.UnmanagedHelpers {
|
||||
public static class GDIExtensions {
|
||||
|
@ -37,6 +39,25 @@ namespace GreenshotPlugin.UnmanagedHelpers {
|
|||
return topLeftVisible && topRightVisible && bottomLeftVisible && bottomRightVisible;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A hbitmap SafeHandle implementation
|
||||
/// </summary>
|
||||
public class SafeHBitmapHandle : SafeHandleZeroOrMinusOneIsInvalid {
|
||||
[SecurityCritical]
|
||||
private SafeHBitmapHandle(): base(true) {
|
||||
}
|
||||
|
||||
[SecurityCritical]
|
||||
public SafeHBitmapHandle(IntPtr preexistingHandle) : base(true) {
|
||||
SetHandle(preexistingHandle);
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle() {
|
||||
return GDI32.DeleteObject(handle);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// GDI32 Helpers
|
||||
/// </summary>
|
||||
|
@ -77,9 +98,11 @@ namespace GreenshotPlugin.UnmanagedHelpers {
|
|||
try {
|
||||
hDCDest = target.GetHdc();
|
||||
hDCSrc = CreateCompatibleDC(hDCDest);
|
||||
IntPtr pOrig = SelectObject(hDCSrc, sourceBitmap.GetHbitmap());
|
||||
StretchBlt(hDCDest, destination.X, destination.Y, destination.Width, destination.Height, hDCSrc, source.Left, source.Top, source.Width, source.Height, CopyPixelOperation.SourceCopy);
|
||||
IntPtr pNew = SelectObject(hDCDest, pOrig);
|
||||
using (SafeHBitmapHandle hBitmapHandle = new SafeHBitmapHandle(sourceBitmap.GetHbitmap())) {
|
||||
IntPtr pOrig = SelectObject(hDCSrc, hBitmapHandle.DangerousGetHandle());
|
||||
StretchBlt(hDCDest, destination.X, destination.Y, destination.Width, destination.Height, hDCSrc, source.Left, source.Top, source.Width, source.Height, CopyPixelOperation.SourceCopy);
|
||||
IntPtr pNew = SelectObject(hDCDest, pOrig);
|
||||
}
|
||||
} finally {
|
||||
if (hDCSrc != IntPtr.Zero) {
|
||||
DeleteDC(hDCSrc);
|
||||
|
@ -101,9 +124,11 @@ namespace GreenshotPlugin.UnmanagedHelpers {
|
|||
try {
|
||||
hDCDest = target.GetHdc();
|
||||
hDCSrc = CreateCompatibleDC(hDCDest);
|
||||
IntPtr pOrig = SelectObject(hDCSrc, sourceBitmap.GetHbitmap());
|
||||
BitBlt(hDCDest, destination.X, destination.Y, source.Width, source.Height, hDCSrc, source.Left, source.Top, CopyPixelOperation.SourceCopy);
|
||||
IntPtr pNew = SelectObject(hDCDest, pOrig);
|
||||
using (SafeHBitmapHandle hBitmapHandle = new SafeHBitmapHandle(sourceBitmap.GetHbitmap())) {
|
||||
IntPtr pOrig = SelectObject(hDCSrc, hBitmapHandle.DangerousGetHandle());
|
||||
BitBlt(hDCDest, destination.X, destination.Y, source.Width, source.Height, hDCSrc, source.Left, source.Top, CopyPixelOperation.SourceCopy);
|
||||
IntPtr pNew = SelectObject(hDCDest, pOrig);
|
||||
}
|
||||
} finally {
|
||||
if (hDCSrc != IntPtr.Zero) {
|
||||
DeleteDC(hDCSrc);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue