使用DropShadowEffect时如何避免OutOfMemoryException?

时间:2011-08-29 14:27:38

标签: c# silverlight

添加DropShadowEffect后,使用ScaleTransform进行放大需要大量内存。记忆太多了!没有DropShaddowEffect,没有问题。

我不明白,为什么它会泄漏记忆以及我可以做些什么来解决。

以下是简单测试程序的代码:

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=Value}" />
    </Canvas.RenderTransform>
  </Canvas>
  <Grid Grid.Row="1">
    <Slider x:Name="slide" Minimum="1" Maximum="200"/>
  </Grid>
</Grid>

代码隐藏:

var dropShadowEffect = new DropShadowEffect();
canvas.Effect = dropShadowEffect;

2 个答案:

答案 0 :(得分:1)

这是.net 3.5还是4.0?使用3.5时,应该避免投影效果,因为它会产生渲染问题。

http://joshsmithonwpf.wordpress.com/2007/07/24/a-lightweight-alternative-to-dropshadowbitmapeffect/

答案 1 :(得分:1)

你应该阅读这篇博客: http://youpvp.com/blog/post/What-you-need-to-know-about-DropShadow-to-create-great-Silverlight-applications.aspx

你看到的内存使用情况与预期的一致。 500x500像素画布的阴影(在您的情况下为5倍变焦)将占用> 1MB内存。更大的阴影将需要更多。