内蒙古住房与城乡建设厅网站网址,全面的移动网站建设,北京专业网站制作介绍,工程建设监理名词解释WPF开发者QQ群#xff1a; 340500857由于微信群人数太多入群请添加小编微信号yanjinhuawechat 或 W_Feng_aiQ 邀请入群需备注WPF开发者 PS#xff1a;有更好的方式欢迎推荐。接着上一篇倒计时控件01—代码如下一、创建 CropControl.cs代码如下。#xff08;修改RowColumn “… 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 xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:controlsclr-namespace:WPFDevelopers.ControlsResourceDictionary.MergedDictionariesResourceDictionary SourceBasic/ControlBasic.xaml/ResourceDictionary SourceBasic/Animations.xaml//ResourceDictionary.MergedDictionariesStyle TargetType{x:Type controls:CropControl} BasedOn{StaticResource ControlBasicStyle}Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type controls:CropControl}UniformGrid Rows{TemplateBinding RowColumn} Columns{TemplateBinding RowColumn}x:NamePART_UniformGrid//ControlTemplate/Setter.Value/Setter/Style/ResourceDictionary三、CropControlExample.xaml 代码如下UserControl x:ClassWPFDevelopers.Samples.ExampleViews.CropControlExamplexmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:localclr-namespace:WPFDevelopers.Samples.ExampleViewsxmlns:wpfdevhttps://github.com/yanjinhuagood/WPFDevelopersmc:Ignorabled d:DesignHeight450 d:DesignWidth800Gridwpfdev:CropControl ImageSourcepack://application:,,,/WPFDevelopers.Samples;component/Images/Crop/0.jpg//Grid
/UserControl02—效果预览鸣谢素材提供者 - 关关(代强)源码地址如下Githubhttps://github.com/WPFDevelopersOrgGiteehttps://gitee.com/WPFDevelopersOrgWPF开发者QQ群 340500857 Githubhttps://github.com/WPFDevelopersOrg出处https://www.cnblogs.com/yanjinhua版权本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。转载请著名作者 出处 https://github.com/WPFDevelopersOrg扫一扫关注我们更多知识早知道点击阅读原文可跳转至源代码