Merge pull request #7390 from flmbray/add-cutout-background-color

Add option to set background color for CutOut tool
This commit is contained in:
Jaex 2024-04-17 07:32:24 +03:00 committed by GitHub
commit 2328863616
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 63 additions and 14 deletions

View file

@ -260,24 +260,24 @@ public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect)
return null;
}
private static Bitmap ApplyCutOutEffect(Bitmap bmp, AnchorStyles effectEdge, CutOutEffectType effectType, int effectSize)
private static Bitmap ApplyCutOutEffect(Bitmap bmp, AnchorStyles effectEdge, CutOutEffectType effectType, int effectSize, Color cutOutBackgroundColor)
{
switch (effectType)
{
case CutOutEffectType.None:
return bmp;
case CutOutEffectType.ZigZag:
return TornEdges(bmp, effectSize, effectSize, effectEdge, false, false);
return TornEdges(bmp, effectSize, effectSize, effectEdge, false, false, cutOutBackgroundColor);
case CutOutEffectType.TornEdge:
return TornEdges(bmp, effectSize, effectSize * 2, effectEdge, false, true);
return TornEdges(bmp, effectSize, effectSize * 2, effectEdge, false, true, cutOutBackgroundColor);
case CutOutEffectType.Wave:
return WavyEdges(bmp, effectSize, effectSize * 5, effectEdge);
return WavyEdges(bmp, effectSize, effectSize * 5, effectEdge, cutOutBackgroundColor);
}
throw new NotImplementedException();
}
public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int start, int size, CutOutEffectType effectType, int effectSize)
public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int start, int size, CutOutEffectType effectType, int effectSize, Color cutOutBackgroundColor)
{
if (bmp != null && size > 0)
{
@ -290,7 +290,7 @@ public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int
: new Rectangle(0, 0, bmp.Width, Math.Min(start, bmp.Height));
firstPart = CropBitmap(bmp, r);
AnchorStyles effectEdge = orientation == Orientation.Horizontal ? AnchorStyles.Right : AnchorStyles.Bottom;
firstPart = ApplyCutOutEffect(firstPart, effectEdge, effectType, effectSize);
firstPart = ApplyCutOutEffect(firstPart, effectEdge, effectType, effectSize, cutOutBackgroundColor);
}
int cutDimension = orientation == Orientation.Horizontal ? bmp.Width : bmp.Height;
@ -302,7 +302,7 @@ public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int
: new Rectangle(0, end, bmp.Width, bmp.Height - end);
secondPart = CropBitmap(bmp, r);
AnchorStyles effectEdge = orientation == Orientation.Horizontal ? AnchorStyles.Left : AnchorStyles.Top;
secondPart = ApplyCutOutEffect(secondPart, effectEdge, effectType, effectSize);
secondPart = ApplyCutOutEffect(secondPart, effectEdge, effectType, effectSize, cutOutBackgroundColor);
}
if (firstPart != null && secondPart != null)
@ -1844,7 +1844,7 @@ public static void FastBoxBlur(Bitmap bmp, int radius)
}
}
public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorStyles sides)
public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorStyles sides, Color cutOutBackgroundColor)
{
if (waveDepth < 1 || waveRange < 1 || sides == AnchorStyles.None)
{
@ -1934,12 +1934,16 @@ public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorS
{
g.SetHighQuality();
g.PixelOffsetMode = PixelOffsetMode.Half;
if (cutOutBackgroundColor.A > 0)
{
g.Clear(cutOutBackgroundColor);
}
g.FillPolygon(brush, points.ToArray());
}
return bmpResult;
}
public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random)
public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random, Color cutOutBackgroundColor)
{
if (tornDepth < 1 || tornRange < 1 || sides == AnchorStyles.None)
{
@ -2028,6 +2032,10 @@ public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorS
{
g.SetHighQuality();
g.PixelOffsetMode = PixelOffsetMode.Half;
if (cutOutBackgroundColor.A > 0)
{
g.Clear(cutOutBackgroundColor);
}
Point[] fillPoints = points.Distinct().ToArray();

View file

@ -26,6 +26,7 @@
using ShareX.HelpersLib;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
namespace ShareX.ImageEffectsLib
@ -45,6 +46,9 @@ internal class TornEdge : ImageEffect
[DefaultValue(true)]
public bool CurvedEdges { get; set; }
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color CutOutBackgroundColor { get; set; }
public TornEdge()
{
this.ApplyDefaultPropertyValues();
@ -52,7 +56,7 @@ public TornEdge()
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.TornEdges(bmp, Depth, Range, Sides, CurvedEdges, true);
return ImageHelpers.TornEdges(bmp, Depth, Range, Sides, CurvedEdges, true, CutOutBackgroundColor);
}
protected override string GetSummary()

View file

@ -26,6 +26,7 @@
using ShareX.HelpersLib;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
namespace ShareX.ImageEffectsLib
@ -47,9 +48,12 @@ public WaveEdge()
this.ApplyDefaultPropertyValues();
}
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color CutOutBackgroundColor { get; set; }
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.WavyEdges(bmp, Depth, Range, Sides);
return ImageHelpers.WavyEdges(bmp, Depth, Range, Sides, CutOutBackgroundColor);
}
protected override string GetSummary()

View file

@ -323,6 +323,15 @@ internal class Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Cut out background color.
/// </summary>
internal static string CutOutBackgroundColor {
get {
return ResourceManager.GetString("CutOutBackgroundColor", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Cut out effect size:.
/// </summary>

View file

@ -830,4 +830,7 @@ Would you like to save the changes before closing the image editor?</value>
<data name="control_record_green" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\control-record-green.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="CutOutBackgroundColor" xml:space="preserve">
<value>Cut out background color</value>
</data>
</root>

View file

@ -110,5 +110,6 @@ public class AnnotationOptions
// Cut out tool
public CutOutEffectType CutOutEffectType { get; set; } = CutOutEffectType.None;
public int CutOutEffectSize { get; set; } = 10;
public Color CutOutBackgroundColor { get; set; } = Color.Transparent;
}
}

View file

@ -1993,12 +1993,12 @@ public void CutOut(RectangleF rect)
if (isHorizontal && cropRect.Width > 0)
{
CollapseAllHorizontal(rect.X, rect.Width);
UpdateCanvas(ImageHelpers.CutOutBitmapMiddle(Form.Canvas, Orientation.Horizontal, cropRect.X, cropRect.Width, AnnotationOptions.CutOutEffectType, AnnotationOptions.CutOutEffectSize));
UpdateCanvas(ImageHelpers.CutOutBitmapMiddle(Form.Canvas, Orientation.Horizontal, cropRect.X, cropRect.Width, AnnotationOptions.CutOutEffectType, AnnotationOptions.CutOutEffectSize, AnnotationOptions.CutOutBackgroundColor));
}
else if (!isHorizontal && cropRect.Height > 0)
{
CollapseAllVertical(rect.Y, rect.Height);
UpdateCanvas(ImageHelpers.CutOutBitmapMiddle(Form.Canvas, Orientation.Vertical, cropRect.Y, cropRect.Height, AnnotationOptions.CutOutEffectType, AnnotationOptions.CutOutEffectSize));
UpdateCanvas(ImageHelpers.CutOutBitmapMiddle(Form.Canvas, Orientation.Vertical, cropRect.Y, cropRect.Height, AnnotationOptions.CutOutEffectType, AnnotationOptions.CutOutEffectSize, AnnotationOptions.CutOutBackgroundColor));
}
}

View file

@ -49,7 +49,7 @@ internal partial class ShapeManager
private ToolStripButton tsbSaveImage, tsbBorderColor, tsbFillColor, tsbHighlightColor;
private ToolStripDropDownButton tsddbShapeOptions;
private ToolStripMenuItem tsmiShadow, tsmiShadowColor, tsmiUndo, tsmiRedo, tsmiDuplicate, tsmiDelete, tsmiDeleteAll,
tsmiMoveTop, tsmiMoveUp, tsmiMoveDown, tsmiMoveBottom, tsmiRegionCapture, tsmiQuickCrop, tsmiShowMagnifier;
tsmiMoveTop, tsmiMoveUp, tsmiMoveDown, tsmiMoveBottom, tsmiRegionCapture, tsmiQuickCrop, tsmiShowMagnifier, tsmiCutOutBackgroundColor;
private ToolStripLabeledNumericUpDown tslnudBorderSize, tslnudCornerRadius, tslnudCenterPoints, tslnudBlurRadius, tslnudPixelateSize, tslnudStepFontSize,
tslnudMagnifierPixelCount, tslnudStartingStepValue, tslnudMagnifyStrength, tslnudCutOutEffectSize;
private ToolStripLabel tslDragLeft, tslDragRight;
@ -668,6 +668,22 @@ internal void CreateToolbar()
};
tsddbShapeOptions.DropDownItems.Add(tslnudCutOutEffectSize);
tsmiCutOutBackgroundColor = new ToolStripMenuItem(Resources.CutOutBackgroundColor);
tsmiCutOutBackgroundColor.Click += (sender, e) =>
{
Form.Pause();
if (PickColor(AnnotationOptions.CutOutBackgroundColor, out Color newColor))
{
AnnotationOptions.CutOutBackgroundColor = newColor;
UpdateMenu();
UpdateCurrentShape();
}
Form.Resume();
};
tsddbShapeOptions.DropDownItems.Add(tsmiCutOutBackgroundColor);
// In dropdown menu if only last item is visible then menu opens at 0, 0 position on first open, so need to add dummy item to solve this weird bug...
tsddbShapeOptions.DropDownItems.Add(new ToolStripSeparator() { Visible = false });
@ -1492,6 +1508,9 @@ private void UpdateMenu()
tslnudCutOutEffectSize.Content.Value = AnnotationOptions.CutOutEffectSize;
if (tsmiCutOutBackgroundColor.Image != null) tsmiCutOutBackgroundColor.Image.Dispose();
tsmiCutOutBackgroundColor.Image = ImageHelpers.CreateColorPickerIcon(AnnotationOptions.CutOutBackgroundColor, new Rectangle(0, 0, 16, 16));
switch (shapeType)
{
default:
@ -1605,6 +1624,7 @@ private void UpdateMenu()
tsbHighlightColor.Visible = shapeType == ShapeType.EffectHighlight;
tscbCutOutEffectType.Visible = shapeType == ShapeType.ToolCutOut;
tslnudCutOutEffectSize.Visible = shapeType == ShapeType.ToolCutOut;
tsmiCutOutBackgroundColor.Visible = shapeType == ShapeType.ToolCutOut;
if (tsmiRegionCapture != null)
{