This commit breaks compiling for a short (!) period, need to sync my work to a different system

This commit is contained in:
Robin Krom 2020-02-25 07:56:45 +01:00
parent 1751880581
commit 684a7615d7
38 changed files with 1845 additions and 1324 deletions

View file

@ -33,363 +33,7 @@ using GreenshotPlugin.Interfaces;
using GreenshotPlugin.UnmanagedHelpers.Structs;
namespace GreenshotPlugin.Core {
/// <summary>
/// This Class is used to pass details about the capture around.
/// The time the Capture was taken and the Title of the window (or a region of) that is captured
/// </summary>
public class CaptureDetails : ICaptureDetails {
public string Title {
get;
set;
}
public string Filename {
get;
set;
}
public DateTime DateTime {
get;
set;
}
public float DpiX {
get;
set;
}
public float DpiY {
get;
set;
}
public Dictionary<string, string> MetaData { get; } = new Dictionary<string, string>();
public void AddMetaData(string key, string value) {
if (MetaData.ContainsKey(key)) {
MetaData[key] = value;
} else {
MetaData.Add(key, value);
}
}
public CaptureMode CaptureMode {
get;
set;
}
public List<IDestination> CaptureDestinations { get; set; } = new List<IDestination>();
public void ClearDestinations() {
CaptureDestinations.Clear();
}
public void RemoveDestination(IDestination destination) {
if (CaptureDestinations.Contains(destination)) {
CaptureDestinations.Remove(destination);
}
}
public void AddDestination(IDestination captureDestination) {
if (!CaptureDestinations.Contains(captureDestination)) {
CaptureDestinations.Add(captureDestination);
}
}
public bool HasDestination(string designation) {
foreach(IDestination destination in CaptureDestinations) {
if (designation.Equals(destination.Designation)) {
return true;
}
}
return false;
}
public CaptureDetails() {
DateTime = DateTime.Now;
}
}
/// <summary>
/// This class is used to pass an instance of the "Capture" around
/// Having the Bitmap, eventually the Windows Title and cursor all together.
/// </summary>
public class Capture : ICapture {
private static readonly ILog Log = LogManager.GetLogger(typeof(Capture));
private List<ICaptureElement> _elements = new List<ICaptureElement>();
private Rectangle _screenBounds;
/// <summary>
/// Get/Set the Screenbounds
/// </summary>
public Rectangle ScreenBounds {
get {
if (_screenBounds == Rectangle.Empty) {
_screenBounds = WindowCapture.GetScreenBounds();
}
return _screenBounds;
}
set {_screenBounds = value;}
}
private Image _image;
/// <summary>
/// Get/Set the Image
/// </summary>
public Image Image {
get {return _image;}
set {
_image?.Dispose();
_image = value;
if (value != null) {
if (value.PixelFormat.Equals(PixelFormat.Format8bppIndexed) || value.PixelFormat.Equals(PixelFormat.Format1bppIndexed) || value.PixelFormat.Equals(PixelFormat.Format4bppIndexed)) {
Log.Debug("Converting Bitmap to PixelFormat.Format32bppArgb as we don't support: " + value.PixelFormat);
try {
// Default Bitmap PixelFormat is Format32bppArgb
_image = new Bitmap(value);
} finally {
// Always dispose, even when a exception occured
value.Dispose();
}
}
Log.DebugFormat("Image is set with the following specifications: {0} - {1}", _image.Size, _image.PixelFormat);
} else {
Log.Debug("Image is removed.");
}
}
}
public void NullImage() {
_image = null;
}
private Icon _cursor;
/// <summary>
/// Get/Set the image for the Cursor
/// </summary>
public Icon Cursor {
get {return _cursor;}
set {
_cursor?.Dispose();
_cursor = (Icon)value.Clone();
}
}
/// <summary>
/// The information which OCR brings
/// </summary>
public OcrInformation OcrInformation { get; set; }
/// <summary>
/// Set if the cursor is visible
/// </summary>
public bool CursorVisible { get; set; }
private Point _cursorLocation = Point.Empty;
/// <summary>
/// Get/Set the CursorLocation
/// </summary>
public Point CursorLocation {
get {return _cursorLocation;}
set {_cursorLocation = value;}
}
private Point _location = Point.Empty;
/// <summary>
/// Get/set the Location
/// </summary>
public Point Location {
get {return _location;}
set {_location = value;}
}
private CaptureDetails _captureDetails;
/// <summary>
/// Get/set the CaptureDetails
/// </summary>
public ICaptureDetails CaptureDetails {
get {return _captureDetails;}
set {_captureDetails = (CaptureDetails)value;}
}
/// <summary>
/// Default Constructor
/// </summary>
public Capture() {
_screenBounds = WindowCapture.GetScreenBounds();
_captureDetails = new CaptureDetails();
}
/// <summary>
/// Constructor with Image
/// Note: the supplied bitmap can be disposed immediately or when constructor is called.
/// </summary>
/// <param name="newImage">Image</param>
public Capture(Image newImage) : this() {
Image = newImage;
}
/// <summary>
/// Destructor
/// </summary>
~Capture() {
Dispose(false);
}
/// <summary>
/// The public accessible Dispose
/// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice
/// </summary>
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// This Dispose is called from the Dispose and the Destructor.
/// When disposing==true all non-managed resources should be freed too!
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing) {
if (disposing) {
_image?.Dispose();
_cursor?.Dispose();
}
_image = null;
_cursor = null;
}
/// <summary>
/// Crops the capture to the specified rectangle (with Bitmap coordinates!)
/// </summary>
/// <param name="cropRectangle">Rectangle with bitmap coordinates</param>
public bool Crop(Rectangle cropRectangle) {
Log.Debug("Cropping to: " + cropRectangle);
if (!ImageHelper.Crop(ref _image, ref cropRectangle))
{
return false;
}
_location = cropRectangle.Location;
// Change mouse location according to the cropRegtangle (including screenbounds) offset
MoveMouseLocation(-cropRectangle.Location.X, -cropRectangle.Location.Y);
// Move all the elements
// TODO: Enable when the elements are usable again.
// MoveElements(-cropRectangle.Location.X, -cropRectangle.Location.Y);
// Remove invisible elements
var newElements = new List<ICaptureElement>();
foreach(var captureElement in _elements) {
if (captureElement.Bounds.IntersectsWith(cropRectangle)) {
newElements.Add(captureElement);
}
}
_elements = newElements;
return true;
}
/// <summary>
/// Apply a translate to the mouse location.
/// e.g. needed for crop
/// </summary>
/// <param name="x">x coordinates to move the mouse</param>
/// <param name="y">y coordinates to move the mouse</param>
public void MoveMouseLocation(int x, int y) {
_cursorLocation.Offset(x, y);
}
// TODO: Enable when the elements are usable again.
///// <summary>
///// Apply a translate to the elements
///// e.g. needed for crop
///// </summary>
///// <param name="x">x coordinates to move the elements</param>
///// <param name="y">y coordinates to move the elements</param>
//public void MoveElements(int x, int y) {
// MoveElements(elements, x, y);
//}
//private void MoveElements(List<ICaptureElement> listOfElements, int x, int y) {
// foreach(ICaptureElement childElement in listOfElements) {
// Rectangle bounds = childElement.Bounds;
// bounds.Offset(x, y);
// childElement.Bounds = bounds;
// MoveElements(childElement.Children, x, y);
// }
//}
///// <summary>
///// Add a new element to the capture
///// </summary>
///// <param name="element">CaptureElement</param>
//public void AddElement(ICaptureElement element) {
// int match = elements.IndexOf(element);
// if (match >= 0) {
// if (elements[match].Children.Count < element.Children.Count) {
// elements.RemoveAt(match);
// elements.Add(element);
// }
// } else {
// elements.Add(element);
// }
//}
///// <summary>
///// Returns a list of rectangles which represent object that are on the capture
///// </summary>
//public List<ICaptureElement> Elements {
// get {
// return elements;
// }
// set {
// elements = value;
// }
//}
}
/// <summary>
/// A class representing an element in the capture
/// </summary>
public class CaptureElement : ICaptureElement {
public CaptureElement(Rectangle bounds) {
Bounds = bounds;
}
public CaptureElement(string name) {
Name = name;
}
public CaptureElement(string name, Rectangle bounds) {
Name = name;
Bounds = bounds;
}
public List<ICaptureElement> Children { get; set; } = new List<ICaptureElement>();
public string Name {
get;
set;
}
public Rectangle Bounds {
get;
set;
}
// CaptureElements are regarded equal if their bounds are equal. this should be sufficient.
public override bool Equals(object obj) {
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return obj is CaptureElement other && Bounds.Equals(other.Bounds);
}
public override int GetHashCode() {
// TODO: Fix this, this is not right...
return Bounds.GetHashCode();
}
}
/// <summary>
/// The Window Capture code
/// </summary>
public static class WindowCapture {
@ -397,7 +41,7 @@ namespace GreenshotPlugin.Core {
private static readonly CoreConfiguration Configuration = IniConfig.GetIniSection<CoreConfiguration>();
/// <summary>
/// Used to cleanup the unmanged resource in the iconInfo for the CaptureCursor method
/// Used to cleanup the unmanaged resource in the iconInfo for the CaptureCursor method
/// </summary>
/// <param name="hObject"></param>
/// <returns></returns>
@ -435,11 +79,11 @@ namespace GreenshotPlugin.Core {
/// <summary>
/// Converts locationRelativeToScreenOrigin to be relative to top left corner of all screen bounds, which might
/// be different in multiscreen setups. This implementation
/// be different in multi-screen setups. This implementation
/// can conveniently be used when the cursor location is needed to deal with a fullscreen bitmap.
/// </summary>
/// <param name="locationRelativeToScreenOrigin"></param>
/// <returns></returns>
/// <returns>Point</returns>
public static Point GetLocationRelativeToScreenBounds(Point locationRelativeToScreenOrigin) {
Point ret = locationRelativeToScreenOrigin;
Rectangle bounds = GetScreenBounds();
@ -456,34 +100,32 @@ namespace GreenshotPlugin.Core {
if (capture == null) {
capture = new Capture();
}
CursorInfo cursorInfo = new CursorInfo();
var cursorInfo = new CursorInfo();
cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
if (User32.GetCursorInfo(out cursorInfo)) {
if (cursorInfo.flags == User32.CURSOR_SHOWING)
{
using SafeIconHandle safeIcon = User32.CopyIcon(cursorInfo.hCursor);
if (User32.GetIconInfo(safeIcon, out var iconInfo)) {
Point cursorLocation = User32.GetCursorLocation();
// Allign cursor location to Bitmap coordinates (instead of Screen coordinates)
var x = cursorLocation.X - iconInfo.xHotspot - capture.ScreenBounds.X;
var y = cursorLocation.Y - iconInfo.yHotspot - capture.ScreenBounds.Y;
// Set the location
capture.CursorLocation = new Point(x, y);
if (!User32.GetCursorInfo(out cursorInfo)) return capture;
if (cursorInfo.flags != User32.CURSOR_SHOWING) return capture;
using (Icon icon = Icon.FromHandle(safeIcon.DangerousGetHandle())) {
capture.Cursor = icon;
}
using SafeIconHandle safeIcon = User32.CopyIcon(cursorInfo.hCursor);
if (!User32.GetIconInfo(safeIcon, out var iconInfo)) return capture;
if (iconInfo.hbmMask != IntPtr.Zero) {
DeleteObject(iconInfo.hbmMask);
}
if (iconInfo.hbmColor != IntPtr.Zero) {
DeleteObject(iconInfo.hbmColor);
}
}
}
}
return capture;
Point cursorLocation = User32.GetCursorLocation();
// Align cursor location to Bitmap coordinates (instead of Screen coordinates)
var x = cursorLocation.X - iconInfo.xHotspot - capture.ScreenBounds.X;
var y = cursorLocation.Y - iconInfo.yHotspot - capture.ScreenBounds.Y;
// Set the location
capture.CursorLocation = new Point(x, y);
using (Icon icon = Icon.FromHandle(safeIcon.DangerousGetHandle())) {
capture.Cursor = icon;
}
if (iconInfo.hbmMask != IntPtr.Zero) {
DeleteObject(iconInfo.hbmMask);
}
if (iconInfo.hbmColor != IntPtr.Zero) {
DeleteObject(iconInfo.hbmColor);
}
return capture;
}
/// <summary>
@ -518,19 +160,19 @@ namespace GreenshotPlugin.Core {
/// <param name="process">Process owning the window</param>
/// <returns>true if it's allowed</returns>
public static bool IsDwmAllowed(Process process) {
if (process != null) {
if (Configuration.NoDWMCaptureForProduct != null && Configuration.NoDWMCaptureForProduct.Count > 0) {
try {
string productName = process.MainModule.FileVersionInfo.ProductName;
if (productName != null && Configuration.NoDWMCaptureForProduct.Contains(productName.ToLower())) {
return false;
}
} catch (Exception ex) {
Log.Warn(ex.Message);
}
}
}
return true;
if (process == null) return true;
if (Configuration.NoDWMCaptureForProduct == null ||
Configuration.NoDWMCaptureForProduct.Count <= 0) return true;
try {
string productName = process.MainModule?.FileVersionInfo.ProductName;
if (productName != null && Configuration.NoDWMCaptureForProduct.Contains(productName.ToLower())) {
return false;
}
} catch (Exception ex) {
Log.Warn(ex.Message);
}
return true;
}
/// <summary>
@ -539,19 +181,19 @@ namespace GreenshotPlugin.Core {
/// <param name="process">Process owning the window</param>
/// <returns>true if it's allowed</returns>
public static bool IsGdiAllowed(Process process) {
if (process != null) {
if (Configuration.NoGDICaptureForProduct != null && Configuration.NoGDICaptureForProduct.Count > 0) {
try {
string productName = process.MainModule.FileVersionInfo.ProductName;
if (productName != null && Configuration.NoGDICaptureForProduct.Contains(productName.ToLower())) {
return false;
}
} catch (Exception ex) {
Log.Warn(ex.Message);
}
}
}
return true;
if (process == null) return true;
if (Configuration.NoGDICaptureForProduct == null ||
Configuration.NoGDICaptureForProduct.Count <= 0) return true;
try {
string productName = process.MainModule?.FileVersionInfo.ProductName;
if (productName != null && Configuration.NoGDICaptureForProduct.Contains(productName.ToLower())) {
return false;
}
} catch (Exception ex) {
Log.Warn(ex.Message);
}
return true;
}
/// <summary>
@ -645,10 +287,10 @@ namespace GreenshotPlugin.Core {
Win32.SetLastError(0);
// create a bitmap we can copy it to, using GetDeviceCaps to get the width/height
using SafeDibSectionHandle safeDibSectionHandle = GDI32.CreateDIBSection(desktopDcHandle, ref bmi, BITMAPINFOHEADER.DIB_RGB_COLORS, out var bits0, IntPtr.Zero, 0);
using SafeDibSectionHandle safeDibSectionHandle = GDI32.CreateDIBSection(desktopDcHandle, ref bmi, BITMAPINFOHEADER.DIB_RGB_COLORS, out _, IntPtr.Zero, 0);
if (safeDibSectionHandle.IsInvalid) {
// Get Exception before the error is lost
Exception exceptionToThrow = CreateCaptureException("CreateDIBSection", captureBounds);
var exceptionToThrow = CreateCaptureException("CreateDIBSection", captureBounds);
exceptionToThrow.Data.Add("hdcDest", safeCompatibleDcHandle.DangerousGetHandle().ToInt32());
exceptionToThrow.Data.Add("hdcSrc", desktopDcHandle.DangerousGetHandle().ToInt32());