为什么BitmapEffect无法在wpf中使用自定义画布?

时间:2019-06-17 14:12:33

标签: c# wpf graphics

我正在开发一个绘图程序,其中在绘图组中使用了自定义画布和几何图形。

我正在尝试在图形组上添加位图效果,我想使用自定义画布的OnRender()方法显示它,但是位图效果没有显示任何效果。

WPF-XAML

<Window x:Class="WpfApp2.MainWindow"
       <!--..............
       some default codes
       ...............-->
       Title="MainWindow" Height="450" Width="800" KeyDown="Window_KeyDown">
   <Grid>
       <ScrollViewer HorizontalScrollBarVisibility="Visible" Name="scrollViewer">
       </ScrollViewer>
   </Grid>
</Window>

C#  我的自定义画布类

public class MyCanvas : Canvas
{
   public DrawingGroup drawingGroup;

   public MyCanvas()
   {
       drawingGroup = new DrawingGroup();
   }

   protected override void OnRender(DrawingContext dc)
   {
       base.OnRender(dc);
       dc.DrawDrawing(drawingGroup);
   }
}

C#  我的主程序

// Creating my custom canvas
MyCanvas Pad = new MyCanvas()
{
   Name = "Pad",
   Width = 500,
   Height =500,    
   ClipToBounds = true
};

public MainWindow()
{
   InitializeComponent();

   //adding canvas to scrollviewer
   scrollViewer.Content = Pad;

   //adding mouse event
   Pad.MouseDown += Pad_MouseDown;

}

private void Pad_MouseDown(object sender, MouseButtonEventArgs e)
{
   //Creating new geometry
   EllipseGeometry ellipse= new EllipseGeometry(e.GetPosition((Canvas)sender), 50, 50);

   //Creating drawing using geometry
   GeometryDrawing geometryDrawing = new GeometryDrawing(Brushes.Red, (Pen)null, ellipse);

   //Creating drawing group in order to add Bitmap effect
   DrawingGroup drawingGroup = new DrawingGroup();
   drawingGroup.Children.Add(geometryDrawing);

   //Creating a bitmap effect
   BlurBitmapEffect blur = new BlurBitmapEffect();
   blur.Radius = 50;

   //Adding Bitmap effect to drawing group
   drawingGroup.BitmapEffect = blur;

   //Adding drawing group to my custom canvas
   Pad.drawingGroup.Children.Add(geometryDrawing);
}

0 个答案:

没有答案