WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示

简介: 原文: WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示 为方便描述, 这里仅以正方形来做演示, 其他图形从略。 运行时效果图:XAML代码:// Transform.
原文: WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示

为方便描述, 这里仅以正方形来做演示, 其他图形从略。

运行时效果图:
WPF UIElement Transform

XAML代码:
// Transform.XAML

<Canvas Width="700" Height="700" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Canvas.Resources>
            <DrawingBrush x:Key="MyGridBrushResource" Viewport="0,0,100,100" ViewportUnits="Absolute" TileMode="Tile">
                <DrawingBrush.Drawing>
                    <DrawingGroup>
                        <DrawingGroup.Children>
                            <!--横线-->
                            <GeometryDrawing Geometry="M0,1 L10,1 M0,2 L10,2 M0,3 L10,3 M0,4 L10,4 M0,5 L10,5 M0,6 L10,6 M0,7 L10,7 M0,8 L10,8 M0,9 L10,9">
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="0.1" Brush="#CCCCFF" />
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                            <!-- 横线100整数倍 -->
                            <GeometryDrawing Geometry="M0,0 L10,0">
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="0.1" Brush="DarkOrange" />
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                            <!--竖线-->
                            <GeometryDrawing Geometry="M1,0 L1,10 M2,0 L2,10 M3,0 L3,10 M4,0 L4,10 M5,0 L5,10 M6,0 L6,10 M7,0 L7,10 M8,0 L8,10 M9,0 L9,10">
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="0.1" Brush="#CCCCFF" />
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                            <!-- 竖线100整数倍 -->
                            <GeometryDrawing Geometry="M0,0 L0,10">
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="0.1" Brush="DarkOrange" />
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                        </DrawingGroup.Children>
                    </DrawingGroup>
                </DrawingBrush.Drawing>
            </DrawingBrush>

            <!-- 这里是外框线 -->
            <Style x:Key="MyGridBorderStyle">
                <Setter Property="Border.Background" Value="{StaticResource MyGridBrushResource}"/>
                <Setter Property="Border.HorizontalAlignment" Value="Center"/>
                <Setter Property="Border.VerticalAlignment" Value="Top"/>
                <Setter Property="Border.BorderBrush" Value="Black"/>
                <Setter Property="Border.BorderThickness" Value="1"/>
            </Style>
        </Canvas.Resources>

        <!-- 引用方式 -->
        <Border Style="{StaticResource MyGridBorderStyle}">
            <Canvas Width="700" Height="600">
<!-- // 演示平移 -->
            <Canvas Width="800" Height="100" Canvas.Top="0" Canvas.Left="0">
<!-- 不平移-->
<Canvas Width="120" Height="120"  Canvas.Left="0" Canvas.Top="0">
                <Rectangle Width="80" Height="80" Stroke="Black" />
</Canvas>
<!--水平平移10 -->
<Canvas Width="120" Height="120"  Canvas.Left="100" Canvas.Top="0">
                <Rectangle Width="80" Height="80" Stroke="Black" />
                <Rectangle Width="80" Height="80" Stroke="Red">
                    <Rectangle.RenderTransform>
                           <TranslateTransform X="10" />
                    </Rectangle.RenderTransform>
                </Rectangle>
</Canvas>

<!--垂直平移10 -->
<Canvas Width="120" Height="120"  Canvas.Left="200" Canvas.Top="0">
                <Rectangle Width="80" Height="80" Stroke="Black" />
                <Rectangle Width="80" Height="80" Stroke="Red">
                    <Rectangle.RenderTransform>
                           <TranslateTransform Y="10" />
                    </Rectangle.RenderTransform>
                </Rectangle>
</Canvas>

<!--水平平移10,垂直平移10 -->
<Canvas Width="120" Height="120"  Canvas.Left="300" Canvas.Top="0">
                <Rectangle Width="80" Height="80" Stroke="Black" />
                <Rectangle Width="80" Height="80" Stroke="Red">
                    <Rectangle.RenderTransform>
                           <TranslateTransform X="10" Y="10" />
                    </Rectangle.RenderTransform>
                </Rectangle></Canvas>
       </Canvas>

<!-- // 演示缩放 -->
            <Canvas Width="800" Height="100" Canvas.Top="100" Canvas.Left="0">
<!-- 未缩放时的原始大小-->
<Canvas Width="120" Height="120"  Canvas.Left="0" Canvas.Top="0">
                <Rectangle Width="80" Height="80" Stroke="Black" />
</Canvas>
<!--水平缩放比例:2倍 -->
<Canvas Width="120" Height="120"  Canvas.Left="100" Canvas.Top="0">
                <Rectangle Width="80" Height="80" Stroke="Red">
                    <Rectangle.RenderTransform>
                           <ScaleTransform ScaleX="2" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle Width="80" Height="80" Stroke="Black" />
</Canvas>

<!--垂直缩放比例2.5倍 -->
<Canvas Width="120" Height="120"  Canvas.Left="300" Canvas.Top="0">
                <Rectangle Width="80" Height="80" Stroke="Red">
                    <Rectangle.RenderTransform>
                           <ScaleTransform ScaleY="2.5" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle Width="80" Height="80" Stroke="Black" />
</Canvas>

<!--水平、垂直同时缩放2.5倍 -->
<Canvas Width="120" Height="120"  Canvas.Left="400" Canvas.Top="0">
                <Rectangle Width="80" Height="80" Stroke="Red">
                    <Rectangle.RenderTransform>
                           <ScaleTransform ScaleX="2.5" ScaleY="2.5" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle Width="80" Height="80" Stroke="Black" />
</Canvas>
       </Canvas>


<!-- // 演示旋转 -->
            <Canvas Width="800" Height="100" Canvas.Top="300" Canvas.Left="0">
<!-- 不旋转-->
<Canvas Width="120" Height="120"  Canvas.Left="0" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black" />
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--旋转15度(默认以左上角为旋转中心 -->
<Canvas Width="120" Height="120"  Canvas.Left="100" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <RotateTransform Angle="15" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="-2" Canvas.Left="-2"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--旋转15度,以右上角为旋转中心 -->
<Canvas Width="120" Height="120"  Canvas.Left="200" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <RotateTransform Angle="15" CenterX="80" CenterY="0" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="-2" Canvas.Left="78"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--旋转15度,以左下角为旋转中心 -->
<Canvas Width="120" Height="120"  Canvas.Left="300" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <RotateTransform Angle="15" CenterX="0" CenterY="80" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="78" Canvas.Left="-2"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--旋转15度,以右下角为旋转中心 -->
<Canvas Width="120" Height="120"  Canvas.Left="400" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <RotateTransform Angle="15" CenterX="80" CenterY="80" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="78" Canvas.Left="78"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--旋转15度,以正方形的中心点为旋转中心 -->
<Canvas Width="120" Height="120"  Canvas.Left="500" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <RotateTransform Angle="15" CenterX="40" CenterY="40" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="38" Canvas.Left="38"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
       </Canvas>

<!-- // 演示倾斜 -->
            <Canvas Width="800" Height="100" Canvas.Top="400" Canvas.Left="0">
<!-- 原始图形-->
<Canvas Width="120" Height="120"  Canvas.Left="0" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black" />
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--沿X轴方向倾斜10度(默认以左上角为原点) -->
<Canvas Width="120" Height="120"  Canvas.Left="100" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <SkewTransform AngleX="10" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 倾斜中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="-2" Canvas.Left="-2"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--沿Y轴方向倾斜10度(默认以左上角为原点) -->
<Canvas Width="120" Height="120"  Canvas.Left="200" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <SkewTransform AngleY="10" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="-2" Canvas.Left="-2"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--X轴、Y轴同时倾斜10度(默认以左上角为原点) -->
<Canvas Width="120" Height="120"  Canvas.Left="300" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <SkewTransform AngleX="10" AngleY="10" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="-2" Canvas.Left="-2"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--设定倾斜中心为矩形的正中心位置,沿X轴倾斜10度 -->
<Canvas Width="120" Height="120"  Canvas.Left="400" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <SkewTransform AngleX="10" CenterX="40" CenterY="40" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="38" Canvas.Left="38"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--仍将正方形的中心点作为倾斜中心点,X轴倾斜负10度 -->
<Canvas Width="120" Height="120"  Canvas.Left="500" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <SkewTransform AngleX="-10" CenterX="40" CenterY="40" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="38" Canvas.Left="38"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
<!--X轴、Y轴同时倾斜负10度(默认以左上角为原点) -->
<Canvas Width="120" Height="120"  Canvas.Left="600" Canvas.Top="20">
                <Rectangle Width="80" Height="80" Stroke="Black">
                    <Rectangle.RenderTransform>
                           <SkewTransform AngleX="-10" AngleY="-10" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <!-- 旋转中心点 -->
                <Ellipse Width="4" Height="4" Fill="Red" Canvas.Top="-2" Canvas.Left="-2"  />
                <!-- 矩形中心点 -->
                <Ellipse Width="4" Height="4" Stroke="Black" Canvas.Top="38" Canvas.Left="38"  />
</Canvas>
       </Canvas>

            </Canvas>
        </Border>

</Canvas>

目录
相关文章
|
C# Windows
WPF技术之图形系列Polygon控件
WPF Polygon是Windows Presentation Foundation (WPF)框架中的一个标记元素,用于绘制多边形形状。它可以通过设置多个点的坐标来定义多边形的形状,可以绘制任意复杂度的多边形。
965 0
|
C# 前端开发
WPF - 图形设计器(Diagram Designer)
原文:WPF - 图形设计器(Diagram Designer)   OpenExpressApp计划中包括建模工具,计划是采用MetaEdit+模型来作为元模型,使用codeproject的《WPF Diagram Designer》一系列文章来做为设计器实现参考,本篇介绍一下codeprojcet的这四个文章,推荐给对图形设计器感兴趣的人去看看,通过WPF的模板功能和其他功能可以很方便的设计出图形编辑器。
3601 0
|
5月前
|
C# Windows 开发者
当WPF遇见OpenGL:一场关于如何在Windows Presentation Foundation中融入高性能跨平台图形处理技术的精彩碰撞——详解集成步骤与实战代码示例
【8月更文挑战第31天】本文详细介绍了如何在Windows Presentation Foundation (WPF) 中集成OpenGL,以实现高性能的跨平台图形处理。通过具体示例代码,展示了使用SharpGL库在WPF应用中创建并渲染OpenGL图形的过程,包括开发环境搭建、OpenGL渲染窗口创建及控件集成等关键步骤,帮助开发者更好地理解和应用OpenGL技术。
388 0
|
5月前
|
开发者 C# 容器
【独家揭秘】当WPF邂逅DirectX:看这两个技术如何联手打造令人惊艳的高性能图形渲染体验,从环境搭建到代码实践,一步步教你成为图形编程高手
【8月更文挑战第31天】本文通过代码示例详细介绍了如何在WPF应用中集成DirectX以实现高性能图形渲染。首先创建WPF项目并使用SharpDX作为桥梁,然后在XAML中定义承载DirectX内容的容器。接着,通过C#代码初始化DirectX环境,设置渲染逻辑,并在WPF窗口中绘制图形。此方法适用于从简单2D到复杂3D场景的各种图形处理需求,为WPF开发者提供了高性能图形渲染的技术支持和实践指导。
334 0
|
5月前
|
API C# 开发者
WPF图形绘制大师指南:GDI+与Direct2D完美融合,带你玩转高性能图形处理秘籍!
【8月更文挑战第31天】GDI+与Direct2D的结合为WPF图形绘制提供了强大的工具集。通过合理地使用这两种技术,开发者可以创造出性能优异且视觉效果丰富的WPF应用程序。在实际应用中,开发者应根据项目需求和技术背景,权衡利弊,选择最合适的技术方案。
269 0
|
5月前
|
存储 搜索推荐 C#
WPF/C#:让绘制的图形可以被选中并将信息显示在ListBox中
WPF/C#:让绘制的图形可以被选中并将信息显示在ListBox中
55 0
|
5月前
|
前端开发 C# Windows
WPF基础:在Canvas上绘制图形
WPF基础:在Canvas上绘制图形
161 0
|
8月前
|
C#
WPF —— 动画缩放变换
`ScaleTransform`用于二维x-y坐标系中对象的缩放,可沿X或Y轴调整。在故事板中,通过RenderTransform.ScaleX和ScaleY属性控制缩放。示例代码展示了如何设置按钮的RenderTransformOrigin、Background等属性,并通过LayoutTransform应用ScaleTransform。当鼠标进入按钮时,EventTrigger启动DoubleAnimation实现X和Y轴的缩放动画。最后,展示了如何将动画集成到自定义按钮样式中。
199 0
|
C# 开发者 Windows
WPF技术之图形系列Path控件
WPF Path是Windows Presentation Foundation (WPF)框架中的一个标记元素,用于绘制复杂的几何路径形状。它可以通过设置一系列的路径命令以及相应的参数来定义形状,可以绘制任意复杂度的路径。
1180 0
|
前端开发 C# Windows
WPF技术之图形系列Rectangle控件
WPF Rectangle是Windows Presentation Foundation (WPF)中的一个图形控件,用于在界面上绘制矩形。它是一个非常常见的UI元素,可用于显示、布局和交互。
1268 0