我猜發光效果應用到適當的視覺效果,而不是繪製背景的成員,如DrawText的()調用。您可以考慮繪製視覺像TextBlock
到圖形上下文...
<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>
然後使用文本塊渲染爲圖像(或者你可以繪製成圖形上下文中還)
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();
讓我知道這是否有幫助....
它的工作(.NET 3.5),但我會記住這是一個很好的備份方案,因爲我通過簡單地畫它恢復到其上繪製文本的文本框一個dropshadoweffect和模擬dropshadoweffect的簡單場景的瞬間兩次。它看起來很好,這很好,並且與.NET 4兼容。 – mtijn