WPF 实现图片切成九宫格控件~
生活随笔
收集整理的這篇文章主要介紹了
WPF 实现图片切成九宫格控件~
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WPF開發者QQ群:?340500857
?? ??? 由于微信群人數太多入群請添加小編微信號
?yanjinhuawechat 或 W_Feng_aiQ?邀請入群
?需備注WPF開發者?
? PS:有更好的方式歡迎推薦。
? 接著上一篇倒計時控件
01
—
代碼如下
一、創建?CropControl.cs代碼如下。
(修改RowColumn =“6” 或者“12”? 甚至其他 都能拆分原圖為
多張小圖)
using?System; using?System.Windows; using?System.Windows.Controls; using?System.Windows.Controls.Primitives; using?System.Windows.Input; using?System.Windows.Media; using?System.Windows.Media.Animation; using?System.Windows.Media.Imaging; using?System.Windows.Shapes;namespace?WPFDevelopers.Controls {[TemplatePart(Name?=?UniformGridTemplateName,?Type?=?typeof(UniformGrid))]public?class?CropControl:?Control{private?const?string?UniformGridTemplateName?=?"PART_UniformGrid";private?UniformGrid?_uniformGrid;public?ImageSource?ImageSource{get?{?return?(ImageSource)GetValue(ImageSourceProperty);?}set?{?SetValue(ImageSourceProperty,?value);?}}public?static?readonly?DependencyProperty?ImageSourceProperty?=DependencyProperty.Register("ImageSource",?typeof(ImageSource),?typeof(CropControl),?new?PropertyMetadata(null));public?int?RowColumn{get?{?return?(int)GetValue(RowColumnProperty);?}set?{?SetValue(RowColumnProperty,?value);?}}public?static?readonly?DependencyProperty?RowColumnProperty?=DependencyProperty.Register("RowColumn",?typeof(int),?typeof(CropControl),?new?PropertyMetadata(3));static?CropControl(){DefaultStyleKeyProperty.OverrideMetadata(typeof(CropControl),?new?FrameworkPropertyMetadata(typeof(CropControl)));}public?override?void?OnApplyTemplate(){base.OnApplyTemplate();_uniformGrid?=?GetTemplateChild(UniformGridTemplateName)?as?UniformGrid;if?(ImageSource?==?null?||?_uniformGrid?==?null)?return;BitmapSource?imgSource?=?(BitmapSource)ImageSource;int?w?=?0,?h?=?0;if?(!imgSource.PixelWidth.Equals(0)&&!imgSource.PixelHeight.Equals(0)){w?=?imgSource.PixelWidth?/?RowColumn;h?=?(int)imgSource.PixelHeight?/?RowColumn;_uniformGrid.Width?=?imgSource.PixelWidth;_uniformGrid.Height?=?imgSource.PixelHeight;}for?(int?i?=?0;?i?<?RowColumn;?i++){for?(int?j?=?0;?j?<?RowColumn;?j++){var?rect?=?new?Rectangle{Fill?=?new?ImageBrush?{?ImageSource?=?new?CroppedBitmap(imgSource,?new?Int32Rect(j?*?w,?i?*?h,?w,?h))?},StrokeThickness?=?.5,Stroke?=?Brushes.White,Cursor?=?Cursors.Hand};rect.RenderTransformOrigin?=?new?Point(.5,?.5);rect.RenderTransform?=?new?ScaleTransform();rect.MouseMove?+=?(sender,?ex)?=>{var?rect1?=?sender?as?Rectangle;Panel.SetZIndex(rect1,?1);var?doubleAnimation?=?new?DoubleAnimation{To?=?2,Duration?=?TimeSpan.FromMilliseconds(100),};var?scaleTransform?=?rect1.RenderTransform?as?ScaleTransform;scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty,?doubleAnimation);scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty,?doubleAnimation);};rect.MouseLeave?+=?(sender,?ex)?=>{var?rect1?=?sender?as?Rectangle;Panel.SetZIndex(rect1,?0);var?scaleTransform?=?rect1.RenderTransform?as?ScaleTransform;var?doubleAnimation?=?new?DoubleAnimation{To?=?1,Duration?=?TimeSpan.FromMilliseconds(100)};scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty,?doubleAnimation);scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty,?doubleAnimation);};_uniformGrid.Children.Add(rect);}}}} }二、CropControl.xaml?代碼如下
<ResourceDictionary?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:controls="clr-namespace:WPFDevelopers.Controls"><ResourceDictionary.MergedDictionaries><ResourceDictionary?Source="Basic/ControlBasic.xaml"/><ResourceDictionary?Source="Basic/Animations.xaml"/></ResourceDictionary.MergedDictionaries><Style?TargetType="{x:Type?controls:CropControl}"?BasedOn="{StaticResource?ControlBasicStyle}"><Setter?Property="Template"><Setter.Value><ControlTemplate?TargetType="{x:Type?controls:CropControl}"><UniformGrid?Rows="{TemplateBinding?RowColumn}"?Columns="{TemplateBinding?RowColumn}"x:Name="PART_UniformGrid"/></ControlTemplate></Setter.Value></Setter></Style></ResourceDictionary>三、CropControlExample.xaml?代碼如下
<UserControl?x:Class="WPFDevelopers.Samples.ExampleViews.CropControlExample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"?xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"xmlns:wpfdev="https://github.com/yanjinhuagood/WPFDevelopers"mc:Ignorable="d"?d:DesignHeight="450"?d:DesignWidth="800"><Grid><wpfdev:CropControl?ImageSource="pack://application:,,,/WPFDevelopers.Samples;component/Images/Crop/0.jpg"/></Grid> </UserControl>02
—
效果預覽
鳴謝素材提供者 -?關關(代強)
源碼地址如下
Github:https://github.com/WPFDevelopersOrg
Gitee:https://gitee.com/WPFDevelopersOrg
WPF開發者QQ群:?340500857?
Github:https://github.com/WPFDevelopersOrg
出處:https://www.cnblogs.com/yanjinhua
版權:本作品采用「署名-非商業性使用-相同方式共享 4.0 國際」許可協議進行許可。
轉載請著名作者 出處 https://github.com/WPFDevelopersOrg
掃一掃關注我們,
更多知識早知道!
點擊閱讀原文可跳轉至源代碼
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的WPF 实现图片切成九宫格控件~的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何定位Source Generator
- 下一篇: 只要300页!火遍全网的NET6+lin