mtijn
mtijn

Reputation: 3678

add gloweffect to drawn text

is there a way to add effects to text drawn using the DrawText method of the drawing context? in particular I'm looking for a glow effect. I am currently drawing the text like this (in the OnRender of my own shape that inherits from UIElement):

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

and if it is possible to add a glow effect to drawn text I also need it to apply when the text changes on subsequent calls to the above line (don't know if that requires additional effort).

Upvotes: 0

Views: 1208

Answers (2)

sellmeadog
sellmeadog

Reputation: 7517

In .NET 4, UIElement.BitmapEffect is obsolete and will not be rendered. Instead, you need to use the UIElement.Effect property. There are fewer effects to choose from, but you can create a glow effect using the DropShadowEffect with the right property settings.

Check out this resource: http://karlshifflett.wordpress.com/2008/11/04/wpf-sample-series-solution-for-the-obsolete-bitmapeffect-property-and-rendering-an-outerglowbitmapeffect/ (note, this sample adds a glow to a Button, but it should apply equally to a Label or TextBlock).

Upvotes: 1

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

I guess glow effects apply to proper Visuals and not to the drawing context members like the DrawText() calls. You may consider drawing a Visual like a TextBlock into the drawing context ...

<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>

And then use the textblock to render as image (alternately you can draw it into the drawing context as well)

   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();

Let me know if this helps....

Upvotes: 1

Related Questions