WPF 实现图片切成九宫格控件~

WPF开发者QQ群: 340500857

       由于微信群人数太多入群请添加小编微信号

 yanjinhuawechat 或 W_Feng_aiQ 入群

 需备注WPF开发者 

06125f24017a831aa612cbbbe3792f91.png

  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

1cefccee530e4aa5a33e162c5f8c8b07.png

扫一扫关注我们,

3f5d480bab84c173b01540ad3eaeed90.gif

更多知识早知道!

3b3b29620014c9bdf2b6a71f202870ba.gif

点击阅读原文可跳转至源代码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/292609.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用Visio进行UML建模

资料地址&#xff1a;使用Visio进行UML建模-http://www.qdgw.edu.cn/zhuantiweb/jpkc/2009/rjkf/xmwd/Visio_UmlModel.htm转载于:https://www.cnblogs.com/svennee/p/4075700.html

GCT之数学公式(三角函数)

转载于:https://www.cnblogs.com/jyh317/p/3386598.html

C++基础知识总结(一)

C程序以.cpp作为文件扩展名。 C标准输入输出库的头文件是iostream&#xff0c;在程序中使用&#xff1a;#include <iostream>//包含头文件 C使用命名空间&#xff1a;using namespace std; 对象的定义及初始化&#xff1a;int z(0)//等价int z0&#xff1b; 用关键字cons…

Android安全与逆向之Java虚拟机和Dalvik虚拟机的区别

Google于2007年底正式发布了Android SDK, 作为 Android系统的重要特性&#xff0c;Dalvik虚拟机也第一次进入了人们的视野。它对内存的高效使用&#xff0c;和在低速CPU上表现出的高性能&#xff0c;确实令人刮目相看。 依赖于底层Posix兼容的操作系统&#xff0c;它可以简单的…

软件与硬件我该选哪个_MacBook Air 和 MacBook Pro 该选哪个……?

一个多月前&#xff0c;Apple 发布了 2019 款 MacBook Air 和 MacBook Pro&#xff0c;经过这一次的更新之后&#xff0c;MacBook 产品线总算变得更加清晰了&#xff0c;同时也减少了之前存在的价格和定位产生冲突的现象。不过从购买决策来看&#xff0c;我发现还是有些问题难倒…

只要300页!火遍全网的NET6+linux知识手册!拿走不谢!

这是微软公司基于最新的.net 6编写&#xff0c;循序渐进地对.net6/C#10进行讲解。对于零基础可以作为.net的快速入门教材&#xff0c;对于高级程序员而言&#xff0c;这也是你的进阶之路&#xff01;NO.1资料介绍该手册,全面的介绍.net6和c#的新特性&#xff0c;看完这个资料&a…

Linux0.11内核剖析--内核体系结构

一个完整可用的操作系统主要由 4 部分组成&#xff1a;硬件、操作系统内核、操作系统服务和用户应用程序&#xff0c;如下图所示&#xff1a; 用户应用程序是指那些字处理程序、 Internet 浏览器程序或用户自行编制的各种应用程序&#xff1b; 操作系统服务程序是指那些向用户所…

centos 6安装报错

Missing ISO 9660 imageThe installer has tried to mount image # 1. but cannot find it on the hard drive.Please copy this image to the drive and click Retry. Click Exit to abort the installation. 这个是安装过程中你没有把iso镜像拷贝到你的U盘所导致&#xff01;…

ubuntu上最使用jni最简单易懂的例子

第一步:爆结果照,让你有坚持下去的信心 二、NDK解释 NDK全称:Native Development Kit。 NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk。这些工具对开发者的帮助是巨大的。 NDK集成了交叉编译器,并提供了相应的m…

buck变换器设计matlab_一种用于Boost PFC变换器的改进关断时间控制策略

随着我国工业的发展&#xff0c;越来越多的非线性负荷接入电网&#xff0c;各种换流设备的使用&#xff0c;使电网的电压波形发生畸变&#xff0c;造成电能质量下降&#xff0c;威胁电网和各种用电设备的安全、经济运行。为了能够确保电网安全&#xff0c;研制出了各种PFC变换器…

你初吻啥时候没有的​?​

1 &#xff1f;&#xff1f;&#xff1f;&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼2 世界上最难受的三件事&#xff08;via.惨绿少年金三坨&#xff0c;侵删&#xff09;▼3 漏网之鱼▼4 有哪里不太对劲&#xff1f;&#xff08;素材来源网络&#xff0c;侵删…

乡亲们,我们创建了 Dapr 中文交流频道

我们创建了 Dapr 中文交流 QQ 频道&#xff0c;欢迎大家加入&#xff01;加入方式在文章最后一节。为什么要创建频道&#xff1f; 解决什么问题**专业性&#xff0c;“你可以在我们群里面钓鱼&#xff0c;因为都是水”** 你肯定加过非常多的这种叫什么技术交流群。你会发现这个…

Maven搭建SpringMVC+Mybatis项目详解【转】

为什么80%的码农都做不了架构师&#xff1f;>>> 前言 最近比较闲&#xff0c;复习搭建一下项目&#xff0c;这次主要使用SpringSpringMVCMybatis。项目持久层使用Mybatis3&#xff0c;控制层使用SpringMVC4.1&#xff0c;使用Spring4.1管理控制器&#xff0c;数据库…

IOS 代码块

1.关系式表示 <returnType>(^BlockName)(list of arguments)^(arguments){body;}; 转载于:https://www.cnblogs.com/keithmoring/p/4077686.html

web前端url传递值 js加密解密

一&#xff1a;Js的Url中传递中文参数乱码问题&#xff0c;重点&#xff1a;encodeURI编码&#xff0c;decodeURI解码&#xff1a; 1.传参页面Javascript代码&#xff1a; <script type”text/javascript”>// <![CDATA[ function send(){ var url "test01.html…

Linux下最简单的修改文件名后缀的命令行技巧

在linux上面一直没有找到好的修改文件的方法&#xff0c;今天找到了一个 mv file.{cpp,c} 上面的意思就是说把文件file结尾为cpp改成c文件格式 看下图 &#xfeff;&#xfeff;

python123可以复制粘贴吗_使用win32com和python复制和粘贴隔离

有没有办法使用python和win32com进行复制和粘贴,以便python脚本可以在后台运行而不会弄乱“用户”复制和粘贴功能&#xff1f; from win32com.client import Dispatch import win32com.client xlApp Dispatch("Excel.Application") xlWb xlApp.Workbooks.Open(file…

mongoDB 高级查询之取模查询$mod

http://hancang2000.i.sohu.com/blog/view/235140698.htm$mod取模运算查询age取模10等于0的数据db.student.find( { age: { $mod : [ 10 , 1 ] } } )举例如下:C1表的数据如下:> db.c1.find(){ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age…