Sprache

CompositionMaskBrush Klasse

Definition

Zeichnet ein SpriteVisual mit einem CompositionBrush mit einer Deckkraftmaske, die darauf angewendet wird. Die Quelle der Deckkraftmaske kann ein beliebiger CompositionBrush vom Typ CompositionColorBrush, CompositionLinearGradientBrush, CompositionSurfaceBrush, CompositionEffectBrush oder ein CompositionNineGridBrush sein. Die Deckkraftmaske muss als CompositionSurfaceBrush angegeben werden.

public ref class CompositionMaskBrush sealed : CompositionBrush
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 196608)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class CompositionMaskBrush final : CompositionBrush
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 196608)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class CompositionMaskBrush : CompositionBrush
Public NotInheritable Class CompositionMaskBrush
Inherits CompositionBrush
Vererbung
Object Platform::Object IInspectable CompositionObject CompositionBrush CompositionMaskBrush
Attribute

Windows-Anforderungen

Anforderungen Beschreibung
Gerätefamilie
Windows 10 Anniversary Edition (eingeführt in 10.0.14393.0)
API contract
Windows.Foundation.UniversalApiContract (eingeführt in v3.0)

Beispiele

Vollständiges Beispiel: Anwenden einer Maske aus einer Bilddatei

Das folgende Beispiel zeigt, wie Sie ein CompositionMaskBrush-End-to-End erstellen, einschließlich des Ladens des Maskenbilds. Verwenden Sie ein PNG mit einem Alphakanal: Vollständig undurchsichtige Bereiche der Maske lassen die Quelle sichtbar, vollständig transparente Bereiche blenden sie aus, und Zwischen alpha-Werte erzeugen teilweise Transparenz. Die RGB-Farbwerte des Masken steuern keine Deckkraft; Ein undurchsichtiges schwarzweißes Bild erzeugt keine Transparenzmaske.

// Get the Compositor from a XAML element
Compositor compositor = ElementCompositionPreview.GetElementVisual(myElement).Compositor;

// Create the mask brush
CompositionMaskBrush maskBrush = compositor.CreateMaskBrush();

// Create the source (the content to be masked)
CompositionColorBrush colorBrush = compositor.CreateColorBrush(Colors.Blue);
maskBrush.Source = colorBrush;

// Load a mask image and create a surface brush from it
// The PNG's alpha channel defines the mask (opaque = visible, transparent = hidden)
LoadedImageSurface maskSurface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///Assets/CircleMask.png"));
CompositionSurfaceBrush maskSurfaceBrush = compositor.CreateSurfaceBrush(maskSurface);
maskBrush.Mask = maskSurfaceBrush;

// Apply to a SpriteVisual and add it to the XAML tree
SpriteVisual visual = compositor.CreateSpriteVisual();
visual.Brush = maskBrush;
visual.Size = new System.Numerics.Vector2(200, 200);
ElementCompositionPreview.SetElementChildVisual(myElement, visual);

Anwenden einer sternförmigen Maske auf ein Bild

Die folgenden Beispiele zeigen verschiedene Möglichkeiten zur Verwendung von CompositionMaskBrush. Jedes Beispiel empfängt vorab erstellte ICompositionSurface-Instanzen . Sie können diese Oberflächen mithilfe von LoadedImageSurface erstellen, wie im obigen Beispiel gezeigt, oder über Win2D.

private SpriteVisual CreateCircleImage(ICompositionSurface myImageSurface, ICompositionSurface myStarMaskSurface) 
{ 
  // Create SurfaceBrush with image surface and specify stretch and alignment 
  CompositionSurfaceBrush imageBrush = _compositor.CreateSurfaceBrush(myImageSurface); 
  imageBrush.HorizontalAlignmentRatio = 0.5f; 
  imageBrush.VerticalAlignmentRatio = 0.5f; 
  imageBrush.Stretch = CompositionStretch.UniformToFill; 

  // Create surface brush with opacity mask surface and specify stretch and alignment 
  CompositionSurfaceBrush starMaskBrush = _compositor.CreateSurfaceBrush(myStarMaskSurface); 
  starMaskBrush.Stretch = CompositionStretch.Uniform; 

  // SpriteVisual to be painted with masked content 
  SpriteVisual imageVisual = _compositor.CreateSpriteVisual(); 
  imageVisual.Size = new Vector2(200, 200); 

  // Create MaskBrush 
  CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush(); 
  maskBrush.Source = imageBrush; // Set source to content that is to be masked 
  maskBrush.Mask = starMaskBrush; // Set mask to content that is the opacity mask 

  imageVisual.Brush = maskBrush; // Paint SpriteVisual with MaskBrush 

  return imageVisual;           
} 

Animieren einer Deckkraftmaske zum Erstellen eines Farbblüteeffekts

private void CreateCircleMaskSprite(ICompositionSurface myCircleMaskSurface, ICompositionSurface myImageSurface)
{
  // SpriteVisual to be painted by MaskBrush
  _circleVisual = _compositor.CreateSpriteVisual();
  _circleVisual.Size = MyContainerGrid.RenderSize.ToVector2();

  // SurfaceBrush w/ image as MaskBrush.Source
  CompositionSurfaceBrush imageBrush = _compositor.CreateSurfaceBrush(myImageSurface);
  imageBrush.Stretch = CompositionStretch.UniformToFill;

  // SurfaceBrush w/ opacity mask surface as MaskBrush.Mask
  CompositionSurfaceBrush circleBrush = _compositor.CreateSurfaceBrush(myCircleMaskSurface);

  // Specify stretch and transform properties to scale and position brush for initial state
  circleBrush.Stretch = CompositionStretch.Uniform;
  circleBrush.CenterPoint = _circleVisual.Size / 2;
  circleBrush.Scale = new Vector2(0.5f);

  // Create MaskBrush
  CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
  maskBrush.Source = imageBrush;
  maskBrush.Mask = circleBrush;
  _circleVisual.Brush = maskBrush;

  // Set SpriteVisual as child of XAML Grid
  ElementCompositionPreview.SetElementChildVisual(MyContainerGrid, _circleVisual);
}

private void BloomAnimation()
{
  // Create KeyFrame Animation to target Scale transform on SurfaceBrush 
  Vector2KeyFrameAnimation scaleAnimation = _compositor.CreateVector2KeyFrameAnimation();
  scaleAnimation.InsertKeyFrame(1.0f, new Vector2(2));
  scaleAnimation.Duration = TimeSpan.FromMilliseconds(500);

  var maskBrush = (CompositionMaskBrush)_circleVisual.Brush;
  var surfaceBrush = (CompositionSurfaceBrush) maskBrush.Mask;

  // Animate the Scale on the circle opacity mask
  surfaceBrush.StartAnimation("Scale", scaleAnimation);      
}

Verwenden einer NineGridBrush-Maske für ein abgerundetes Rechteck

private SpriteVisual CreateMaskedRoundedRectVisual(ICompositionSurface myRoundedRectMaskSurface)
{
  // ColorBrush to be set as MaskBrush.Source
  CompositionColorBrush colorBrush = _compositor.CreateColorBrush(Colors.Blue);

  // SurfaceBrush w/ opacity mask surface
  CompositionSurfaceBrush roundedRectBrush = _compositor.CreateSurfaceBrush(myRoundedRectMaskSurface);
  roundedRectBrush.Stretch = CompositionStretch.Fill; // stretch for center of nine-grid

  // NineGridBrush w/ insets on opacity mask surface
  CompositionNineGridBrush ninegridBrush = _compositor.CreateNineGridBrush();
  ninegridBrush.Source = roundedRectBrush;
  ninegridBrush.SetInsets(_cornerRadius); // the radius, in pixels, of the corner as specified on my opacity mask surface 

  // Create MaskBrush
  CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
  maskBrush.Source = colorBrush;
  maskBrush.Mask = ninegridBrush;

  // Paint SpriteVisual with MaskBrush
  SpriteVisual sprite = _compositor.CreateSpriteVisual();
  sprite.Size = new Vector2(300, 200);
  sprite.Brush = maskBrush;
  return sprite;

}

Hinweise

CompositionMaskBrush kann nicht als Quellparameter auf einen CompositionEffectBrush festgelegt werden. Wenn Sie einen IGraphicsEffect auf den maskierten Inhalt anwenden möchten, verwenden Sie stattdessen einen CompositionEffectBrush mit dem Zusammengesetzten Effekt .

Eigenschaften

Name Beschreibung
Comment

Eine Zeichenfolge, die dem CompositionObject zugeordnet werden soll.

(Geerbt von CompositionObject)
Compositor

Der Kompositor , der zum Erstellen dieses CompositionObjects verwendet wurde.

(Geerbt von CompositionObject)
Dispatcher

Der Dispatcher für das CompositionObject.

(Geerbt von CompositionObject)
DispatcherQueue

Ruft den DispatcherQueue für das KompostionObject ab.

(Geerbt von CompositionObject)
ImplicitAnimations

Die Auflistung impliziter Animationen, die diesem Objekt zugeordnet sind.

(Geerbt von CompositionObject)
Mask

Ein Pinsel, der die Deckkraftmaske enthält, mit der der Inhalt des Quellpinsels maskiert werden soll. Kann vom Typ CompositionSurfaceBrush oder CompositionNineGridBrush sein.

Properties

Die Auflistung der Eigenschaften, die dem CompositionObject zugeordnet sind.

(Geerbt von CompositionObject)
Source

Ein Pinsel, dessen Inhalt durch die Deckkraftmaske maskiert werden soll. Kann vom Typ CompositionSurfaceBrush, CompositionColorBrush oder CompositionNineGridBrush sein.

Methoden

Name Beschreibung
Close()

Schließt das CompositionObject und gibt Systemressourcen frei.

(Geerbt von CompositionObject)
Dispose()

Führt anwendungsdefinierte Aufgaben aus, die mit dem Freigeben, Freigeben oder Zurücksetzen nicht verwalteter Ressourcen verknüpft sind.

(Geerbt von CompositionObject)
PopulatePropertyInfo(String, AnimationPropertyInfo)

Definiert eine Eigenschaft, die animiert werden kann.

(Geerbt von CompositionObject)
StartAnimation(String, CompositionAnimation, AnimationController)

Verbindet eine Animation mit der angegebenen Eigenschaft des Objekts und startet die Animation.

(Geerbt von CompositionObject)
StartAnimation(String, CompositionAnimation)

Verbindet eine Animation mit der angegebenen Eigenschaft des Objekts und startet die Animation.

(Geerbt von CompositionObject)
StartAnimationGroup(ICompositionAnimationBase)

Startet eine Animationsgruppe.

Mit der StartAnimationGroup-Methode auf CompositionObject können Sie CompositionAnimationGroup starten. Alle Animationen in der Gruppe werden gleichzeitig für das Objekt gestartet.

(Geerbt von CompositionObject)
StopAnimation(String)

Trennt eine Animation von der angegebenen Eigenschaft und stoppt die Animation.

(Geerbt von CompositionObject)
StopAnimationGroup(ICompositionAnimationBase)

Beendet eine Animationsgruppe.

(Geerbt von CompositionObject)
TryGetAnimationController(String)

Gibt einen AnimationController für die Animation zurück, die auf der angegebenen Eigenschaft ausgeführt wird.

(Geerbt von CompositionObject)

Gilt für:

Siehe auch