DrawingContext Classe

Definição

Descreve o conteúdo visual usando os comandos draw, push e pop.Describes visual content using draw, push, and pop commands.

public ref class DrawingContext abstract : System::Windows::Threading::DispatcherObject, IDisposable
public abstract class DrawingContext : System.Windows.Threading.DispatcherObject, IDisposable
type DrawingContext = class
    inherit DispatcherObject
    interface IDisposable
Public MustInherit Class DrawingContext
Inherits DispatcherObject
Implements IDisposable
Herança
DrawingContext
Implementações

Exemplos

O exemplo a seguir recupera um DrawingContext de um DrawingVisual e o usa para desenhar um retângulo.The following example retrieves a DrawingContext from a DrawingVisual and uses it to draw a rectangle.

// Create a DrawingVisual that contains a rectangle.
private DrawingVisual CreateDrawingVisualRectangle()
{
    DrawingVisual drawingVisual = new DrawingVisual();

    // Retrieve the DrawingContext in order to create new drawing content.
    DrawingContext drawingContext = drawingVisual.RenderOpen();

    // Create a rectangle and draw it in the DrawingContext.
    Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
    drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);

    // Persist the drawing content.
    drawingContext.Close();

    return drawingVisual;
}
' Create a DrawingVisual that contains a rectangle.
Private Function CreateDrawingVisualRectangle() As DrawingVisual
    Dim drawingVisual As New DrawingVisual()

    ' Retrieve the DrawingContext in order to create new drawing content.
    Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()

    ' Create a rectangle and draw it in the DrawingContext.
    Dim rect As New Rect(New Point(160, 100), New Size(320, 80))
    drawingContext.DrawRectangle(Brushes.LightBlue, CType(Nothing, Pen), rect)

    ' Persist the drawing content.
    drawingContext.Close()

    Return drawingVisual
End Function

O exemplo a seguir demonstra os comandos PushOpacity, PushEffecte Pop.The next example demonstrates the PushOpacity, PushEffect, and Pop commands. A DrawingContext é Obtida de uma DrawingGroup e exibida usando um controle de Image.The DrawingContext is obtained from a DrawingGroup and displayed using an Image control.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Media.Effects;

namespace SDKSample
{

    public class PushEffectExample : Page
    {

        public PushEffectExample()
        {
            Pen shapeOutlinePen = new Pen(Brushes.Black, 2);
            shapeOutlinePen.Freeze();

            // Create a DrawingGroup
            DrawingGroup dGroup = new DrawingGroup();

            // Obtain a DrawingContext from 
            // the DrawingGroup.
            using (DrawingContext dc = dGroup.Open())
            {
                // Draw a rectangle at full opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(0, 0, 25, 25));

                // Push an opacity change of 0.5. 
                // The opacity of each subsequent drawing will
                // will be multiplied by 0.5.
                dc.PushOpacity(0.5);

                // This rectangle is drawn at 50% opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(25, 25, 25, 25));

                // Blurs subsquent drawings. 
                dc.PushEffect(new BlurBitmapEffect(), null);

                // This rectangle is blurred and drawn at 50% opacity (0.5 x 0.5). 
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(50, 50, 25, 25));

                // This rectangle is also blurred and drawn at 50% opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(75, 75, 25, 25));

                // Stop applying the blur to subsquent drawings.
                dc.Pop();

                // This rectangle is drawn at 50% opacity with no blur effect.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, new Rect(100, 100, 25, 25));
            }

            // Display the drawing using an image control.
            Image theImage = new Image();
            DrawingImage dImageSource = new DrawingImage(dGroup);
            theImage.Source = dImageSource;

            this.Content = theImage;
        }
    }
}
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Effects

Namespace SDKSample


    Public Class PushEffectExample
        Inherits Page

        Public Sub New()
            Dim shapeOutlinePen As New Pen(Brushes.Black, 2)
            shapeOutlinePen.Freeze()

            ' Create a DrawingGroup
            Dim dGroup As New DrawingGroup()

            ' Obtain a DrawingContext from 
            ' the DrawingGroup.
            Using dc As DrawingContext = dGroup.Open()
                ' Draw a rectangle at full opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(0, 0, 25, 25))

                ' Push an opacity change of 0.5. 
                ' The opacity of each subsequent drawing will
                ' will be multiplied by 0.5.
                dc.PushOpacity(0.5)

                ' This rectangle is drawn at 50% opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(25, 25, 25, 25))

                ' Blurs subsquent drawings. 
                dc.PushEffect(New BlurBitmapEffect(), Nothing)

                ' This rectangle is blurred and drawn at 50% opacity (0.5 x 0.5). 
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(50, 50, 25, 25))

                ' This rectangle is also blurred and drawn at 50% opacity.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(75, 75, 25, 25))

                ' Stop applying the blur to subsquent drawings.
                dc.Pop()

                ' This rectangle is drawn at 50% opacity with no blur effect.
                dc.DrawRectangle(Brushes.Blue, shapeOutlinePen, New Rect(100, 100, 25, 25))
            End Using

            ' Display the drawing using an image control.
            Dim theImage As New Image()
            Dim dImageSource As New DrawingImage(dGroup)
            theImage.Source = dImageSource

            Me.Content = theImage

        End Sub




    End Class

End Namespace

Comentários

Use um DrawingContext para preencher um Visual ou um Drawing com conteúdo visual.Use a DrawingContext to populate a Visual or a Drawing with visual content.

Embora os métodos de desenho DrawingContext pareçam semelhantes aos métodos Draw do tipo System.Drawing.Graphics, eles funcionam de forma muito diferente: DrawingContext é usado com um sistema gráfico de modo retido, enquanto o tipo System.Drawing.Graphics é usado com um sistema gráfico de modo imediato.Although the DrawingContext draw methods appear similar to the draw methods of the System.Drawing.Graphics type, they function very differently: DrawingContext is used with a retained mode graphics system, while the System.Drawing.Graphics type is used with an immediate mode graphics system. Quando você usa os comandos Draw de um objeto DrawingContext, na verdade está armazenando um conjunto de instruções de renderização (embora o mecanismo de armazenamento exato dependa do tipo de objeto que fornece o DrawingContext) que será usado posteriormente pelo sistema de gráficos; Você não está desenhando na tela em tempo real.When you use a DrawingContext object's draw commands, you are actually storing a set of rendering instructions (although the exact storage mechanism depends on the type of object that supplies the DrawingContext) that will later be used by the graphics system; you are not drawing to the screen in real-time. Para obter mais informações sobre como o sistema gráfico do Windows Presentation Foundation (WPF) funciona, consulte visão geral da renderização de gráficos do WPF.For more information about how the Windows Presentation Foundation (WPF) graphics system works, see WPF Graphics Rendering Overview.

Você nunca instancia diretamente um DrawingContext; no entanto, você pode adquirir um contexto de desenho de determinados métodos, como DrawingGroup.Open e DrawingVisual.RenderOpen.You never directly instantiate a DrawingContext; you can, however, acquire a drawing context from certain methods, such as DrawingGroup.Open and DrawingVisual.RenderOpen.

Propriedades

Dispatcher

Obtém o Dispatcher ao qual este DispatcherObject está associado.Gets the Dispatcher this DispatcherObject is associated with.

(Herdado de DispatcherObject)

Métodos

CheckAccess()

Determina se o thread de chamada tem acesso a este DispatcherObject.Determines whether the calling thread has access to this DispatcherObject.

(Herdado de DispatcherObject)
Close()

Fecha o DrawingContext e libera o conteúdo.Closes the DrawingContext and flushes the content. Depois disso, o DrawingContext não pode ser modificado.Afterward, the DrawingContext cannot be modified.

DisposeCore()

Libera todos os recursos usados pelo DrawingContext.Releases all resources used by the DrawingContext.

DrawDrawing(Drawing)

Desenha o objeto Drawing especificado.Draws the specified Drawing object.

DrawEllipse(Brush, Pen, Point, AnimationClock, Double, AnimationClock, Double, AnimationClock)

Desenha uma elipse com o Brush e Pen especificado e aplica os relógios de animação especificados.Draws an ellipse with the specified Brush and Pen and applies the specified animation clocks.

DrawEllipse(Brush, Pen, Point, Double, Double)

Desenha uma elipse com o Brush e Pen especificados.Draws an ellipse with the specified Brush and Pen.

DrawGeometry(Brush, Pen, Geometry)

Desenha a Geometry especificada usando o Brush e Pen especificados.Draws the specified Geometry using the specified Brush and Pen.

DrawGlyphRun(Brush, GlyphRun)

Desenha o texto especificado.Draws the specified text.

DrawImage(ImageSource, Rect)

Desenha uma imagem na região definida pelo Rect especificado.Draws an image into the region defined by the specified Rect.

DrawImage(ImageSource, Rect, AnimationClock)

Desenha uma imagem na região definida pelo Rect especificado e aplica o relógio de animação especificado.Draws an image into the region defined by the specified Rect and applies the specified animation clock.

DrawLine(Pen, Point, AnimationClock, Point, AnimationClock)

Desenha uma linha entre os pontos especificados usando o Pen especificado e aplica os relógios de animação especificados.Draws a line between the specified points using the specified Pen and applies the specified animation clocks.

DrawLine(Pen, Point, Point)

Desenha uma linha entre os pontos especificados usando o Pen especificado.Draws a line between the specified points using the specified Pen.

DrawRectangle(Brush, Pen, Rect)

Desenha um retângulo com o Brush e Pen especificados.Draws a rectangle with the specified Brush and Pen. A caneta e o pincel podem ser null.The pen and the brush can be null.

DrawRectangle(Brush, Pen, Rect, AnimationClock)

Desenha um retângulo com o Brush e Pen especificado e aplica os relógios de animação especificados.Draws a rectangle with the specified Brush and Pen and applies the specified animation clocks.

DrawRoundedRectangle(Brush, Pen, Rect, AnimationClock, Double, AnimationClock, Double, AnimationClock)

Desenha um retângulo arredondado com o Brush e Pen especificado e aplica os relógios de animação especificados.Draws a rounded rectangle with the specified Brush and Pen and applies the specified animation clocks.

DrawRoundedRectangle(Brush, Pen, Rect, Double, Double)

Desenha um retângulo arredondado com o Brush e Pen especificados.Draws a rounded rectangle with the specified Brush and Pen.

DrawText(FormattedText, Point)

Desenha texto formatado no local especificado.Draws formatted text at the specified location.

DrawVideo(MediaPlayer, Rect)

Desenha um vídeo na região especificada.Draws a video into the specified region.

DrawVideo(MediaPlayer, Rect, AnimationClock)

Desenha um vídeo na região especificada e aplica o relógio de animação especificado.Draws a video into the specified region and applies the specified animation clock.

Equals(Object)

Determina se o objeto especificado é igual ao objeto atual.Determines whether the specified object is equal to the current object.

(Herdado de Object)
GetHashCode()

Serve como a função de hash padrão.Serves as the default hash function.

(Herdado de Object)
GetType()

Obtém o Type da instância atual.Gets the Type of the current instance.

(Herdado de Object)
MemberwiseClone()

Cria uma cópia superficial do Object atual.Creates a shallow copy of the current Object.

(Herdado de Object)
Pop()

Exibe a última operação de máscara de opacidade, opacidade, recortar, efeito ou transformação que foi enviada por push no contexto de desenho.Pops the last opacity mask, opacity, clip, effect, or transform operation that was pushed onto the drawing context.

PushClip(Geometry)

Envia a região de corte especificada no contexto do desenho.Pushes the specified clip region onto the drawing context.

PushEffect(BitmapEffect, BitmapEffectInput)

Envia o BitmapEffect especificado no contexto do desenho.Pushes the specified BitmapEffect onto the drawing context.

PushGuidelineSet(GuidelineSet)

Envia o GuidelineSet especificado no contexto do desenho.Pushes the specified GuidelineSet onto the drawing context.

PushOpacity(Double)

Envia por push a configuração de opacidade especificada no contexto de desenho.Pushes the specified opacity setting onto the drawing context.

PushOpacity(Double, AnimationClock)

Envia por push a configuração de opacidade especificada no contexto do desenho e aplica o relógio de animação especificado.Pushes the specified opacity setting onto the drawing context and applies the specified animation clock.

PushOpacityMask(Brush)

Envia por push a máscara de opacidade especificada no contexto de desenho.Pushes the specified opacity mask onto the drawing context.

PushTransform(Transform)

Envia o Transform especificado no contexto do desenho.Pushes the specified Transform onto the drawing context.

ToString()

Retorna uma cadeia de caracteres que representa o objeto atual.Returns a string that represents the current object.

(Herdado de Object)
VerifyAccess()

Impõe que o thread de chamada tenha acesso a este DispatcherObject.Enforces that the calling thread has access to this DispatcherObject.

(Herdado de DispatcherObject)
VerifyApiNonstructuralChange()

Este membro dá suporte à infraestrutura WPFWPF e não se destina a ser usado diretamente do código.This member supports the WPFWPF infrastructure and is not intended to be used directly from your code.

Implantações explícitas de interface

IDisposable.Dispose()

Este membro dá suporte à infraestrutura Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) e não se destina a ser usado diretamente do código.This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

Aplica-se a

Veja também