點陣圖效果概觀

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

重要

在 .NET Framework 4 或更新版本中,類別 BitmapEffect 已經過時。 如果您嘗試使用 類別 BitmapEffect ,您將會收到過時的例外狀況。 類別的非過時替代方案 BitmapEffectEffect 類別。 在大部分情況下,類別 Effect 的速度會大幅加快。

WPF 點陣圖效果

點陣圖效果( BitmapEffect 物件)是簡單的圖元處理作業。 點陣圖效果會採用 BitmapSource 作為輸入,並在套用效果之後產生新的 BitmapSource ,例如模糊或陰影。 每個點陣圖效果都會公開可以控制篩選屬性的屬性,例如 RadiusBlurBitmapEffect

就特殊情況而言,在 WPF 中,效果可以設定為即時 Visual 物件的屬性,例如 ButtonTextBox 。 像素處理會在執行階段套用並進行轉譯。 在此情況下,在轉譯時, Visual 會自動轉換成其 BitmapSource 對等專案,並將 做為 的 BitmapEffect 輸入。 輸出會 Visual 取代物件的預設轉譯行為。 這就是為什麼物件強制視覺效果在軟體中轉譯的原因 BitmapEffect ,也就是在套用效果時,視覺效果上不會有任何硬體加速。

注意

WPF 點陣圖效果會以軟體模式呈現。 套用效果的任何物件也會在軟體中轉譯。 對大型視覺物件使用點陣圖效果或為點陣圖效果的屬性建立動畫時,效能會降低最多。 這並不是說您不應該以此種方式使用點陣圖效果,而是您應特別小心並徹底測試,以確保使用者能得到預期的體驗。

注意

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

如何套用效果

BitmapEffect 是 上的 Visual 屬性。 因此,將效果套用至視覺效果,例如 ButtonImageDrawingVisualUIElement ,就像設定屬性一樣簡單。 BitmapEffect 可以設定為單 BitmapEffect 一物件,或使用 物件來鏈結 BitmapEffectGroup 多個效果。

下列範例示範如何在 Extensible Application Markup Language (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;

注意

BitmapEffect當 套用至配置容器,例如 DockPanelCanvas 時,效果會套用至專案或視覺效果的視覺化樹狀結構,包括其所有子專案。

建立自訂效果

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

另請參閱