BUG-2707: Reducing the amount of NPE issues

This commit is contained in:
Robin Krom 2021-01-20 21:54:41 +01:00
commit 7bf2b7c092
4 changed files with 58 additions and 61 deletions

View file

@ -105,10 +105,6 @@ namespace Greenshot.Drawing {
cursor.DrawStretched(graphics, Bounds); cursor.DrawStretched(graphics, Bounds);
} }
public override Size DefaultSize { public override Size DefaultSize => cursor?.Size ?? new Size(16, 16);
get {
return cursor.Size;
}
}
} }
} }

View file

@ -562,23 +562,11 @@ namespace Greenshot.Drawing
return ScaleHelper.ShapeAngleRoundBehavior.Instance; return ScaleHelper.ShapeAngleRoundBehavior.Instance;
} }
public virtual bool HasContextMenu { public virtual bool HasContextMenu => true;
get {
return true;
}
}
public virtual bool HasDefaultSize { public virtual bool HasDefaultSize => false;
get {
return false;
}
}
public virtual Size DefaultSize { public virtual Size DefaultSize => throw new NotSupportedException("Object doesn't have a default size");
get {
throw new NotSupportedException("Object doesn't have a default size");
}
}
/// <summary> /// <summary>
/// Allows to override the initializing of the fields, so we can actually have our own defaults /// Allows to override the initializing of the fields, so we can actually have our own defaults

View file

@ -62,7 +62,7 @@ namespace Greenshot.Drawing {
Width = value.Width; Width = value.Width;
Height = value.Height; Height = value.Height;
} }
get { return icon; } get => icon;
} }
/** /**
@ -78,27 +78,32 @@ namespace Greenshot.Drawing {
base.Dispose(disposing); base.Dispose(disposing);
} }
public void Load(string filename) { public void Load(string filename)
if (File.Exists(filename)) {
{ if (!File.Exists(filename))
using Icon fileIcon = new Icon(filename); {
Icon = fileIcon; return;
Log.Debug("Loaded file: " + filename + " with resolution: " + Height + "," + Width); }
} using Icon fileIcon = new Icon(filename);
Icon = fileIcon;
Log.Debug("Loaded file: " + filename + " with resolution: " + Height + "," + Width);
} }
public override void Draw(Graphics graphics, RenderMode rm) { public override void Draw(Graphics graphics, RenderMode rm)
if (icon != null) { {
graphics.SmoothingMode = SmoothingMode.HighQuality; if (icon == null)
graphics.InterpolationMode = InterpolationMode.NearestNeighbor; {
graphics.CompositingQuality = CompositingQuality.Default; return;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawIcon(icon, Bounds);
} }
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
graphics.CompositingQuality = CompositingQuality.Default;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawIcon(icon, Bounds);
} }
public override bool HasDefaultSize => true; public override bool HasDefaultSize => true;
public override Size DefaultSize => icon.Size; public override Size DefaultSize => icon?.Size ?? new Size(16,16);
} }
} }

View file

@ -77,11 +77,14 @@ namespace Greenshot.Drawing {
AddField(GetType(), FieldType.SHADOW, false); AddField(GetType(), FieldType.SHADOW, false);
} }
protected void BitmapContainer_OnFieldChanged(object sender, FieldChangedEventArgs e) { protected void BitmapContainer_OnFieldChanged(object sender, FieldChangedEventArgs e)
if (sender.Equals(this)) { {
if (FieldType.SHADOW.Equals(e.Field.FieldType)) { if (!sender.Equals(this))
ChangeShadowField(); {
} return;
}
if (FieldType.SHADOW.Equals(e.Field.FieldType)) {
ChangeShadowField();
} }
} }
@ -189,12 +192,14 @@ namespace Greenshot.Drawing {
/// This checks if a shadow is already generated /// This checks if a shadow is already generated
/// </summary> /// </summary>
/// <param name="shadow"></param> /// <param name="shadow"></param>
private void CheckShadow(bool shadow) { private void CheckShadow(bool shadow)
if (shadow && _shadowBitmap == null) {
{ if (!shadow || _shadowBitmap != null)
using var matrix = new Matrix(); {
_shadowBitmap = ImageHelper.ApplyEffect(image, new DropShadowEffect(), matrix); return;
} }
using var matrix = new Matrix();
_shadowBitmap = ImageHelper.ApplyEffect(image, new DropShadowEffect(), matrix);
} }
/// <summary> /// <summary>
@ -202,25 +207,28 @@ namespace Greenshot.Drawing {
/// </summary> /// </summary>
/// <param name="graphics"></param> /// <param name="graphics"></param>
/// <param name="rm"></param> /// <param name="rm"></param>
public override void Draw(Graphics graphics, RenderMode rm) { public override void Draw(Graphics graphics, RenderMode rm)
if (image != null) { {
bool shadow = GetFieldValueAsBool(FieldType.SHADOW); if (image == null)
graphics.SmoothingMode = SmoothingMode.HighQuality; {
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; return;
graphics.CompositingQuality = CompositingQuality.HighQuality; }
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; bool shadow = GetFieldValueAsBool(FieldType.SHADOW);
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
if (shadow) { if (shadow) {
CheckShadow(true); CheckShadow(true);
graphics.DrawImage(_shadowBitmap, Bounds); graphics.DrawImage(_shadowBitmap, Bounds);
} else { } else {
graphics.DrawImage(image, Bounds); graphics.DrawImage(image, Bounds);
}
} }
} }
public override bool HasDefaultSize => true; public override bool HasDefaultSize => true;
public override Size DefaultSize => image.Size; public override Size DefaultSize => image?.Size ?? new Size(32, 32);
} }
} }