How do I avoid an OutOfMemoryException when using DropShadowEffect?
When a DropShadowEffect is added, zooming in with ScaleTransform needs a lot of memory. Too much memory! Without the DropShaddowEffect, there's no problem.
I do not understand, why it is leaking memory and what I could do to work around.
Here's the code for a simple test program:
XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Canvas x:Name="canvas" Height="100" Width="100"
Background="Beige" Grid.Row="0">
<Canvas.RenderTransform>
<ScaleTransform ScaleX="{Binding ElementName=slide, Path=Value}"
ScaleY="{Binding ElementName=slide, Path开发者_Python百科=Value}" />
</Canvas.RenderTransform>
</Canvas>
<Grid Grid.Row="1">
<Slider x:Name="slide" Minimum="1" Maximum="200"/>
</Grid>
</Grid>
CodeBehind:
var dropShadowEffect = new DropShadowEffect();
canvas.Effect = dropShadowEffect;
You should read this blog: http://youpvp.com/blog/post/What-you-need-to-know-about-DropShadow-to-create-great-Silverlight-applications.aspx
Pretty much the memory usage you see is in line with that should be expected. Shadow for 500x500 pixels canvas (5x zoom in your case) would take >1MB memory. Bigger shadow will take even more.
Is this .net 3.5 or 4.0? With 3.5 the drop shadow effect should be avoided to due rendering issues with it.
http://joshsmithonwpf.wordpress.com/2007/07/24/a-lightweight-alternative-to-dropshadowbitmapeffect/
精彩评论