mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-01 04:30:49 +12:00
Refactor CutOutBitmapMiddle
This commit is contained in:
parent
2465e94206
commit
9de1953e6d
|
@ -223,124 +223,72 @@ public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect)
|
|||
return null;
|
||||
}
|
||||
|
||||
public static Bitmap CutOutBitmapMiddleHorizontal(Bitmap bmp, int x, int width, CutOutEffectType effectType, int effectSize)
|
||||
private static Bitmap ApplyCutOutEffect(Bitmap bmp, AnchorStyles effectEdge, CutOutEffectType effectType, int effectSize)
|
||||
{
|
||||
if (bmp != null && width > 0)
|
||||
{
|
||||
Bitmap leftPart = null, rightPart = null;
|
||||
if (x > 0)
|
||||
{
|
||||
leftPart = CropBitmap(bmp, new Rectangle(0, 0, Math.Min(x, bmp.Width), bmp.Height));
|
||||
switch (effectType)
|
||||
{
|
||||
case CutOutEffectType.None:
|
||||
break;
|
||||
return bmp;
|
||||
|
||||
case CutOutEffectType.ZigZag:
|
||||
break;
|
||||
return bmp;
|
||||
|
||||
case CutOutEffectType.TornEdge:
|
||||
leftPart = TornEdges(leftPart, effectSize, effectSize * 2, AnchorStyles.Right, false);
|
||||
break;
|
||||
return TornEdges(bmp, effectSize, effectSize * 2, effectEdge, false);
|
||||
|
||||
case CutOutEffectType.Wave:
|
||||
break;
|
||||
return bmp;
|
||||
|
||||
case CutOutEffectType.Gradient:
|
||||
break;
|
||||
return bmp;
|
||||
}
|
||||
|
||||
throw new NotImplementedException(); // should not be reachable
|
||||
}
|
||||
if (x + width < bmp.Width)
|
||||
|
||||
public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int start, int size, CutOutEffectType effectType, int effectSize)
|
||||
{
|
||||
int x2 = Math.Max(x + width, 0);
|
||||
rightPart = CropBitmap(bmp, new Rectangle(x2, 0, bmp.Width - x2, bmp.Height));
|
||||
switch (effectType)
|
||||
if (bmp != null && size > 0)
|
||||
{
|
||||
case CutOutEffectType.None:
|
||||
break;
|
||||
case CutOutEffectType.ZigZag:
|
||||
break;
|
||||
case CutOutEffectType.TornEdge:
|
||||
rightPart = TornEdges(rightPart, effectSize, effectSize * 2, AnchorStyles.Left, false);
|
||||
break;
|
||||
case CutOutEffectType.Wave:
|
||||
break;
|
||||
case CutOutEffectType.Gradient:
|
||||
break;
|
||||
Bitmap firstPart = null, secondPart = null;
|
||||
|
||||
if (start > 0)
|
||||
{
|
||||
Rectangle r = orientation == Orientation.Horizontal
|
||||
? new Rectangle(0, 0, Math.Min(start, bmp.Width), bmp.Height)
|
||||
: 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);
|
||||
}
|
||||
|
||||
int cutDimension = orientation == Orientation.Horizontal ? bmp.Width : bmp.Height;
|
||||
if (start + size < cutDimension)
|
||||
{
|
||||
int end = Math.Max(start + size, 0);
|
||||
Rectangle r = orientation == Orientation.Horizontal
|
||||
? new Rectangle(end, 0, bmp.Width - end, bmp.Height)
|
||||
: 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);
|
||||
}
|
||||
|
||||
if (firstPart != null && secondPart != null)
|
||||
{
|
||||
return CombineImages(new List<Bitmap> { firstPart, secondPart }, orientation);
|
||||
}
|
||||
else if (firstPart != null)
|
||||
{
|
||||
return firstPart;
|
||||
}
|
||||
else if (secondPart != null)
|
||||
{
|
||||
return secondPart;
|
||||
}
|
||||
}
|
||||
|
||||
if (leftPart != null && rightPart != null)
|
||||
{
|
||||
return CombineImages(new List<Bitmap> { leftPart, rightPart }, Orientation.Horizontal);
|
||||
}
|
||||
else if (leftPart != null)
|
||||
{
|
||||
return leftPart;
|
||||
}
|
||||
else if (rightPart != null)
|
||||
{
|
||||
return rightPart;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Bitmap CutOutBitmapMiddleVertical(Bitmap bmp, int y, int height, CutOutEffectType effectType, int effectSize)
|
||||
{
|
||||
if (bmp != null && height > 0)
|
||||
{
|
||||
Bitmap topPart = null, bottomPart = null;
|
||||
if (y > 0)
|
||||
{
|
||||
topPart = CropBitmap(bmp, new Rectangle(0, 0, bmp.Width, Math.Min(y, bmp.Height)));
|
||||
switch (effectType)
|
||||
{
|
||||
case CutOutEffectType.None:
|
||||
break;
|
||||
case CutOutEffectType.ZigZag:
|
||||
break;
|
||||
case CutOutEffectType.TornEdge:
|
||||
topPart = TornEdges(topPart, effectSize, effectSize * 2, AnchorStyles.Bottom, false);
|
||||
break;
|
||||
case CutOutEffectType.Wave:
|
||||
break;
|
||||
case CutOutEffectType.Gradient:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (y + height < bmp.Height)
|
||||
{
|
||||
int y2 = Math.Max(y + height, 0);
|
||||
bottomPart = CropBitmap(bmp, new Rectangle(0, y2, bmp.Width, bmp.Height - y2));
|
||||
switch (effectType)
|
||||
{
|
||||
case CutOutEffectType.None:
|
||||
break;
|
||||
case CutOutEffectType.ZigZag:
|
||||
break;
|
||||
case CutOutEffectType.TornEdge:
|
||||
bottomPart = TornEdges(bottomPart, effectSize, effectSize * 2, AnchorStyles.Top, false);
|
||||
break;
|
||||
case CutOutEffectType.Wave:
|
||||
break;
|
||||
case CutOutEffectType.Gradient:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (topPart != null && bottomPart != null)
|
||||
{
|
||||
return CombineImages(new List<Bitmap> { topPart, bottomPart }, Orientation.Vertical);
|
||||
}
|
||||
else if (topPart != null)
|
||||
{
|
||||
return topPart;
|
||||
}
|
||||
else if (bottomPart != null)
|
||||
{
|
||||
return bottomPart;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return bmp;
|
||||
}
|
||||
|
||||
/// <summary>Automatically crop image to remove transparent outside area.</summary>
|
||||
|
|
|
@ -1929,12 +1929,12 @@ public void CutOut(RectangleF rect)
|
|||
if (isHorizontal && cropRect.Width > 0)
|
||||
{
|
||||
CollapseAllHorizontal(rect.X, rect.Width);
|
||||
UpdateCanvas(ImageHelpers.CutOutBitmapMiddleHorizontal(Form.Canvas, cropRect.X, cropRect.Width, CutOutEffectType.TornEdge, 5));
|
||||
UpdateCanvas(ImageHelpers.CutOutBitmapMiddle(Form.Canvas, Orientation.Horizontal, cropRect.X, cropRect.Width, CutOutEffectType.TornEdge, 5));
|
||||
}
|
||||
else if (!isHorizontal && cropRect.Height > 0)
|
||||
{
|
||||
CollapseAllVertical(rect.Y, rect.Height);
|
||||
UpdateCanvas(ImageHelpers.CutOutBitmapMiddleVertical(Form.Canvas, cropRect.Y, cropRect.Height, CutOutEffectType.TornEdge, 5));
|
||||
UpdateCanvas(ImageHelpers.CutOutBitmapMiddle(Form.Canvas, Orientation.Vertical, cropRect.Y, cropRect.Height, CutOutEffectType.TornEdge, 5));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue