Skip to content

add in CropImage #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/WPFDevelopers.Net40/Themes/Theme.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5982,6 +5982,22 @@
</Setter>
</Style>
<Style BasedOn="{StaticResource WD.BreadCrumbBar}" TargetType="{x:Type controls:BreadCrumbBar}" />
<Style x:Key="WD.CropImage" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type controls:CropImage}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:CropImage}">
<Canvas x:Name="PART_Canvas">
<Rectangle x:Name="PART_RectangleLeft" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
<Rectangle x:Name="PART_RectangleTop" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
<Rectangle x:Name="PART_RectangleRight" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
<Rectangle x:Name="PART_RectangleBottom" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
<Border x:Name="PART_Border" Background="Transparent" BorderBrush="{DynamicResource WD.PrimaryNormalSolidColorBrush}" BorderThickness="2" Cursor="SizeAll" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style BasedOn="{StaticResource WD.CropImage}" TargetType="{x:Type controls:CropImage}" />
<Style x:Key="WD.DatePickerCalendarStyle" BasedOn="{StaticResource {x:Type Calendar}}" TargetType="{x:Type Calendar}" />
<Style x:Key="WD.DefaultDatePicker" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type DatePicker}">
<Setter Property="IsTodayHighlighted" Value="True" />
Expand Down
26 changes: 16 additions & 10 deletions src/WPFDevelopers.Net45x/Themes/Theme.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4415,16 +4415,6 @@
</Setter>
</Style>
<Style BasedOn="{StaticResource WD.CircularProgressBar}" TargetType="{x:Type controls:CircularProgressBar}" />
<Style x:Key="WD.CropControl" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type controls:CropControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:CropControl}">
<UniformGrid x:Name="PART_UniformGrid" Columns="{TemplateBinding RowColumn}" Rows="{TemplateBinding RowColumn}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style BasedOn="{StaticResource WD.CropControl}" TargetType="{x:Type controls:CropControl}" />
<Style x:Key="WD.EdgeLight" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type controls:EdgeLight}">
<Setter Property="BorderBrush" Value="{DynamicResource WD.PrimaryNormalSolidColorBrush}" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
Expand Down Expand Up @@ -5993,6 +5983,22 @@
</Setter>
</Style>
<Style BasedOn="{StaticResource WD.BreadCrumbBar}" TargetType="{x:Type controls:BreadCrumbBar}" />
<Style x:Key="WD.CropImage" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type controls:CropImage}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:CropImage}">
<Canvas x:Name="PART_Canvas">
<Rectangle x:Name="PART_RectangleLeft" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
<Rectangle x:Name="PART_RectangleTop" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
<Rectangle x:Name="PART_RectangleRight" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
<Rectangle x:Name="PART_RectangleBottom" Style="{DynamicResource WD.ScreenCutRectangleStyle}" />
<Border x:Name="PART_Border" Background="Transparent" BorderBrush="{DynamicResource WD.PrimaryNormalSolidColorBrush}" BorderThickness="2" Cursor="SizeAll" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style BasedOn="{StaticResource WD.CropImage}" TargetType="{x:Type controls:CropImage}" />
<Style x:Key="WD.DatePickerCalendarStyle" BasedOn="{StaticResource {x:Type Calendar}}" TargetType="{x:Type Calendar}" />
<Style x:Key="WD.DefaultDatePicker" BasedOn="{StaticResource WD.ControlBasicStyle}" TargetType="{x:Type DatePicker}">
<Setter Property="IsTodayHighlighted" Value="True" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<UserControl
x:Class="WPFDevelopers.Samples.ExampleViews.CropImageExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<controls:CodeViewer>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<wd:CropImage
Name="MyCropImage"
Grid.Row="0"
Grid.Column="0" />
<Image
Grid.Column="1"
Width="{Binding CurrentRect.Width, ElementName=MyCropImage}"
Height="{Binding CurrentRect.Height, ElementName=MyCropImage}"
VerticalAlignment="Center"
Source="{Binding CurrentAreaBitmap, ElementName=MyCropImage}"
Stretch="Uniform" />
<StackPanel
Grid.Row="1"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button
Margin="0,20,10,20"
Click="OnImportClickHandler"
Content="选择图片"
Style="{StaticResource WD.PrimaryButton}" />
<Button
Margin="0,20,10,20"
Click="BtnSave_Click"
Content="保存图片"
Style="{StaticResource WD.SuccessPrimaryButton}" />
</StackPanel>
</Grid>
<controls:CodeViewer.SourceCodes>
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/CropImageExample.xaml" CodeType="Xaml" />
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/CropImageExample.xaml.cs" CodeType="CSharp" />
</controls:CodeViewer.SourceCodes>
</controls:CodeViewer>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace WPFDevelopers.Samples.ExampleViews
{
public partial class CropImageExample : UserControl
{
public CropImageExample()
{
InitializeComponent();
}
double ConvertBytesToMB(long bytes)
{
return (double)bytes / (1024 * 1024);
}
private void OnImportClickHandler(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图像文件(*.jpg;*.jpeg;*.png;)|*.jpg;*.jpeg;*.png;";
if (openFileDialog.ShowDialog() == true)
{
var fileInfo = new FileInfo(openFileDialog.FileName);
var fileSize = fileInfo.Length;
var mb = ConvertBytesToMB(fileSize);
if (mb > 1)
{
WPFDevelopers.Controls.MessageBox.Show("图片不能大于 1M ", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = new Uri(openFileDialog.FileName, UriKind.Absolute);
bitmap.EndInit();

if (bitmap.PixelWidth > 500 || bitmap.PixelHeight > 500)
{
var width = (int)(bitmap.PixelWidth * 0.5);
var height = (int)(bitmap.PixelHeight * 0.5);
var croppedBitmap = new CroppedBitmap(bitmap, new Int32Rect(0, 0, width, height));
var bitmapNew = new BitmapImage();
bitmapNew.BeginInit();
bitmapNew.DecodePixelWidth = width;
bitmapNew.DecodePixelHeight = height;
var memoryStream = new MemoryStream();
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(croppedBitmap.Source));
encoder.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
bitmapNew.StreamSource = memoryStream;
bitmapNew.EndInit();
MyCropImage.Source = bitmapNew;
}
else
{
MyCropImage.Source = bitmap;
}
}
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
var dlg = new SaveFileDialog();
dlg.FileName = $"WPFDevelopers_CropImage_{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg";
dlg.DefaultExt = ".jpg";
dlg.Filter = "image file|*.jpg";
if (dlg.ShowDialog() == true)
{
var pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create((BitmapSource)MyCropImage.CurrentAreaBitmap));
using (var fs = File.OpenWrite(dlg.FileName))
{
pngEncoder.Save(fs);
fs.Dispose();
fs.Close();
}
}

}
}
}
1 change: 1 addition & 0 deletions src/WPFDevelopers.Samples.Shared/Helpers/MenuEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public enum MenuEnum
Selector,
MultiSelectSearchComboBox,
BreadCrumbBar,
CropImage,
VirtualizingWrapPanel,
AcrylicBlur,
TaskbarInfo
Expand Down
21 changes: 3 additions & 18 deletions src/WPFDevelopers.Samples.Shared/ViewModels/MainVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ namespace WPFDevelopers.Samples.ViewModels
{
public class MainVM:ViewModelBase
{
//private ObservableCollection<NavigateMenuModel> _navigateMenuModelList;

//public ObservableCollection<NavigateMenuModel> NavigateMenuModelList
//{
// get { return _navigateMenuModelList; }
// set { _navigateMenuModelList = value; }
//}

private ObservableCollection<ListBoxItem> _navigateMenuModelList;

public ObservableCollection<ListBoxItem> NavigateMenuModelList
Expand Down Expand Up @@ -64,13 +56,6 @@ public object ControlPanel
}
public MainVM()
{
//NavigateMenuModelList = new ObservableCollection<NavigateMenuModel>();
//foreach (MenuEnum menuEnum in Enum.GetValues(typeof(MenuEnum)))
//{
// NavigateMenuModelList.Add(new NavigateMenuModel { Name = menuEnum.ToString() });
//}
//NavigateMenuModelList.Add(new NavigateMenuModel { Name = "持续更新中" });

NavigateMenuModelList = new ObservableCollection<ListBoxItem>();
foreach (MenuEnum menuEnum in Enum.GetValues(typeof(MenuEnum)))
{
Expand All @@ -87,15 +72,12 @@ public MainVM()
public ICommand MenuSelectionChangedCommand => new RelayCommand(obj =>
{
if (obj == null) return;
//var model = obj as NavigateMenuModel;
//MenuItemSelection(model.Name);
var model = obj as ListBoxItem;
MenuItemSelection(model.Content.ToString());
});

public ICommand CloseCommand => new RelayCommand( obj =>
{
//Environment.Exit(0);
Application.Current.MainWindow.Close();
});

Expand Down Expand Up @@ -309,6 +291,9 @@ void MenuItemSelection(string _menuName)
case MenuEnum.BreadCrumbBar:
ControlPanel = new BreadCrumbBarExample();
break;
case MenuEnum.CropImage:
ControlPanel = new CropImageExample();
break;
case MenuEnum.VirtualizingWrapPanel:
ControlPanel = new VirtualizingWrapPanel();
new VirtualizingWrapPanelExample().MaskShowDialog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
<SubType>Code</SubType>
<DependentUpon>CutImageExample.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)ExampleViews\CropImageExample.xaml.cs">
<SubType>Code</SubType>
<DependentUpon>CropImageExample.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)ExampleViews\DashboardExample.xaml.cs">
<SubType>Code</SubType>
<DependentUpon>DashboardExample.xaml</DependentUpon>
Expand Down Expand Up @@ -549,6 +553,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="$(MSBuildThisFileDirectory)ExampleViews\CropImageExample.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="$(MSBuildThisFileDirectory)ExampleViews\CutImageExample.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
4 changes: 3 additions & 1 deletion src/WPFDevelopers.Samples40/WPFDevelopers.Samples40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<PackageReference Include="WPFDevelopers" Version="1.1.0.1-preview8" />
</ItemGroup>
<ItemGroup>
<Resource Include="..\WPFDevelopers.Samples.Shared\WPFDevelopers.ico">
<Link>WPFDevelopers.ico</Link>
Expand Down Expand Up @@ -172,7 +175,6 @@
</Resource>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WPFDevelopers.Net40\WPFDevelopers.Net40.csproj" />
<ProjectReference Include="..\WPFDevelopers.SamplesCode\WPFDevelopers.SamplesCode.csproj" />
</ItemGroup>
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ElementAdorner\ElementAdorner.cs" Link="Controls\ElementAdorner\ElementAdorner.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ElementAdorner\ThumbAngle.cs" Link="Controls\ElementAdorner\ThumbAngle.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CropControl\CropControl.cs" Link="Controls\CropControl\CropControl.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\CropImageExample.xaml.cs" Link="ExampleViews\CropImageExample.xaml.cs" DependentUpon="CropImageExample.xaml" />
</ItemGroup>
<ItemGroup>
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\AboutWindow.xaml">
Expand Down Expand Up @@ -435,8 +436,8 @@
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CropControl\CropControl.xaml">
<Link>Controls\CropControl\CropControl.xaml</Link>
</Resource>
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CropControl\CropControl.xaml">
<Link>Controls\CropControl\CropControl.xaml</Link>
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\CropImageExample.xaml">
<Link>ExampleViews\CropImageExample.xaml</Link>
</Resource>
</ItemGroup>
</Project>
Loading