mirror of
https://github.com/greenshot/greenshot
synced 2025-08-14 18:57:28 -07:00
Code quality changes for stability and reduced memory usage.
This commit is contained in:
parent
eb042dca58
commit
08216b09c0
21 changed files with 342 additions and 316 deletions
|
@ -100,18 +100,19 @@ EndSelection:<<<<<<<4
|
|||
try {
|
||||
IntPtr hWnd = User32.GetClipboardOwner();
|
||||
if (hWnd != IntPtr.Zero) {
|
||||
IntPtr pid = IntPtr.Zero;
|
||||
IntPtr tid = User32.GetWindowThreadProcessId( hWnd, out pid);
|
||||
Process me = Process.GetCurrentProcess();
|
||||
Process ownerProcess = Process.GetProcessById( pid.ToInt32() );
|
||||
// Exclude myself
|
||||
if (ownerProcess != null && me.Id != ownerProcess.Id) {
|
||||
// Get Process Name
|
||||
owner = ownerProcess.ProcessName;
|
||||
// Try to get the starting Process Filename, this might fail.
|
||||
try {
|
||||
owner = ownerProcess.Modules[0].FileName;
|
||||
} catch (Exception) {
|
||||
int pid;
|
||||
User32.GetWindowThreadProcessId( hWnd, out pid);
|
||||
using (Process me = Process.GetCurrentProcess())
|
||||
using (Process ownerProcess = Process.GetProcessById(pid)) {
|
||||
// Exclude myself
|
||||
if (ownerProcess != null && me.Id != ownerProcess.Id) {
|
||||
// Get Process Name
|
||||
owner = ownerProcess.ProcessName;
|
||||
// Try to get the starting Process Filename, this might fail.
|
||||
try {
|
||||
owner = ownerProcess.Modules[0].FileName;
|
||||
} catch (Exception) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,12 +44,12 @@ namespace GreenshotPlugin.Core {
|
|||
using (MemoryStream ms = new MemoryStream()) {
|
||||
byte[] rgbIV = Encoding.ASCII.GetBytes(RGBIV);
|
||||
byte[] key = Encoding.ASCII.GetBytes(KEY);
|
||||
CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);
|
||||
|
||||
cs.Write(clearTextBytes, 0, clearTextBytes.Length);
|
||||
|
||||
cs.Close();
|
||||
returnValue = Convert.ToBase64String(ms.ToArray());
|
||||
using (CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write)) {
|
||||
cs.Write(clearTextBytes, 0, clearTextBytes.Length);
|
||||
cs.FlushFinalBlock();
|
||||
|
||||
returnValue = Convert.ToBase64String(ms.ToArray());
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
LOG.ErrorFormat("Error encrypting, error: ", ex.Message);
|
||||
|
@ -72,14 +72,13 @@ namespace GreenshotPlugin.Core {
|
|||
|
||||
byte[] rgbIV = Encoding.ASCII.GetBytes(RGBIV);
|
||||
byte[] key = Encoding.ASCII.GetBytes(KEY);
|
||||
|
||||
using (CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(key, rgbIV), CryptoStreamMode.Write)) {
|
||||
cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);
|
||||
cs.FlushFinalBlock();
|
||||
returnValue = Encoding.ASCII.GetString(ms.ToArray());
|
||||
}
|
||||
|
||||
CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(key, rgbIV),
|
||||
CryptoStreamMode.Write);
|
||||
|
||||
cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);
|
||||
|
||||
cs.Close();
|
||||
returnValue = Encoding.ASCII.GetString(ms.ToArray());
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
LOG.ErrorFormat("Error decrypting {0}, error: ", EncryptedText, ex.Message);
|
||||
|
|
|
@ -250,21 +250,22 @@ namespace GreenshotPlugin.Core {
|
|||
processStartInfo.RedirectStandardOutput = true;
|
||||
processStartInfo.RedirectStandardError = true;
|
||||
processStartInfo.UseShellExecute = false;
|
||||
Process process = Process.Start(processStartInfo);
|
||||
if (process != null) {
|
||||
process.WaitForExit();
|
||||
if (process.ExitCode == 0) {
|
||||
if (LOG.IsDebugEnabled) {
|
||||
LOG.DebugFormat("File size after processing {0}", new FileInfo(tmpFileName).Length);
|
||||
LOG.DebugFormat("Reading back tmp file: {0}", tmpFileName);
|
||||
using (Process process = Process.Start(processStartInfo)) {
|
||||
if (process != null) {
|
||||
process.WaitForExit();
|
||||
if (process.ExitCode == 0) {
|
||||
if (LOG.IsDebugEnabled) {
|
||||
LOG.DebugFormat("File size after processing {0}", new FileInfo(tmpFileName).Length);
|
||||
LOG.DebugFormat("Reading back tmp file: {0}", tmpFileName);
|
||||
}
|
||||
byte[] processedImage = File.ReadAllBytes(tmpFileName);
|
||||
targetStream.Write(processedImage, 0, processedImage.Length);
|
||||
return true;
|
||||
}
|
||||
byte[] processedImage = File.ReadAllBytes(tmpFileName);
|
||||
targetStream.Write(processedImage, 0, processedImage.Length);
|
||||
return true;
|
||||
LOG.ErrorFormat("Error while processing PNG image: {0}", process.ExitCode);
|
||||
LOG.ErrorFormat("Output: {0}", process.StandardOutput.ReadToEnd());
|
||||
LOG.ErrorFormat("Error: {0}", process.StandardError.ReadToEnd());
|
||||
}
|
||||
LOG.ErrorFormat("Error while processing PNG image: {0}", process.ExitCode);
|
||||
LOG.ErrorFormat("Output: {0}", process.StandardOutput.ReadToEnd());
|
||||
LOG.ErrorFormat("Error: {0}", process.StandardError.ReadToEnd());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.Error("Error while processing PNG image: ", e);
|
||||
|
|
|
@ -427,14 +427,14 @@ namespace GreenshotPlugin.Core {
|
|||
foreach (LanguageFile deleteFile in deleteList) {
|
||||
currentFiles.Remove(deleteFile);
|
||||
}
|
||||
LOG.InfoFormat("Added language {0} from: {1}", languageFile.Description, languageFile.Filepath);
|
||||
LOG.InfoFormat("Added language definition {0} from: {1}", languageFile.Description, languageFile.Filepath);
|
||||
currentFiles.Add(languageFile);
|
||||
}
|
||||
} else {
|
||||
currentFiles = new List<LanguageFile>();
|
||||
currentFiles.Add(languageFile);
|
||||
languageFiles.Add(languageFile.Ietf, currentFiles);
|
||||
LOG.InfoFormat("Added language {0} from: {1}", languageFile.Description, languageFile.Filepath);
|
||||
LOG.InfoFormat("Added language definition {0} from: {1}", languageFile.Description, languageFile.Filepath);
|
||||
}
|
||||
}
|
||||
} catch (DirectoryNotFoundException) {
|
||||
|
|
|
@ -91,7 +91,7 @@ namespace GreenshotPlugin.Core {
|
|||
public WindowsEnumerator GetWindows(IntPtr hWndParent, string classname) {
|
||||
items = new List<WindowDetails>();
|
||||
List<WindowDetails> windows = new List<WindowDetails>();
|
||||
User32.EnumChildWindows(hWndParent, new EnumWindowsProc(WindowEnum), 0);
|
||||
User32.EnumChildWindows(hWndParent, new EnumWindowsProc(WindowEnum), IntPtr.Zero);
|
||||
|
||||
bool hasParent = !IntPtr.Zero.Equals(hWndParent);
|
||||
string parentText = null;
|
||||
|
@ -161,7 +161,6 @@ namespace GreenshotPlugin.Core {
|
|||
/// Provides details about a Window returned by the
|
||||
/// enumeration
|
||||
/// </summary>
|
||||
[SuppressMessage("Microsoft.Design", "CA1049:TypesThatOwnNativeResourcesShouldBeDisposable")]
|
||||
public class WindowDetails : IEquatable<WindowDetails>{
|
||||
private const string METRO_WINDOWS_CLASS = "Windows.UI.Core.CoreWindow";
|
||||
private const string METRO_APPLAUNCHER_CLASS = "ImmersiveLauncher";
|
||||
|
@ -281,7 +280,7 @@ namespace GreenshotPlugin.Core {
|
|||
return string.Empty;
|
||||
}
|
||||
// Get the process id
|
||||
IntPtr processid;
|
||||
int processid;
|
||||
User32.GetWindowThreadProcessId(Handle, out processid);
|
||||
return Kernel32.GetProcessPath(processid);
|
||||
}
|
||||
|
@ -730,10 +729,10 @@ namespace GreenshotPlugin.Core {
|
|||
return parentHandle != IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
public IntPtr ProcessId {
|
||||
|
||||
public int ProcessId {
|
||||
get {
|
||||
IntPtr processId;
|
||||
int processId;
|
||||
User32.GetWindowThreadProcessId(Handle, out processId);
|
||||
return processId;
|
||||
}
|
||||
|
@ -742,9 +741,9 @@ namespace GreenshotPlugin.Core {
|
|||
public Process Process {
|
||||
get {
|
||||
try {
|
||||
IntPtr processId;
|
||||
int processId;
|
||||
User32.GetWindowThreadProcessId(Handle, out processId);
|
||||
Process process = Process.GetProcessById(processId.ToInt32());
|
||||
Process process = Process.GetProcessById(processId);
|
||||
if (process != null) {
|
||||
return process;
|
||||
}
|
||||
|
@ -866,7 +865,7 @@ namespace GreenshotPlugin.Core {
|
|||
return (WindowStyleFlags)User32.GetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_STYLE);
|
||||
}
|
||||
set {
|
||||
User32.SetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_STYLE, (uint)value);
|
||||
User32.SetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_STYLE, new IntPtr((uint)value));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -892,7 +891,7 @@ namespace GreenshotPlugin.Core {
|
|||
return (ExtendedWindowStyleFlags)User32.GetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_EXSTYLE);
|
||||
}
|
||||
set {
|
||||
User32.SetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_EXSTYLE, (uint)value);
|
||||
User32.SetWindowLongWrapper(hWnd, (int)WindowLongIndex.GWL_EXSTYLE, new IntPtr((uint)value));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -992,9 +991,11 @@ namespace GreenshotPlugin.Core {
|
|||
// check if the capture fits
|
||||
if (!doesCaptureFit) {
|
||||
// if GDI is allowed.. (a screenshot won't be better than we comes if we continue)
|
||||
if (!isMetroApp && WindowCapture.isGDIAllowed(Process)) {
|
||||
// we return null which causes the capturing code to try another method.
|
||||
return null;
|
||||
using (Process thisWindowProcess = Process) {
|
||||
if (!isMetroApp && WindowCapture.isGDIAllowed(thisWindowProcess)) {
|
||||
// we return null which causes the capturing code to try another method.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1292,28 +1293,30 @@ namespace GreenshotPlugin.Core {
|
|||
/// Warning: Use only if no other way!!
|
||||
/// </summary>
|
||||
private bool FreezeWindow() {
|
||||
Process proc = Process.GetProcessById(ProcessId.ToInt32());
|
||||
string processName = proc.ProcessName;
|
||||
if (!CanFreezeOrUnfreeze(processName)) {
|
||||
LOG.DebugFormat("Not freezing {0}", processName);
|
||||
return false;
|
||||
}
|
||||
if (!CanFreezeOrUnfreeze(Text)) {
|
||||
LOG.DebugFormat("Not freezing {0}", processName);
|
||||
return false;
|
||||
}
|
||||
LOG.DebugFormat("Freezing process: {0}", processName);
|
||||
|
||||
bool frozen = false;
|
||||
|
||||
foreach (ProcessThread pT in proc.Threads) {
|
||||
IntPtr pOpenThread = Kernel32.OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
|
||||
|
||||
if (pOpenThread == IntPtr.Zero) {
|
||||
break;
|
||||
using (Process proc = Process.GetProcessById(ProcessId)) {
|
||||
string processName = proc.ProcessName;
|
||||
if (!CanFreezeOrUnfreeze(processName)) {
|
||||
LOG.DebugFormat("Not freezing {0}", processName);
|
||||
return false;
|
||||
}
|
||||
if (!CanFreezeOrUnfreeze(Text)) {
|
||||
LOG.DebugFormat("Not freezing {0}", processName);
|
||||
return false;
|
||||
}
|
||||
LOG.DebugFormat("Freezing process: {0}", processName);
|
||||
|
||||
|
||||
foreach (ProcessThread pT in proc.Threads) {
|
||||
IntPtr pOpenThread = Kernel32.OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
|
||||
|
||||
if (pOpenThread == IntPtr.Zero) {
|
||||
break;
|
||||
}
|
||||
frozen = true;
|
||||
Kernel32.SuspendThread(pOpenThread);
|
||||
pT.Dispose();
|
||||
}
|
||||
frozen = true;
|
||||
Kernel32.SuspendThread(pOpenThread);
|
||||
}
|
||||
return frozen;
|
||||
}
|
||||
|
@ -1322,27 +1325,27 @@ namespace GreenshotPlugin.Core {
|
|||
/// Unfreeze the process belonging to the window
|
||||
/// </summary>
|
||||
public void UnfreezeWindow() {
|
||||
Process proc = Process.GetProcessById(ProcessId.ToInt32());
|
||||
|
||||
string processName = proc.ProcessName;
|
||||
if (!CanFreezeOrUnfreeze(processName)) {
|
||||
LOG.DebugFormat("Not unfreezing {0}", processName);
|
||||
return;
|
||||
}
|
||||
if (!CanFreezeOrUnfreeze(Text)) {
|
||||
LOG.DebugFormat("Not unfreezing {0}", processName);
|
||||
return;
|
||||
}
|
||||
LOG.DebugFormat("Unfreezing process: {0}", processName);
|
||||
|
||||
foreach (ProcessThread pT in proc.Threads) {
|
||||
IntPtr pOpenThread = Kernel32.OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
|
||||
|
||||
if (pOpenThread == IntPtr.Zero) {
|
||||
break;
|
||||
using (Process proc = Process.GetProcessById(ProcessId)) {
|
||||
string processName = proc.ProcessName;
|
||||
if (!CanFreezeOrUnfreeze(processName)) {
|
||||
LOG.DebugFormat("Not unfreezing {0}", processName);
|
||||
return;
|
||||
}
|
||||
if (!CanFreezeOrUnfreeze(Text)) {
|
||||
LOG.DebugFormat("Not unfreezing {0}", processName);
|
||||
return;
|
||||
}
|
||||
LOG.DebugFormat("Unfreezing process: {0}", processName);
|
||||
|
||||
foreach (ProcessThread pT in proc.Threads) {
|
||||
IntPtr pOpenThread = Kernel32.OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pT.Id);
|
||||
|
||||
if (pOpenThread == IntPtr.Zero) {
|
||||
break;
|
||||
}
|
||||
|
||||
Kernel32.ResumeThread(pOpenThread);
|
||||
}
|
||||
|
||||
Kernel32.ResumeThread(pOpenThread);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1363,15 +1366,12 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
returnImage = new Bitmap(windowRect.Width, windowRect.Height, pixelFormat);
|
||||
using (Graphics graphics = Graphics.FromImage(returnImage)) {
|
||||
IntPtr hDCDest = graphics.GetHdc();
|
||||
try {
|
||||
bool printSucceeded = User32.PrintWindow(Handle, hDCDest, 0x0);
|
||||
using (SafeDeviceContextHandle graphicsDC = graphics.getSafeDeviceContext()) {
|
||||
bool printSucceeded = User32.PrintWindow(Handle, graphicsDC.DangerousGetHandle(), 0x0);
|
||||
if (!printSucceeded) {
|
||||
// something went wrong, most likely a "0x80004005" (Acess Denied) when using UAC
|
||||
exceptionOccured = User32.CreateWin32Exception("PrintWindow");
|
||||
}
|
||||
} finally {
|
||||
graphics.ReleaseHdc(hDCDest);
|
||||
}
|
||||
|
||||
// Apply the region "transparency"
|
||||
|
@ -1439,7 +1439,9 @@ namespace GreenshotPlugin.Core {
|
|||
get {
|
||||
try {
|
||||
if (!isMetroApp) {
|
||||
return "Greenshot".Equals(Process.MainModule.FileVersionInfo.ProductName);
|
||||
using (Process thisWindowProcess = Process) {
|
||||
return "Greenshot".Equals(thisWindowProcess.MainModule.FileVersionInfo.ProductName);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
LOG.Warn(ex);
|
||||
|
@ -1622,7 +1624,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="windowToLinkTo"></param>
|
||||
/// <returns></returns>
|
||||
public static WindowDetails GetLinkedWindow(WindowDetails windowToLinkTo) {
|
||||
IntPtr processIdSelectedWindow = windowToLinkTo.ProcessId;
|
||||
int processIdSelectedWindow = windowToLinkTo.ProcessId;
|
||||
foreach(WindowDetails window in GetAllWindows()) {
|
||||
// Ignore windows without title
|
||||
if (window.Text.Length == 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue