2011-09-13 24 views
0

描画コンテキストのDrawTextメソッドを使用して描画されたテキストにエフェクトを追加する方法はありますか?特に私はグロー効果を探しています。私は現在、(UIElementから継承私自身の形状のOnRenderに)このようなテキストを描画しています:描画テキストにグロー効果を追加

... 
drawingContext.PushClip(new RectangleGeometry(textrect)); 
drawingContext.DrawText(formattedText, new Point(textrect.Left, textrect.Top)); 
drawingContext.Pop(); 
... 

、それが描かれたテキストにグロー効果を追加することが可能であるならば、私はまた、ときに適用する必要があります上記の行へのその後の呼び出しでテキストが変更されます(追加の作業が必要かどうかわかりません)。

答えて

1

グロー効果は、適切なビジュアルに適用され、DrawText()呼び出しのような描画コンテキストメンバーには当てはまりません。あなたは

Visual theVisual = textBlock ; //Put the aimed visual here. 
    double width = Convert.ToDouble(theVisual.GetValue(FrameworkElement.WidthProperty)); 
    double height = Convert.ToDouble(
         theVisual.GetValue(FrameworkElement.HeightProperty)); 
    if (double.IsNaN(width) || double.IsNaN(height)) 
    { 
     throw new FormatException(
      "You need to indicate the Width and Height values of the UIElement."); 
    } 
    RenderTargetBitmap render = new RenderTargetBitmap(
      Convert.ToInt32(width), 
      Convert.ToInt32(this.GetValue(FrameworkElement.HeightProperty)), 
      96, 
      96, 
      PixelFormats.Pbgra32); 
    // Indicate which control to render in the image 
    render.Render(this); 
    Stream oStream = new MemoryStream(); 
    PngBitmapEncoder encoder = new PngBitmapEncoder(); 
    encoder.Frames.Add(BitmapFrame.Create(render)); 
    encoder.Save(oStream); 
    oStream.Flush(); 

(交互あなたにも描画コンテキストにそれを描くことができます)画像としてレンダリングするテキストブロックを使用し、その後
<TextBox Width="200"> 
    <TextBox.BitmapEffect> 

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

    <!-- The OuterGlow is blue, extends out 30 pixels, has the 
     maximum noise possible, and is 40% Opaque. --> 
    <OuterGlowBitmapEffect GlowColor="Blue" GlowSize="30" Noise="1" 
    Opacity="0.4" /> 

    </TextBox.BitmapEffect> 
</TextBox> 

...描画コンテキストに TextBlockのようなビジュアルを描くと考えることができます

これが役に立ったら教えてください....

+0

これはうまくいっています(.NET 3.5)が、これを良いバックアップシナリオとして残しておきます。私はテキストボックスにdropshadoweffectと描画されたテキストにシミュレートされたドロップシャドウ効果を単純なシナリオに戻しました。二度。それは十分であり、.NET 4と互換性があります。 – mtijn

1

.NET 4では、UIElement.BitmapEffectは廃止されており、レンダリングされません。代わりに、UIElement.Effectプロパティを使用する必要があります。選択できるエフェクトは少なくなりますが、右のプロパティ設定でDropShadowEffectを使用してグロー効果を作成できます。

このリソースチェックアウト:http://karlshifflett.wordpress.com/2008/11/04/wpf-sample-series-solution-for-the-obsolete-bitmapeffect-property-and-rendering-an-outerglowbitmapeffect/(ノートを、このサンプルはButtonに輝きが追加されますが、それはLabelまたはTextBlockにも同様に適用されるべきです)。

関連する問題