Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
新功能:画图
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiJh committed Mar 5, 2021
1 parent c3e3521 commit f032045
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 12 deletions.
Binary file modified .vs/ShotApp/v16/.suo
Binary file not shown.
20 changes: 20 additions & 0 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="eraserIcon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\eraserIcon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="logo" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\logo.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="penIcon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\penIcon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added Resources/eraserIcon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/penIcon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 16 additions & 6 deletions Screenshot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Page Include="View\ImgBox.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand All @@ -123,6 +128,7 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="View\ImgBox.cs" />
<Compile Include="View\ImgBox.xaml.cs">
<DependentUpon>ImgBox.xaml</DependentUpon>
</Compile>
Expand All @@ -135,11 +141,6 @@
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
Expand Down Expand Up @@ -180,7 +181,16 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="Resources\penIcon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="Resources\eraserIcon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterResolveReferences">
<ItemGroup>
Expand Down
160 changes: 160 additions & 0 deletions View/ImgBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApp.View
{
public partial class ImgBox : Window
{
private void Pen_Click(object sender, RoutedEventArgs e)
{
if (!mode.Equals(Mode.Pen))
{
ShutdownEraserMode();
OpenPenMode();
}
else
{
ShutdownPenMode();
}

}

private void Eraser_Click(object sender, RoutedEventArgs e)
{
if (!mode.Equals(Mode.Eraser))
{
ShutdownPenMode();
OpenEraserMode();
}
else
{
ShutdownEraserMode();
}
}

private void OpenEraserMode()
{
mode = Mode.Eraser;
this.MouseLeftButtonDown += RemoveLine;
}

private void ShutdownEraserMode()
{
mode = Mode.Drag;
this.MouseLeftButtonDown -= RemoveLine;
}

private void RemoveLine(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(this);
IEnumerator ie = this.Panel.Children.GetEnumerator();
Line cur = null;
double min = 10;
while (ie.MoveNext())
{
var elem = ie.Current as Line;
double dis = GetDistance(p, elem);
if (dis < min)
{
cur = elem;
min = dis;
}
}
if(cur != null)
{
this.Panel.Children.Remove(cur);
}
}

private double GetDistance(Point p, Line l)
{
double k = (l.Y2 - l.Y1) / (l.X2 - l.X1);
double a = k;
double b = -1;
double c = l.Y1 - k * l.X1;
double fz = Math.Abs(a * p.X + b * p.Y + c);
double fm = Math.Sqrt(a * a + b * b);
return fz / fm;
}

private void OpenPenMode()
{
mode = Mode.Pen;
this.Cursor = Cursors.Pen;
this.MouseLeftButtonUp += StopDrawLine;
this.MouseLeftButtonDown += StartDrawLine;
this.MouseMove += DrawLine;
}

private void ShutdownPenMode()
{
mode = Mode.Drag;
this.Cursor = Cursors.Arrow;
this.MouseLeftButtonUp -= StopDrawLine;
this.MouseLeftButtonDown -= StartDrawLine;
this.MouseMove -= DrawLine;
}

private Line line;

private void StartDrawLine(object sender, MouseEventArgs e)
{
var p = e.GetPosition(this);
line = new Line();
line.Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
line.StrokeThickness = 3;
line.X1 = p.X;
line.Y1 = p.Y;
this.Panel.Children.Add(line);
}

private void DrawLine(object sender, MouseEventArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
{
Point p = e.GetPosition(this);
if (line != null)
{
line.X2 = p.X;
line.Y2 = p.Y;
}
}
}

private void StopDrawLine(object sender, MouseEventArgs e)
{
line = null;
}

private void Bar_MouseEnter(object sender, MouseEventArgs e)
{
this.Bar.Width = rw;
}

private void Bar_MouseLeave(object sender, MouseEventArgs e)
{
this.Bar.Width = 15;
}

private void Bar_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine(e.Key);
if(e.Key.Equals(Key.Escape))
{
if (!mode.Equals(Mode.Drag))
{
ShutdownEraserMode();
ShutdownPenMode();
}
}
}
}
}
44 changes: 42 additions & 2 deletions View/ImgBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,53 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp.View"
mc:Ignorable="d"
Title="ImgBox" MouseMove="WindowDrag" Topmost="True" SizeChanged="img_SizeChanged" WindowStyle="None" Background="White"
KeyDown="Bar_KeyDown"
Title="ImgBox" MouseMove="Mouse_Move" Topmost="True" SizeChanged="img_SizeChanged" WindowStyle="None" Background="{x:Null}"
ResizeMode="NoResize" BorderThickness="0.5" Height="424" Width="663">
<Window.Resources>
<Style x:Key="MyButton" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.8" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Opacity" Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid VerticalAlignment="Top" MouseWheel="Grid_MouseWheel" Margin="0,0,0,0">
<Image Name="img" Source="测试用.jpg" Panel.ZIndex="2000" RenderTransformOrigin="0,0" Margin="0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Image x:Name="img" Source="测试用.jpg" Panel.ZIndex="2000" RenderTransformOrigin="0,0" Margin="0.083,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.RowSpan="2" Height="423" Width="661.917"/>
<Canvas x:Name="Panel" HorizontalAlignment="Left" Height="423" Margin="0.083,0,0,0" VerticalAlignment="Top" Width="661.917" Panel.ZIndex="2000"/>
<Viewbox HorizontalAlignment="Left" Height="49" VerticalAlignment="Top" Width="171.707" Panel.ZIndex="2000">
<ToolBar ToolBarTray.IsLocked="True" x:Name="Bar" HorizontalAlignment="Left" Height="49" VerticalAlignment="Top" Width="171.707" Background="White" Cursor="Hand" MouseEnter="Bar_MouseEnter" MouseLeave="Bar_MouseLeave">
<Button x:Name="Pen" Content="" Height="42" VerticalAlignment="Center" Width="72" Margin="0" Cursor="Hand" IsTabStop="False" Style="{DynamicResource MyButton}" Click="Pen_Click" >
<Button.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/penIcon.png" Stretch="Uniform"/>
</Button.Background>
</Button>
<Button x:Name="Eraser" Content="" VerticalAlignment="Center" Cursor="Hand" Style="{DynamicResource MyButton}" Width="72" Height="42" Click="Eraser_Click">
<Button.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/eraserIcon.png" Stretch="Uniform"/>
</Button.Background>
</Button>
</ToolBar>
</Viewbox>
</Grid>
<Window.ContextMenu>
<ContextMenu>
<MenuItem Name="ClearItem" Header="清空" Click="ClearItem_Click"/>
<MenuItem Name="CopyItem" Header="复制" Click="CopyItem_Click" />
<MenuItem Name="SaveItem" Header="保存" Click="SaveItem_Click" />
<MenuItem Name="ExitItem" Header="退出" Click="ExitItem_Click" />
Expand Down
30 changes: 27 additions & 3 deletions View/ImgBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@

namespace WpfApp.View
{

enum Mode
{
Drag,
Pen,
Eraser,
}

/// <summary>
/// ImgBox.xaml 的交互逻辑
/// </summary>
public partial class ImgBox : Window
{
private double scale;
private Mode mode = Mode.Drag;
private double rw;

public ImgBox(ImageSource image, double width, double height)
{
Expand All @@ -29,12 +39,21 @@ public ImgBox(ImageSource image, double width, double height)

scale = width / height;
img.Source = image;

rw = this.Bar.Width;
this.Bar.Width = 15;
}

private void WindowDrag(object sender, MouseEventArgs e)
private void Mouse_Move(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
this.DragMove();
{
if (mode.Equals(Mode.Drag))
{
this.DragMove();
}
}

}

private void ExitItem_Click(object sender, RoutedEventArgs e)
Expand All @@ -50,7 +69,7 @@ private void CopyItem_Click(object sender, RoutedEventArgs e)
private void SaveItem_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Image Files (*.bmp, *.png, *.jpg)|*.bmp;*.png;*.jpg | All Files | *.*";
sfd.Filter = "Image Files (*.png, *.bmp, *.jpg)|*.png;*.bmp;*.jpg | All Files | *.*";
sfd.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
if (sfd.ShowDialog().GetValueOrDefault())
{
Expand Down Expand Up @@ -86,5 +105,10 @@ private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
this.Height = this.Width / scale;
}
}

private void ClearItem_Click(object sender, RoutedEventArgs e)
{
this.Panel.Children.Clear();
}
}
}
2 changes: 1 addition & 1 deletion View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
<ColumnDefinition Width="311*"/>
</Grid.ColumnDefinitions>
<Image VerticalAlignment="Top" x:Name="img" Source="测试用.jpg" Margin="0,0,0,0" Grid.ColumnSpan="2"/>
<Canvas Name = "canvas" HorizontalAlignment="Left" Height="439" VerticalAlignment="Top" Width="790" Background="#62636363" Margin="0,0,-134,-20" Grid.ColumnSpan="2"/>
<Canvas Name = "canvas" HorizontalAlignment="Left" Height="439" VerticalAlignment="Top" Width="791.546" Background="#62636363" Margin="0,0,-99.898,0" Grid.ColumnSpan="2"/>
</Grid>
</Window>

0 comments on commit f032045

Please sign in to comment.