Skip to content

Commit

Permalink
- 优化 截图时的编辑体验
Browse files Browse the repository at this point in the history
- 修复 截图出错 fixed #6
  • Loading branch information
hyjiacan committed Sep 22, 2020
1 parent b6fca0b commit 1d13636
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 更新日志

## 4.0.3

- 优化 截图时的编辑体验
- 修复 截图出错 [#6](/~https://github.com/hyjiacan/ColorWanted/issues/6)

## 4.0.2

- 优化 错误报告信息中添加异常类型
Expand Down
4 changes: 2 additions & 2 deletions ColorWanted/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.0.2")]
[assembly: AssemblyFileVersion("4.0.2")]
[assembly: AssemblyVersion("4.0.3")]
[assembly: AssemblyFileVersion("4.0.3")]
Binary file modified ColorWanted/bin/Release/ColorWanted.exe
Binary file not shown.
22 changes: 19 additions & 3 deletions ColorWanted/ext/GraphicsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void Draw(this Graphics graphics, DrawRecord record)
{
case DrawShapes.Curve:
pen.EndCap = LineCap.Round;
graphics.DrawCurve(pen, record.Points.Select(p=>new Point((int)p.X, (int)p.Y)).ToArray());
graphics.DrawCurve(pen, record.Points.Select(p => new Point((int)p.X, (int)p.Y)).ToArray());
break;
case DrawShapes.Ellipse:
graphics.DrawEllipse(pen, record.Rect.ToDrawingRectangle());
Expand All @@ -44,11 +44,27 @@ public static void Draw(this Graphics graphics, DrawRecord record)
{
return;
}
graphics.DrawString(record.Text, record.TextFont,
graphics.DrawString(record.Text, record.TextFont,
new SolidBrush(record.Color.ToDrawingColor()), record.Start.ToDrawingPoint());
break;
case DrawShapes.Arrow:
var cap = new AdjustableArrowCap(5, 5, false);
int arrowSize;
switch (record.Distance)
{
case 5:
case 4:
arrowSize = 2;
break;
case 3:
case 2:
case 1:
arrowSize = 1;
break;
default:
arrowSize = 5;
break;
}
var cap = new AdjustableArrowCap(arrowSize, arrowSize, false);
pen.CustomEndCap = cap;
graphics.DrawLine(pen, record.Start.ToDrawingPoint(), record.End.ToDrawingPoint());
break;
Expand Down
28 changes: 21 additions & 7 deletions ColorWanted/screenshot/DrawRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,21 @@ public FrameworkElement GetElement()
StrokeLineJoin = PenLineJoin.Bevel
};
}
((Polygon)shape).Points = new PointCollection(MakePolygon(Start.X, Start.Y, End.X, End.Y));
double arrowSize;
var distance = Distance;
if (distance >= 60)
{
arrowSize = 20;
}
else if (distance >= 32)
{
arrowSize = distance / 4.0;
}
else
{
arrowSize = 8;
}
((Polygon)shape).Points = new PointCollection(MakeArrow(Start.X, Start.Y, End.X, End.Y, arrowLength: arrowSize));
shape.Fill = new SolidColorBrush(Color);
break;
case DrawShapes.Text:
Expand Down Expand Up @@ -208,18 +222,18 @@ public FrameworkElement GetElement()
return shape;
}

private Point[] MakePolygon(double x1, double y1, double x2, double y2, double arrowAngle = Math.PI / 12, double arrowLength = 20)
private Point[] MakeArrow(double x1, double y1, double x2, double y2, double arrowAngle = Math.PI / 12, double arrowLength = 20)
{
Point point1 = new Point(x1, y1); // 箭头起点
Point point2 = new Point(x2, y2); // 箭头终点
double angleOri = Math.Atan((y2 - y1) / (x2 - x1)); // 起始点线段夹角
double angleDown = angleOri - arrowAngle; // 箭头扩张角度
double angleUp = angleOri + arrowAngle; // 箭头扩张角度
int directionFlag = (x2 > x1) ? -1 : 1; // 方向标识
double x3 = x2 + ((directionFlag * arrowLength) * Math.Cos(angleDown)); // 箭头第三个点的坐标
double y3 = y2 + ((directionFlag * arrowLength) * Math.Sin(angleDown));
double x4 = x2 + ((directionFlag * arrowLength) * Math.Cos(angleUp)); // 箭头第四个点的坐标
double y4 = y2 + ((directionFlag * arrowLength) * Math.Sin(angleUp));
double x3 = x2 + (directionFlag * arrowLength * Math.Cos(angleDown)); // 箭头第三个点的坐标
double y3 = y2 + (directionFlag * arrowLength * Math.Sin(angleDown));
double x4 = x2 + (directionFlag * arrowLength * Math.Cos(angleUp)); // 箭头第四个点的坐标
double y4 = y2 + (directionFlag * arrowLength * Math.Sin(angleUp));
Point point3 = new Point(x3, y3); // 箭头第三个点
Point point4 = new Point(x4, y4); // 箭头第四个点
return new Point[] { point1, point2, point3, point4, point2 }; // 多边形,起点 --> 终点 --> 第三点 --> 第四点 --> 终点
Expand Down Expand Up @@ -299,7 +313,7 @@ public Rect ElementRect
/// <summary>
/// 起点与终点的距离
/// </summary>
public int Distance => (int)Math.Sqrt(Math.Abs(Start.X - End.X) * Math.Abs(Start.X - End.X) + Math.Abs(Start.Y - End.Y) * Math.Abs(Start.Y - End.Y));
public int Distance => (int)Math.Sqrt((Math.Abs(Start.X - End.X) * Math.Abs(Start.X - End.X)) + Math.Abs(Start.Y - End.Y) * Math.Abs(Start.Y - End.Y));

public DrawRecord()
{
Expand Down
34 changes: 28 additions & 6 deletions ColorWanted/screenshot/ExtendedCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ private void EmitDrawEvent(DrawState state, bool isEmpty = false)
private void BindEvent()
{
MouseLeftButtonDown += OnMouseLeftButtonDown;
MouseLeftButtonUp += ExtendedCanvas_MouseLeaveOrUp;
MouseLeave += ExtendedCanvas_MouseLeaveOrUp; ;
MouseLeftButtonUp += ExtendedCanvas_MouseUp;
MouseLeave += ExtendedCanvas_MouseLeave; ;
MouseMove += OnMouseMove;
MouseRightButtonDown += On_MouseRightButtonDown;
}
Expand Down Expand Up @@ -237,7 +237,7 @@ private void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseButt
if (e.ClickCount == 2)
{
// 双击图形,触发双击事件
AreaDoubleClicked.Invoke(this, new AreaEventArgs(current.Rect));
AreaDoubleClicked.Invoke(this, new AreaEventArgs(current.ElementRect));
return;
}
MoveMode = true;
Expand Down Expand Up @@ -272,13 +272,18 @@ private void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseButt
EmitDrawEvent(DrawState.Start);
}

private void ExtendedCanvas_MouseLeaveOrUp(object sender, System.Windows.Input.MouseEventArgs e)
public void ExtendedCanvas_MouseUp(object sender, System.Windows.Input.MouseEventArgs e)
{
ExtendedCanvas_MouseLeave(sender, e);
IsMouseDown = false;
}

private void ExtendedCanvas_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!EditEnabled || !IsMouseDown)
{
return;
}
IsMouseDown = false;
var point = e.GetPosition(this);

// 保证线不会画出界
Expand Down Expand Up @@ -318,14 +323,31 @@ private void ExtendedCanvas_MouseLeaveOrUp(object sender, System.Windows.Input.M
EmitDrawEvent(DrawState.End);
}

private void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
public void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!EditEnabled || !IsMouseDown)
{
return;
}

var point = e.GetPosition(this);
// 保证线不会画出界
if (point.X < 0)
{
point.X = 0;
}
else if (point.X > Width)
{
point.X = Width;
}
if (point.Y < 0)
{
point.Y = 0;
}
else if (point.Y > Height)
{
point.Y = Height;
}
if (MouseDownPoint == point)
{
return;
Expand Down
3 changes: 2 additions & 1 deletion ColorWanted/screenshot/ImageEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<Canvas x:Name="container">
<local:ExtendedCanvas x:Name="canvasMask" DrawShape="Rectangle" DrawColor="BlueViolet"
MakeSelectionOnly="True" EditEnabled="True" OnDraw="CanvasMask_OnDraw"
AreaDoubleClicked="CanvasMask_AreaDoubleClicked">
AreaDoubleClicked="CanvasMask_AreaDoubleClicked" MouseLeftButtonUp="canvasMask_MouseUp"
MouseMove="canvasMask_MouseMove">
<local:ExtendedCanvas.Background>
<ImageBrush x:Name="maskBackground"></ImageBrush>
</local:ExtendedCanvas.Background>
Expand Down
17 changes: 16 additions & 1 deletion ColorWanted/screenshot/ImageEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,13 @@ private void UpdateSelectArea(Rect selectedRect)

private void CanvasMask_AreaDoubleClicked(object sender, AreaEventArgs e)
{
if (e.Rect.IsEmpty)
{
return;
}
var image = SelectedImage ?? EndEdit();
// 截图完成
Compeleted.Invoke(this, new DoubleClickEventArgs(SelectedImage));
Compeleted.Invoke(this, new DoubleClickEventArgs(image));
}

public void Reset()
Expand Down Expand Up @@ -304,5 +309,15 @@ public void Reset()
canvasEdit.Visibility = Visibility.Hidden;
canvasMask.EditEnabled = true;
}

private void canvasMask_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
canvasEdit.ExtendedCanvas_MouseUp(sender, e);
}

private void canvasMask_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
canvasEdit.OnMouseMove(null, e);
}
}
}

0 comments on commit 1d13636

Please sign in to comment.