位图效果概述

更新:2007 年 11 月

使用位图效果,设计人员和开发人员可以将可视化效果应用到已呈现的 Windows Presentation Foundation (WPF) 内容。例如,使用位图效果,您可以轻松地将 DropShadowBitmapEffect 效果或模糊效果应用到图像或按钮。

本主题包括下列各节。

  • WPF 位图效果
  • 如何应用效果
  • 创建自定义效果
  • 相关主题

WPF 位图效果

位图效果(BitmapEffect 对象)是简单的像素处理操作。位图效果将 BitmapSource 作为输入并在应用效果(如模糊或投影)之后生成新的 BitmapSource。每个位图效果都公开了控制筛选属性的属性,如 BlurBitmapEffectRadius

在 WPF 中,可以将效果设置为活动 Visual 对象的属性,如 ButtonTextBox,这是一种特殊情况。像素处理在运行时应用和呈现。在这种情况下,呈现时 Visual 将自动转换为其等效 BitmapSource,并作为输入提供给 BitmapEffect。输出替换 Visual 对象的默认呈现行为。这就是 BitmapEffect 对象强制 Visual 类仅以软件的方式呈现的原因,即应用效果之后无需针对 Visual 类进行硬件加速。

说明:

WPF 位图效果以软件模式呈现。应用效果的所有对象也将在软件中呈现。将位图效果应用于大型 Visual 类或对位图效果的属性进行动画处理时,将最大程度地降低性能。这并不表示您不能按照这种方式使用位图效果,而是指使用时应谨慎并进行充分的测试,以保证您的用户获得的体验和您预期的一样。

说明:

WPF 位图效果不支持部分信任的执行。若要使用位图效果,应用程序必须具有完全信任权限。

如何应用效果

BitmapEffectVisual 的一个属性。因此,将效果应用到诸如 ButtonImageDrawingVisualUIElement 等 Visual 类上就如同设置属性一样简单。可以将 BitmapEffect 设置为单个 BitmapEffect 对象,或者通过 BitmapEffectGroup 对象将多个效果链接在一起。

下面的示例演示如何在可扩展应用程序标记语言 (XAML) 中应用 BitmapEffect

<Button  Width="200">You Can't Read This!
  <Button.BitmapEffect>

  <!-- <BitmapEffectGroup> would go here if you wanted to apply more 
         then one effect to the Button. However, in this example only  
         one effect is being applied so BitmapEffectGroup does not need  
         to be included. -->

    <!-- The larger the Radius, the more blurring. The default range is 20.
         In addition, the KernelType is set to a box kernel. A box kernel
         creates less disruption (less blur) then the default Gaussian kernel. -->
    <BlurBitmapEffect Radius="10" KernelType="Box" />

  </Button.BitmapEffect>
</Button>

下面的示例演示如何在代码中应用 BitmapEffect

// Get a reference to the Button.
Button myButton = (Button)sender;

// Initialize a new BlurBitmapEffect that will be applied
// to the Button.
BlurBitmapEffect myBlurEffect = new BlurBitmapEffect();

// Set the Radius property of the blur. This determines how 
// blurry the effect will be. The larger the radius, the more
// blurring. 
myBlurEffect.Radius = 10;

// Set the KernelType property of the blur. A KernalType of "Box"
// creates less blur than the Gaussian kernal type.
myBlurEffect.KernelType = KernelType.Box;

// Apply the bitmap effect to the Button.
myButton.BitmapEffect = myBlurEffect;

有关演示如何使用 BitmapEffectGroup 对象应用多个效果的示例,请参见如何:创建多个可视化效果

说明:

BitmapEffect 应用到某个布局容器(如 DockPanelCanvas)时,该效果将被应用到该元素或 Visual 类及其所有子元素的可视化树中。

创建自定义效果

WPF 还提供了非托管接口,用于创建可在托管的 WPF 应用程序中使用的自定义效果。有关创建自定义位图效果的其他参考资料,请参见非托管 WPF 位图效果文档。

有关自定义位图效果的示例,请参见自定义 BitmapEffect 示例 - RGBFilter 示例。此示例演示创建自定义效果所需的非托管接口、托管的互操作性层以及使用自定义效果的 WPF 应用程序。

请参见

任务

如何:对模糊可视化效果进行动画处理

如何:对发光效果进行动画处理

如何:对投影可视化效果进行动画处理

如何:对斜切可视化效果进行动画处理

如何:对浮雕可视化效果进行动画处理

概念

图像处理概述

Windows Presentation Foundation 安全性

Windows Presentation Foundation 图形呈现概述

优化性能:二维图形和图像处理

参考

BitmapEffectGroup

BitmapEffectInput

BitmapEffectCollection

Unmanaged WPF Bitmap Effect