Skip to content

Commit

Permalink
2.3.3.0 - Application Focus moved from Screen to Setup. Fixed Applica…
Browse files Browse the repository at this point in the history
…tion Focus bug with Active Window Title. Renamed user setting keys. New method for capturing device display resolution.
  • Loading branch information
gavinkendall committed Nov 1, 2020
1 parent 599d3d9 commit 49f32fe
Show file tree
Hide file tree
Showing 29 changed files with 681 additions and 610 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.3.2.9")]
[assembly: AssemblyFileVersion("2.3.2.9")]
[assembly: AssemblyVersion("2.3.3.0")]
[assembly: AssemblyFileVersion("2.3.3.0")]
[assembly: NeutralResourcesLanguageAttribute("en-CA")]
233 changes: 163 additions & 70 deletions ScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace AutoScreenCapture
{
Expand Down Expand Up @@ -110,6 +111,71 @@ internal struct WINDOWPLACEMENT
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

public const int ENUM_CURRENT_SETTINGS = -1;

[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public ScreenOrientation dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}

/// <summary>
/// A struct containing a Windows Forms screen object, display device width, and display device height.
/// </summary>
public struct DeviceResolution
{
/// <summary>
/// A Windows Forms screen object.
/// </summary>
public System.Windows.Forms.Screen screen;

/// <summary>
/// The width of the display device.
/// </summary>
public int width;

/// <summary>
/// The height of the display device.
/// </summary>
public int height;
}

/// <summary>
/// The minimum capture limit.
/// </summary>
Expand Down Expand Up @@ -220,6 +286,86 @@ internal struct WINDOWPLACEMENT
/// </summary>
public bool CaptureNow { get; set; }

private ImageCodecInfo GetEncoderInfo(string mimeType)
{
var encoders = ImageCodecInfo.GetImageEncoders();
return encoders.FirstOrDefault(t => t.MimeType == mimeType);
}

private static string GetMD5Hash(Bitmap bitmap, ImageFormat format)
{
byte[] bytes = null;

using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, format.Format);
bytes = ms.ToArray();
}

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

byte[] hash = md5.ComputeHash(bytes);

StringBuilder sb = new StringBuilder();

foreach (byte b in hash)
{
sb.Append(b.ToString("x2").ToLower());
}

return sb.ToString();
}

private void SaveToFile(string path, ImageFormat format, int jpegQuality, Bitmap bitmap)
{
try
{
if (bitmap != null && format != null && !string.IsNullOrEmpty(path))
{
if (format.Name.Equals(ImageFormatSpec.NAME_JPEG))
{
var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, jpegQuality);

var encoderInfo = GetEncoderInfo("image/jpeg");

bitmap.Save(path, encoderInfo, encoderParams);
}
else
{
bitmap.Save(path, format.Format);
}

Log.WriteMessage("Screenshot saved to file at path \"" + path + "\"");
}
}
catch (Exception ex)
{
Log.WriteExceptionMessage("ScreenCapture::SaveToFile", ex);
}
}

/// <summary>
/// Gets the resolution of the display device associated with the screen.
/// </summary>
/// <param name="screen">The Windows Forms screen object associated with the display device.</param>
/// <returns>A struct having the screen, display device width, and display device height.</returns>
public static DeviceResolution GetDeviceResolution(System.Windows.Forms.Screen screen)
{
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
EnumDisplaySettings(screen.DeviceName, ENUM_CURRENT_SETTINGS, ref dm);

DeviceResolution deviceResolution = new DeviceResolution()
{
screen = screen,
width = dm.dmPelsWidth,
height = dm.dmPelsHeight
};

return deviceResolution;
}

/// <summary>
/// Gets the screenshot image from an image file. This is used when we want to show screenshot images.
/// </summary>
Expand Down Expand Up @@ -507,7 +653,7 @@ public bool GetScreenImages(int component, int x, int y, int width, int height,
/// <param name="screenshotCollection">A collection of screenshot objects.</param>
/// <returns>A boolean to determine if we successfully saved the screenshot.</returns>
public bool SaveScreenshot(string path, ImageFormat format, int component, ScreenshotType screenshotType, int jpegQuality,
Guid viewId, Bitmap bitmap, string label, string windowTitle, string processName, ScreenshotCollection screenshotCollection, string applicationFocus)
Guid viewId, Bitmap bitmap, string label, string windowTitle, string processName, ScreenshotCollection screenshotCollection)
{
try
{
Expand All @@ -522,26 +668,6 @@ public bool SaveScreenshot(string path, ImageFormat format, int component, Scree
path = path.Substring(0, filepathLengthLimit);
}

if (!string.IsNullOrEmpty(applicationFocus))
{
Process[] process = Process.GetProcessesByName(applicationFocus);

foreach (var item in process)
{
var proc = Process.GetProcessById(item.Id);

IntPtr handle = proc.MainWindowHandle;
SetForegroundWindow(handle);

var placement = GetPlacement(proc.MainWindowHandle);

if (placement.showCmd == ShowWindowCommands.Minimized)
{
ShowWindowAsync(proc.MainWindowHandle, (int)ShowWindowCommands.Normal);
}
}
}

Log.WriteMessage("Attempting to write image to file at path \"" + path + "\"");

// This is a normal path used in Windows (such as "C:\screenshots\").
Expand Down Expand Up @@ -701,63 +827,30 @@ public bool SaveScreenshot(string path, ImageFormat format, int component, Scree
}
}

private void SaveToFile(string path, ImageFormat format, int jpegQuality, Bitmap bitmap)
{
try
{
if (bitmap != null && format != null && !string.IsNullOrEmpty(path))
{
if (format.Name.Equals(ImageFormatSpec.NAME_JPEG))
{
var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, jpegQuality);

var encoderInfo = GetEncoderInfo("image/jpeg");

bitmap.Save(path, encoderInfo, encoderParams);
}
else
{
bitmap.Save(path, format.Format);
}

Log.WriteMessage("Screenshot saved to file at path \"" + path + "\"");
}
}
catch (Exception ex)
{
Log.WriteExceptionMessage("ScreenCapture::SaveToFile", ex);
}
}

private ImageCodecInfo GetEncoderInfo(string mimeType)
/// <summary>
/// Sets the application focus to the defined application using a given process name.
/// </summary>
/// <param name="applicationFocus">The name of the process of the application to focus.</param>
public static void SetApplicationFocus(string applicationFocus)
{
var encoders = ImageCodecInfo.GetImageEncoders();
return encoders.FirstOrDefault(t => t.MimeType == mimeType);
}
if (string.IsNullOrEmpty(applicationFocus)) return;

private static string GetMD5Hash(Bitmap bitmap, ImageFormat format)
{
byte[] bytes = null;
Process[] process = Process.GetProcessesByName(applicationFocus);

using (MemoryStream ms = new MemoryStream())
foreach (var item in process)
{
bitmap.Save(ms, format.Format);
bytes = ms.ToArray();
}
var proc = Process.GetProcessById(item.Id);

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
IntPtr handle = proc.MainWindowHandle;
SetForegroundWindow(handle);

byte[] hash = md5.ComputeHash(bytes);
var placement = GetPlacement(proc.MainWindowHandle);

StringBuilder sb = new StringBuilder();

foreach (byte b in hash)
{
sb.Append(b.ToString("x2").ToLower());
if (placement.showCmd == ShowWindowCommands.Minimized)
{
ShowWindowAsync(proc.MainWindowHandle, (int)ShowWindowCommands.Normal);
}
}

return sb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<assemblyIdentity
type="win32"
name="GavinKendall.AutoScreenCapture"
version="2.3.2.9"/>
version="2.3.3.0"/>
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>True/PM</dpiAware>
Expand Down
3 changes: 2 additions & 1 deletion interface/FormAbout.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="richTextBoxDeveloper.Text" xml:space="preserve">
<value>Auto Screen Capture 2.3.2.9 ("Boombayah")
<value>Auto Screen Capture 2.3.3.0 ("Boombayah")
Developed by Gavin Kendall (2008 - 2020)

SourceForge Project Site
Expand Down Expand Up @@ -322,6 +322,7 @@ END OF TERMS AND CONDITIONS</value>
</data>
<data name="richTextBoxChangelog.Text" xml:space="preserve">
<value>Codename "Boombayah"
2.3.3.0 Application Focus moved from Screen to Setup. Fixed Application Focus bug with Active Window Title. Renamed user setting keys. New method for capturing device display resolution.
2.3.2.9 Application Focus implemented for Screen.
2.3.2.8 Changelog added to About Auto Screen Capture window. Fixed bug with hidden system tray icon so no notification balloon appears when system tray icon is hidden.
2.3.2.7 Quarter Year tag implemented so you can have %quarteryear% return "3" if we're currently in the third quarter of the current year.
Expand Down
2 changes: 1 addition & 1 deletion interface/FormEnterPassphrase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void buttonUnlock_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxPassphrase.Text)) return;

if (Security.Hash(textBoxPassphrase.Text).Equals(Settings.User.GetByKey("StringPassphrase", DefaultSettings.StringPassphrase).Value))
if (Security.Hash(textBoxPassphrase.Text).Equals(Settings.User.GetByKey("Passphrase", DefaultSettings.Passphrase).Value))
{
Log.WriteDebugMessage("Screen capture session was successfully unlocked by " + Environment.UserName + " on " + Environment.MachineName);

Expand Down
Loading

0 comments on commit 49f32fe

Please sign in to comment.