Skip to content

Commit bec9215

Browse files
Merge pull request #41 from WPFDevelopersOrg/dev
add in CropImage
2 parents 55045f6 + 2e833ae commit bec9215

File tree

12 files changed

+482
-41
lines changed

12 files changed

+482
-41
lines changed

src/WPFDevelopers.Net40/Themes/Theme.xaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5982,6 +5982,22 @@
59825982
</Setter>
59835983
</Style>
59845984
<Style BasedOn="{StaticResource WD.BreadCrumbBar}" TargetType="{x:Type controls:BreadCrumbBar}" />
5985+
<Style x:Key="WD.CropImage" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type controls:CropImage}">
5986+
<Setter Property="Template">
5987+
<Setter.Value>
5988+
<ControlTemplate TargetType="{x:Type controls:CropImage}">
5989+
<Canvas x:Name="PART_Canvas">
5990+
<Rectangle x:Name="PART_RectangleLeft" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
5991+
<Rectangle x:Name="PART_RectangleTop" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
5992+
<Rectangle x:Name="PART_RectangleRight" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
5993+
<Rectangle x:Name="PART_RectangleBottom" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
5994+
<Border x:Name="PART_Border" Background="Transparent" BorderBrush="{DynamicResource WD.PrimaryNormalSolidColorBrush}" BorderThickness="2" Cursor="SizeAll" />
5995+
</Canvas>
5996+
</ControlTemplate>
5997+
</Setter.Value>
5998+
</Setter>
5999+
</Style>
6000+
<Style BasedOn="{StaticResource WD.CropImage}" TargetType="{x:Type controls:CropImage}" />
59856001
<Style x:Key="WD.DatePickerCalendarStyle" BasedOn="{StaticResource {x:Type Calendar}}" TargetType="{x:Type Calendar}" />
59866002
<Style x:Key="WD.DefaultDatePicker" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type DatePicker}">
59876003
<Setter Property="IsTodayHighlighted" Value="True" />

src/WPFDevelopers.Net45x/Themes/Theme.xaml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4415,16 +4415,6 @@
44154415
</Setter>
44164416
</Style>
44174417
<Style BasedOn="{StaticResource WD.CircularProgressBar}" TargetType="{x:Type controls:CircularProgressBar}" />
4418-
<Style x:Key="WD.CropControl" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type controls:CropControl}">
4419-
<Setter Property="Template">
4420-
<Setter.Value>
4421-
<ControlTemplate TargetType="{x:Type controls:CropControl}">
4422-
<UniformGrid x:Name="PART_UniformGrid" Columns="{TemplateBinding RowColumn}" Rows="{TemplateBinding RowColumn}" />
4423-
</ControlTemplate>
4424-
</Setter.Value>
4425-
</Setter>
4426-
</Style>
4427-
<Style BasedOn="{StaticResource WD.CropControl}" TargetType="{x:Type controls:CropControl}" />
44284418
<Style x:Key="WD.EdgeLight" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type controls:EdgeLight}">
44294419
<Setter Property="BorderBrush" Value="{DynamicResource WD.PrimaryNormalSolidColorBrush}" />
44304420
<Setter Property="HorizontalContentAlignment" Value="Center" />
@@ -5993,6 +5983,22 @@
59935983
</Setter>
59945984
</Style>
59955985
<Style BasedOn="{StaticResource WD.BreadCrumbBar}" TargetType="{x:Type controls:BreadCrumbBar}" />
5986+
<Style x:Key="WD.CropImage" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type controls:CropImage}">
5987+
<Setter Property="Template">
5988+
<Setter.Value>
5989+
<ControlTemplate TargetType="{x:Type controls:CropImage}">
5990+
<Canvas x:Name="PART_Canvas">
5991+
<Rectangle x:Name="PART_RectangleLeft" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
5992+
<Rectangle x:Name="PART_RectangleTop" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
5993+
<Rectangle x:Name="PART_RectangleRight" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
5994+
<Rectangle x:Name="PART_RectangleBottom" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
5995+
<Border x:Name="PART_Border" Background="Transparent" BorderBrush="{DynamicResource WD.PrimaryNormalSolidColorBrush}" BorderThickness="2" Cursor="SizeAll" />
5996+
</Canvas>
5997+
</ControlTemplate>
5998+
</Setter.Value>
5999+
</Setter>
6000+
</Style>
6001+
<Style BasedOn="{StaticResource WD.CropImage}" TargetType="{x:Type controls:CropImage}" />
59966002
<Style x:Key="WD.DatePickerCalendarStyle" BasedOn="{StaticResource {x:Type Calendar}}" TargetType="{x:Type Calendar}" />
59976003
<Style x:Key="WD.DefaultDatePicker" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type DatePicker}">
59986004
<Setter Property="IsTodayHighlighted" Value="True" />
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<UserControl
2+
x:Class="WPFDevelopers.Samples.ExampleViews.CropImageExample"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
10+
d:DesignHeight="450"
11+
d:DesignWidth="800"
12+
mc:Ignorable="d">
13+
<controls:CodeViewer>
14+
<Grid>
15+
<Grid.RowDefinitions>
16+
<RowDefinition Height="*" />
17+
<RowDefinition Height="Auto" />
18+
</Grid.RowDefinitions>
19+
<Grid.ColumnDefinitions>
20+
<ColumnDefinition />
21+
<ColumnDefinition />
22+
</Grid.ColumnDefinitions>
23+
<wd:CropImage
24+
Name="MyCropImage"
25+
Grid.Row="0"
26+
Grid.Column="0" />
27+
<Image
28+
Grid.Column="1"
29+
Width="{Binding CurrentRect.Width, ElementName=MyCropImage}"
30+
Height="{Binding CurrentRect.Height, ElementName=MyCropImage}"
31+
VerticalAlignment="Center"
32+
Source="{Binding CurrentAreaBitmap, ElementName=MyCropImage}"
33+
Stretch="Uniform" />
34+
<StackPanel
35+
Grid.Row="1"
36+
Grid.ColumnSpan="2"
37+
HorizontalAlignment="Center"
38+
Orientation="Horizontal">
39+
<Button
40+
Margin="0,20,10,20"
41+
Click="OnImportClickHandler"
42+
Content="选择图片"
43+
Style="{StaticResource WD.PrimaryButton}" />
44+
<Button
45+
Margin="0,20,10,20"
46+
Click="BtnSave_Click"
47+
Content="保存图片"
48+
Style="{StaticResource WD.SuccessPrimaryButton}" />
49+
</StackPanel>
50+
</Grid>
51+
<controls:CodeViewer.SourceCodes>
52+
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/CropImageExample.xaml" CodeType="Xaml" />
53+
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/CropImageExample.xaml.cs" CodeType="CSharp" />
54+
</controls:CodeViewer.SourceCodes>
55+
</controls:CodeViewer>
56+
</UserControl>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Microsoft.Win32;
2+
using System;
3+
using System.IO;
4+
using System.Windows;
5+
using System.Windows.Controls;
6+
using System.Windows.Media.Imaging;
7+
8+
namespace WPFDevelopers.Samples.ExampleViews
9+
{
10+
public partial class CropImageExample : UserControl
11+
{
12+
public CropImageExample()
13+
{
14+
InitializeComponent();
15+
}
16+
double ConvertBytesToMB(long bytes)
17+
{
18+
return (double)bytes / (1024 * 1024);
19+
}
20+
private void OnImportClickHandler(object sender, RoutedEventArgs e)
21+
{
22+
var openFileDialog = new OpenFileDialog();
23+
openFileDialog.Filter = "图像文件(*.jpg;*.jpeg;*.png;)|*.jpg;*.jpeg;*.png;";
24+
if (openFileDialog.ShowDialog() == true)
25+
{
26+
var fileInfo = new FileInfo(openFileDialog.FileName);
27+
var fileSize = fileInfo.Length;
28+
var mb = ConvertBytesToMB(fileSize);
29+
if (mb > 1)
30+
{
31+
WPFDevelopers.Controls.MessageBox.Show("图片不能大于 1M ", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
32+
return;
33+
}
34+
var bitmap = new BitmapImage();
35+
bitmap.BeginInit();
36+
bitmap.CacheOption = BitmapCacheOption.OnLoad;
37+
bitmap.UriSource = new Uri(openFileDialog.FileName, UriKind.Absolute);
38+
bitmap.EndInit();
39+
40+
if (bitmap.PixelWidth > 500 || bitmap.PixelHeight > 500)
41+
{
42+
var width = (int)(bitmap.PixelWidth * 0.5);
43+
var height = (int)(bitmap.PixelHeight * 0.5);
44+
var croppedBitmap = new CroppedBitmap(bitmap, new Int32Rect(0, 0, width, height));
45+
var bitmapNew = new BitmapImage();
46+
bitmapNew.BeginInit();
47+
bitmapNew.DecodePixelWidth = width;
48+
bitmapNew.DecodePixelHeight = height;
49+
var memoryStream = new MemoryStream();
50+
var encoder = new JpegBitmapEncoder();
51+
encoder.Frames.Add(BitmapFrame.Create(croppedBitmap.Source));
52+
encoder.Save(memoryStream);
53+
memoryStream.Seek(0, SeekOrigin.Begin);
54+
bitmapNew.StreamSource = memoryStream;
55+
bitmapNew.EndInit();
56+
MyCropImage.Source = bitmapNew;
57+
}
58+
else
59+
{
60+
MyCropImage.Source = bitmap;
61+
}
62+
}
63+
}
64+
private void BtnSave_Click(object sender, RoutedEventArgs e)
65+
{
66+
var dlg = new SaveFileDialog();
67+
dlg.FileName = $"WPFDevelopers_CropImage_{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg";
68+
dlg.DefaultExt = ".jpg";
69+
dlg.Filter = "image file|*.jpg";
70+
if (dlg.ShowDialog() == true)
71+
{
72+
var pngEncoder = new PngBitmapEncoder();
73+
pngEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)MyCropImage.CurrentAreaBitmap));
74+
using (var fs = File.OpenWrite(dlg.FileName))
75+
{
76+
pngEncoder.Save(fs);
77+
fs.Dispose();
78+
fs.Close();
79+
}
80+
}
81+
82+
}
83+
}
84+
}

src/WPFDevelopers.Samples.Shared/Helpers/MenuEnum.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public enum MenuEnum
7171
Selector,
7272
MultiSelectSearchComboBox,
7373
BreadCrumbBar,
74+
CropImage,
7475
VirtualizingWrapPanel,
7576
AcrylicBlur,
7677
TaskbarInfo

src/WPFDevelopers.Samples.Shared/ViewModels/MainVM.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ namespace WPFDevelopers.Samples.ViewModels
2020
{
2121
public class MainVM:ViewModelBase
2222
{
23-
//private ObservableCollection<NavigateMenuModel> _navigateMenuModelList;
24-
25-
//public ObservableCollection<NavigateMenuModel> NavigateMenuModelList
26-
//{
27-
// get { return _navigateMenuModelList; }
28-
// set { _navigateMenuModelList = value; }
29-
//}
30-
3123
private ObservableCollection<ListBoxItem> _navigateMenuModelList;
3224

3325
public ObservableCollection<ListBoxItem> NavigateMenuModelList
@@ -64,13 +56,6 @@ public object ControlPanel
6456
}
6557
public MainVM()
6658
{
67-
//NavigateMenuModelList = new ObservableCollection<NavigateMenuModel>();
68-
//foreach (MenuEnum menuEnum in Enum.GetValues(typeof(MenuEnum)))
69-
//{
70-
// NavigateMenuModelList.Add(new NavigateMenuModel { Name = menuEnum.ToString() });
71-
//}
72-
//NavigateMenuModelList.Add(new NavigateMenuModel { Name = "持续更新中" });
73-
7459
NavigateMenuModelList = new ObservableCollection<ListBoxItem>();
7560
foreach (MenuEnum menuEnum in Enum.GetValues(typeof(MenuEnum)))
7661
{
@@ -87,15 +72,12 @@ public MainVM()
8772
public ICommand MenuSelectionChangedCommand => new RelayCommand(obj =>
8873
{
8974
if (obj == null) return;
90-
//var model = obj as NavigateMenuModel;
91-
//MenuItemSelection(model.Name);
9275
var model = obj as ListBoxItem;
9376
MenuItemSelection(model.Content.ToString());
9477
});
9578

9679
public ICommand CloseCommand => new RelayCommand( obj =>
9780
{
98-
//Environment.Exit(0);
9981
Application.Current.MainWindow.Close();
10082
});
10183

@@ -309,6 +291,9 @@ void MenuItemSelection(string _menuName)
309291
case MenuEnum.BreadCrumbBar:
310292
ControlPanel = new BreadCrumbBarExample();
311293
break;
294+
case MenuEnum.CropImage:
295+
ControlPanel = new CropImageExample();
296+
break;
312297
case MenuEnum.VirtualizingWrapPanel:
313298
ControlPanel = new VirtualizingWrapPanel();
314299
new VirtualizingWrapPanelExample().MaskShowDialog();

src/WPFDevelopers.Samples.Shared/WPFDevelopers.Samples.Shared.projitems

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@
134134
<SubType>Code</SubType>
135135
<DependentUpon>CutImageExample.xaml</DependentUpon>
136136
</Compile>
137+
<Compile Include="$(MSBuildThisFileDirectory)ExampleViews\CropImageExample.xaml.cs">
138+
<SubType>Code</SubType>
139+
<DependentUpon>CropImageExample.xaml</DependentUpon>
140+
</Compile>
137141
<Compile Include="$(MSBuildThisFileDirectory)ExampleViews\DashboardExample.xaml.cs">
138142
<SubType>Code</SubType>
139143
<DependentUpon>DashboardExample.xaml</DependentUpon>
@@ -549,6 +553,10 @@
549553
<Generator>MSBuild:Compile</Generator>
550554
<SubType>Designer</SubType>
551555
</Page>
556+
<Page Include="$(MSBuildThisFileDirectory)ExampleViews\CropImageExample.xaml">
557+
<Generator>MSBuild:Compile</Generator>
558+
<SubType>Designer</SubType>
559+
</Page>
552560
<Page Include="$(MSBuildThisFileDirectory)ExampleViews\CutImageExample.xaml">
553561
<Generator>MSBuild:Compile</Generator>
554562
<SubType>Designer</SubType>

src/WPFDevelopers.Samples40/WPFDevelopers.Samples40.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<Install>false</Install>
3232
</BootstrapperPackage>
3333
</ItemGroup>
34+
<ItemGroup>
35+
<PackageReference Include="WPFDevelopers" Version="1.1.0.1-preview8" />
36+
</ItemGroup>
3437
<ItemGroup>
3538
<Resource Include="..\WPFDevelopers.Samples.Shared\WPFDevelopers.ico">
3639
<Link>WPFDevelopers.ico</Link>
@@ -172,7 +175,6 @@
172175
</Resource>
173176
</ItemGroup>
174177
<ItemGroup>
175-
<ProjectReference Include="..\WPFDevelopers.Net40\WPFDevelopers.Net40.csproj" />
176178
<ProjectReference Include="..\WPFDevelopers.SamplesCode\WPFDevelopers.SamplesCode.csproj" />
177179
</ItemGroup>
178180
<PropertyGroup>

src/WPFDevelopers.SamplesCode/WPFDevelopers.SamplesCode.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ElementAdorner\ElementAdorner.cs" Link="Controls\ElementAdorner\ElementAdorner.cs" />
131131
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ElementAdorner\ThumbAngle.cs" Link="Controls\ElementAdorner\ThumbAngle.cs" />
132132
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CropControl\CropControl.cs" Link="Controls\CropControl\CropControl.cs" />
133+
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\CropImageExample.xaml.cs" Link="ExampleViews\CropImageExample.xaml.cs" DependentUpon="CropImageExample.xaml" />
133134
</ItemGroup>
134135
<ItemGroup>
135136
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\AboutWindow.xaml">
@@ -435,8 +436,8 @@
435436
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CropControl\CropControl.xaml">
436437
<Link>Controls\CropControl\CropControl.xaml</Link>
437438
</Resource>
438-
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CropControl\CropControl.xaml">
439-
<Link>Controls\CropControl\CropControl.xaml</Link>
439+
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\CropImageExample.xaml">
440+
<Link>ExampleViews\CropImageExample.xaml</Link>
440441
</Resource>
441442
</ItemGroup>
442443
</Project>

0 commit comments

Comments
 (0)