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);
}
public override Size DefaultSize {
get {
return cursor.Size;
}
}
public override Size DefaultSize => cursor?.Size ?? new Size(16, 16);
}
}

View file

@ -562,23 +562,11 @@ namespace Greenshot.Drawing
return ScaleHelper.ShapeAngleRoundBehavior.Instance;
}
public virtual bool HasContextMenu {
get {
return true;
}
}
public virtual bool HasContextMenu => true;
public virtual bool HasDefaultSize {
get {
return false;
}
}
public virtual bool HasDefaultSize => false;
public virtual Size DefaultSize {
get {
throw new NotSupportedException("Object doesn't have a default size");
}
}
public virtual Size DefaultSize => throw new NotSupportedException("Object doesn't have a default size");
/// <summary>
/// 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;
Height = value.Height;
}
get { return icon; }
get => icon;
}
/**
@ -78,27 +78,32 @@ namespace Greenshot.Drawing {
base.Dispose(disposing);
}
public void Load(string filename) {
if (File.Exists(filename))
public void Load(string filename)
{
if (!File.Exists(filename))
{
return;
}
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) {
if (icon != null) {
public override void Draw(Graphics graphics, RenderMode rm)
{
if (icon == null)
{
return;
}
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 Size DefaultSize => icon.Size;
public override Size DefaultSize => icon?.Size ?? new Size(16,16);
}
}

View file

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