2011-03-05 29 views
3

我們需要在按鈕中顯示一個視頻,但我們希望顯示按鈕的背景,並且我們不能將背景放在視頻中,因爲按鈕可以更改大小,背景需要伸展到按鈕的大小,而不是視頻。視頻必須保持其大小的比例,所以我想知道是否有辦法像WPF中的綠屏一樣,所以我們可以在視頻上放置綠色背景,並讓按鈕忽略它以顯示它自己的視頻背景。WPF中的綠屏

我知道這是一個遠射,但任何建議是非常受歡迎的。

謝謝!

回答

6

好吧,我找到了解決方案。 4個簡單的步驟。

1)下載並安裝Shader Effects BuildTask and Templates

2)下載WPF Pixel Shader library。打開MainSolution文件夾中的解決方案,右鍵單擊WPFShaderEffectLibrary進行編譯,然後將編譯後的DLL的引用添加到項目中。注:僅編譯WPFShaderEffectLibrary,如果您嘗試編譯整個解決方案,它可能無法正常工作。

3)如下使用像素着色器(我用它在主窗口的構造但這並不重要):

public MainWindow() 
{ 
    InitializeComponent(); 

    ColorKeyAlphaEffect effect = new ColorKeyAlphaEffect(); 

    Brush brush = Effect.ImplicitInput; 

    effect.Input = brush; 

    // This is the Image control in your xaml. It should contain 
    // the image in which you want the green screen effect 
    imageControl.Effect = effect; 
} 

你需要這個庫:

using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Effects; 
using ShaderEffectLibrary; 

4)着色器只能用黑色工作,所以你需要改變一些東西以使它能夠與綠色(或任何其他你喜歡的顏色)一起工作,在我的情況下,我希望能夠使用綠色(比如綠屏) ,所以我改變了效果的價值。在您從Codeplex下載的解決方案中,您必須轉到WPFShaderEffectLibrary項目 - > ShaderSource文件夾ColorKeyAlpha.fx文件。在那裏,你必須改變下面的代碼:

float4 main(float2 uv : TEXCOORD) : COLOR 
    { 
     float4 color = tex2D(implicitInputSampler, uv); 

     // FROM THIS 
     if(color.r + color.g + color.b < 0.3) { 
      color.rgba = 0; 
     } 

     return color; 
    } 

對此

float4 main(float2 uv : TEXCOORD) : COLOR 
{ 
    float4 color = tex2D(implicitInputSampler, uv); 

    // TO THIS 
    if(color.r == 0 && color.g == 1.0 && color.b == 0) { 
     color.rgba = 0; 
    } 

    return color; 
} 

一旦做到這一點,重新編譯WPFShaderEffectLibrary項目,並更新項目(參考的一個步驟# 2)。一旦參考更新,PixelShader將開始使綠色值(R = 0,G = 255,B = 0)完全透明。它可以同時處理圖像和視頻。

我真的很難實現這一點,我希望任何人讀到這:)有用。

謝謝!

+0

你能分享你的最終代碼嗎? – 2012-12-14 17:14:20

+0

哦,我不認爲我已經擁有它了......你有沒有發現任何併發症? – Carlo 2012-12-14 17:53:20

+0

是啊...我找到了所有的東西......沒關係。我面臨的問題是在這裏http://stackoverflow.com/questions/13879362/mediaelement-freezes-video你可以建議的東西嗎?所以我使用Shader DLL並使用一些像TransitionEffect effect = new RippleTransitionEffect()的東西;並重置動畫this.Player1.Effect = null;但是當我最大。全屏動畫中的WPF窗口完全凍結。 (我使用2個MedieElement。) – 2012-12-14 18:31:22

0

爲各種示例搜索「色度鍵wpf效果」

+1

哈哈我搜索了,我得到的是一個鏈接到這個答案。任何其他建議? – Carlo 2011-03-07 19:06:10

+2

找到併發布了答案。感謝您的建議。 – Carlo 2011-03-07 20:07:56