방법: 펜 정의
이 예제는 Pen을 사용하여 도형의 윤곽선을 지정하는 방법을 보여 줍니다. 간단한 Pen을 만들려면 Thickness와 Brush만 지정하면 됩니다. DashStyle, DashCap, LineJoin, StartLineCap, EndLineCap을 지정하면 보다 복잡한 펜을 만들 수 있습니다.
예제
다음 예제는 Pen을 사용하여 GeometryDrawing에 정의된 도형의 윤곽선을 지정합니다.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace SDKSample
{
public partial class PenExample : Page
{
public PenExample()
{
// Create several geometries.
RectangleGeometry myRectangleGeometry = new RectangleGeometry();
myRectangleGeometry.Rect = new Rect(0, 0, 50, 50);
EllipseGeometry myEllipseGeometry = new EllipseGeometry();
myEllipseGeometry.Center = new Point(75, 75);
myEllipseGeometry.RadiusX = 50;
myEllipseGeometry.RadiusY = 50;
LineGeometry myLineGeometry = new LineGeometry();
myLineGeometry.StartPoint = new Point(75, 75);
myLineGeometry.EndPoint = new Point(75, 0);
// Create a GeometryGroup and add the geometries to it.
GeometryGroup myGeometryGroup = new GeometryGroup();
myGeometryGroup.Children.Add(myRectangleGeometry);
myGeometryGroup.Children.Add(myEllipseGeometry);
myGeometryGroup.Children.Add(myLineGeometry);
// Create a GeometryDrawing and use the GeometryGroup to specify
// its geometry.
GeometryDrawing myGeometryDrawing = new GeometryDrawing();
myGeometryDrawing.Geometry = myGeometryGroup;
// Add the GeometryDrawing to a DrawingGroup.
DrawingGroup myDrawingGroup = new DrawingGroup();
myDrawingGroup.Children.Add(myGeometryDrawing);
// Create a Pen to add to the GeometryDrawing created above.
Pen myPen = new Pen();
myPen.Thickness = 10;
myPen.LineJoin = PenLineJoin.Round;
myPen.EndLineCap = PenLineCap.Round;
// Create a gradient to use as a value for the Pen's Brush property.
GradientStop firstStop = new GradientStop();
firstStop.Offset = 0.0;
Color c1 = new Color();
c1.A = 255;
c1.R = 204;
c1.G = 204;
c1.B = 255;
firstStop.Color = c1;
GradientStop secondStop = new GradientStop();
secondStop.Offset = 1.0;
secondStop.Color = Colors.Purple;
LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
myLinearGradientBrush.GradientStops.Add(firstStop);
myLinearGradientBrush.GradientStops.Add(secondStop);
myPen.Brush = myLinearGradientBrush;
myGeometryDrawing.Pen = myPen;
// Create an Image and set its DrawingImage to the Geometry created above.
Image myImage = new Image();
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(10);
DrawingImage myDrawingImage = new DrawingImage();
myDrawingImage.Drawing = myDrawingGroup;
myImage.Source = myDrawingImage;
this.Content = myImage;
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Namespace SDKSample
Partial Public Class PenExample
Inherits Page
Public Sub New()
' Create several geometries.
Dim myRectangleGeometry As New RectangleGeometry()
myRectangleGeometry.Rect = New Rect(0, 0, 50, 50)
Dim myEllipseGeometry As New EllipseGeometry()
myEllipseGeometry.Center = New Point(75, 75)
myEllipseGeometry.RadiusX = 50
myEllipseGeometry.RadiusY = 50
Dim myLineGeometry As New LineGeometry()
myLineGeometry.StartPoint = New Point(75, 75)
myLineGeometry.EndPoint = New Point(75, 0)
' Create a GeometryGroup and add the geometries to it.
Dim myGeometryGroup As New GeometryGroup()
myGeometryGroup.Children.Add(myRectangleGeometry)
myGeometryGroup.Children.Add(myEllipseGeometry)
myGeometryGroup.Children.Add(myLineGeometry)
' Create a GeometryDrawing and use the GeometryGroup to specify
' its geometry.
Dim myGeometryDrawing As New GeometryDrawing()
myGeometryDrawing.Geometry = myGeometryGroup
' Add the GeometryDrawing to a DrawingGroup.
Dim myDrawingGroup As New DrawingGroup()
myDrawingGroup.Children.Add(myGeometryDrawing)
' Create a Pen to add to the GeometryDrawing created above.
Dim myPen As New Pen()
myPen.Thickness = 10
myPen.LineJoin = PenLineJoin.Round
myPen.EndLineCap = PenLineCap.Round
' Create a gradient to use as a value for the Pen's Brush property.
Dim firstStop As New GradientStop()
firstStop.Offset = 0.0
Dim c1 As New Color()
c1.A = 255
c1.R = 204
c1.G = 204
c1.B = 255
firstStop.Color = c1
Dim secondStop As New GradientStop()
secondStop.Offset = 1.0
secondStop.Color = Colors.Purple
Dim myLinearGradientBrush As New LinearGradientBrush()
myLinearGradientBrush.GradientStops.Add(firstStop)
myLinearGradientBrush.GradientStops.Add(secondStop)
myPen.Brush = myLinearGradientBrush
myGeometryDrawing.Pen = myPen
' Create an Image and set its DrawingImage to the Geometry created above.
Dim myImage As New Image()
myImage.Stretch = Stretch.None
myImage.Margin = New Thickness(10)
Dim myDrawingImage As New DrawingImage()
myDrawingImage.Drawing = myDrawingGroup
myImage.Source = myDrawingImage
Me.Content = myImage
End Sub
End Class
End Namespace
GeometryDrawing
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback