방법: Drawing의 불투명도 제어

업데이트: 2007년 11월

이 예제에서는 Drawing의 불투명도를 수정하는 방법을 보여 줍니다. DrawingGroup 클래스는 Opacity 속성이 있는 유일한 형식의 Drawing 개체입니다.

Drawing 개체의 불투명도를 변경하려면 개체를 DrawingGroup에 추가한 다음 DrawingGroup 개체의 Opacity 속성을 설정합니다.

DrawingGroup 개체의 Opacity 설정값은 이 개체의 불투명도 값에 자식 그리기의 불투명도 값을 곱한 값입니다. 예를 들어 DrawingGroupOpacity가 0.5이고 이 개체에 불투명도가 50%인 Brush가 있는 GeometryDrawing이 포함되어 있는 경우 브러시의 불투명도는 25%(0.5*0.5)가 됩니다.

그리기에서 선택한 일부분만 불투명도를 변경하려면 OpacityMask를 사용합니다.

예제

다음 예제에서는 DrawingGroup을 사용하여 여러 개의 GeometryDrawing 개체를 결합합니다. 이 예제에서는 DrawingGroup 개체의 불투명도를 0.25로 설정하여 그리기의 불투명도를 25%로 만듭니다.

이 그림에서는 Opacity를 0.25로 설정하기 전과 후의 DrawingGroup을 보여 줍니다.

여러 불투명도 설정의 DrawingGroup

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SDKSample
{
    public class OpacityExample : Page
    {
        public OpacityExample()
        {

            //
            // Create a GeometryDrawing.
            //

            // Define the drawing's contents.
            PathFigure pLineFigure = new PathFigure();
            pLineFigure.StartPoint = new Point(25, 25);
            PolyLineSegment pLineSegment = new PolyLineSegment();
            pLineSegment.Points.Add(new Point(0, 50));
            pLineSegment.Points.Add(new Point(25, 75));
            pLineSegment.Points.Add(new Point(50, 50));
            pLineSegment.Points.Add(new Point(25, 25));
            pLineSegment.Points.Add(new Point(25, 0));
            pLineFigure.Segments.Add(pLineSegment);
            PathGeometry pGeometry = new PathGeometry();
            pGeometry.Figures.Add(pLineFigure);

            GeometryDrawing drawing1 = new GeometryDrawing(
                    Brushes.Lime,
                    new Pen(Brushes.Black, 10),
                    pGeometry
                );

            //
            // Create another GeometryDrawing.
            //
            GeometryDrawing drawing2 = new GeometryDrawing(
                    Brushes.Lime,
                    new Pen(Brushes.Black, 2),
                    new EllipseGeometry(new Point(10, 10), 5, 5)
                );

            // Create the DrawingGroup and add the
            // geometry drawings.
            DrawingGroup aDrawingGroup = new DrawingGroup();
            aDrawingGroup.Children.Add(drawing1);
            aDrawingGroup.Children.Add(drawing2);

            //
            // Set the opacity of the DrawingGroup to 0.25.
            //
            aDrawingGroup.Opacity = 0.25;

            // Use an Image control and a DrawingImage to
            // display the drawing.
            DrawingImage aDrawingImage = new DrawingImage(aDrawingGroup);

            // Freeze the DrawingImage for performance benefits.
            aDrawingImage.Freeze();

            Image anImage = new Image();
            anImage.Source = aDrawingImage;
            anImage.Stretch = Stretch.None;
            anImage.HorizontalAlignment = HorizontalAlignment.Left;

            // Create a border around the images and add it to the
            // page.
            Border imageBorder = new Border();
            imageBorder.BorderBrush = Brushes.Gray;
            imageBorder.BorderThickness = new Thickness(1);
            imageBorder.VerticalAlignment = VerticalAlignment.Top;
            imageBorder.HorizontalAlignment = HorizontalAlignment.Left;
            imageBorder.Margin = new Thickness(20);
            imageBorder.Child = anImage;

            this.Background = Brushes.White;
            this.Margin = new Thickness(20);
            this.Content = imageBorder;

        }
    }
}
<Page 
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:PresentationOptions="https://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
  xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="PresentationOptions"
  Background="White" Margin="20">
  <Border BorderBrush="Gray" BorderThickness="1" 
    HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="20">
    <Image Stretch="None">
      <Image.Source>
        <DrawingImage PresentationOptions:Freeze="True">
          <DrawingImage.Drawing>

            <!-- The drawing group, with an Opacity of 0.25. -->
            <DrawingGroup Opacity="0.25">
              <GeometryDrawing Brush="Lime" Geometry="M 25,25 L 0,50 25,75 50,50 25,25 25,0">
                <GeometryDrawing.Pen>
                  <Pen Thickness="10" Brush="Black" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Brush="Lime">
                <GeometryDrawing.Geometry>
                  <EllipseGeometry Center="10,10" RadiusX="5" RadiusY="5" />
                </GeometryDrawing.Geometry>
                <GeometryDrawing.Pen>
                  <Pen Thickness="2" Brush="Black" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
            </DrawingGroup>
          </DrawingImage.Drawing>
        </DrawingImage>
      </Image.Source>
    </Image>
  </Border>


</Page>

참고 항목

개념

Drawing 개체 개요

참조

Freeze

PresentationOptions:Freeze 특성

OpacityMask