共用方式為


點陣圖效果概觀

更新:2007 年 11 月

點陣圖效果可以讓設計人員和開發人員將視覺效果套用至呈現的 Windows Presentation Foundation (WPF) 內容。例如,點陣圖效果可以讓您輕易地將 DropShadowBitmapEffect 效果或模糊效果套用至影像或按鈕。

這個主題包含下列章節。

  • WPF 點陣圖效果
  • 如何套用效果
  • 建立自訂效果
  • 相關主題

WPF 點陣圖效果

點陣圖效果 (BitmapEffect 物件) 是一種簡單的像素處理作業。點陣圖效果會將 BitmapSource 當做輸入,並且在套用效果 (例如模糊或下拉式陰影) 後產生新的 BitmapSource。每個點陣圖效果都會公開屬性,這些屬性可以控制篩選屬性,例如 BlurBitmapEffectRadius

有一個特殊的情況,在 WPF 中,效果可以以實況 Visual 物件上的屬性來設定,例如 ButtonTextBox。像素處理作業會在執行階段套用和呈現。在此情況下,呈現時 Visual 會自動轉換成其 BitmapSource 的對等項目,並且當成輸入傳遞至 BitmapEffect。輸出結果會取代 Visual 物件的預設呈現行為。這就是為何 BitmapEffect 物件會強迫視覺物件只在軟體中呈現,也就是說,套用效果時視覺物件不會使用硬體加速功能。

注意事項:

WPF 點陣圖效果是在軟體模式中呈現。任何套用效果的物件也都會在軟體中呈現。在大型視覺物件上使用點陣圖效果或者以點陣圖效果的屬性建立動畫時,降低的效能最多。這並不代表您完全不能這樣使用點陣圖效果,但是應該考量周詳並且做過徹底測試,以確保使用者能得到預期的經驗。

注意事項:

WPF 點陣圖效果不支援執行部分信任。應用程式必須具有完全信任權限,才能使用點陣圖效果。

如何套用效果

BitmapEffectVisual 上的屬性。因此,將效果套用至視覺物件,例如 ButtonImageDrawingVisualUIElement,就跟設定屬性一樣簡單。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 物件套用多個效果的範例,請參閱 HOW TO:建立多個視覺效果

注意事項:

BitmapEffect 套用至配置容器時,例如 DockPanelCanvas,效果會套用至項目或視覺物件的視覺化樹狀目錄,包括其所有子項目。

建立自訂效果

WPF 也提供 Unmanaged 的介面,以建立可以在 Unmanaged WPF 應用程式中使用的自訂效果。如需建立自訂點陣圖效果的其他參考資料,請參閱 Unmanaged WPF 點陣圖效果文件。

如需自訂點陣圖效果的範例,請參閱自訂 BitmapEffect 範例 - RGBFilter 範例。這個範例示範建立自訂效果、Managed 互通性 (Interoperability) 層以及使用該自訂效果的 WPF 應用程式時所需的 Unmanaged 介面。

請參閱

工作

HOW TO:建立柔邊視覺效果動畫

HOW TO:建立光暈效果動畫

HOW TO:建立下拉式陰影視覺效果的動畫

HOW TO:建立斜面視覺效果動畫

HOW TO:建立浮凸視覺效果動畫

概念

影像處理概觀

Windows Presentation Foundation 安全性

Windows Presentation Foundation 圖形轉譯概觀

最佳化效能:2D 圖形和影像處理

參考

BitmapEffectGroup

BitmapEffectInput

BitmapEffectCollection

Unmanaged WPF Bitmap Effect