代码之家  ›  专栏  ›  技术社区  ›  codymanix

WPF:位图不显示

  •  0
  • codymanix  · 技术社区  · 14 年前

    我想做一些模拟。会有无数的精灵漂浮在周围。因为我认为每帧渲染1000次组成精灵的相同基本体会很慢,所以我希望将它们渲染一次为位图,然后每帧显示一个精灵。

    我的WPF来源很简单:

    <Window x:Class="WPFGraphicsTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="800" Width="1000">
        <Canvas>
    
        </Canvas>
    </Window>
    

    这是我的代码:

        public partial class MainWindow : Window
        {
            Ellipse e;
            RenderTargetBitmap bmp2;
    
            public MainWindow()
            {
                InitializeComponent();
    
                e = new Ellipse();
                e.Width = 40;
                e.Height = 40;
                e.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 200));
    
                ((Canvas)this.Content).Children.Add(e);
                ((Canvas)this.Content).Measure(new Size(1000, 800));
                ((Canvas)this.Content).Arrange(new Rect(new Size(1000, 800)));
    
                RenderTargetBitmap bmp2 = new RenderTargetBitmap(40, 40, 96, 96, PixelFormats.Pbgra32);
    
                bmp2.Render(e);
                ((Canvas)this.Content).Children.Remove(e);
    }
    
    
            protected override void OnRender(DrawingContext drawingContext)
            {
                base.OnRender(drawingContext);
    
                drawingContext.DrawImage(bmp2, new Rect(100,100, 40, 40));
    
            }
    }
    

    为什么这不管用?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Chris Taylor    14 年前

    可以将图像对象放在画布上,然后使用RenderTargetBitmap更新图像。例如

    <Window x:Class="WPFGraphicsTest.MainWindow" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            Title="MainWindow" Height="800" Width="1000"> 
        <Canvas> 
          <Image Name="frameImage" />
        </Canvas> 
    </Window> 
    

    public partial class MainWindow : Window
      {
        DispatcherTimer _timer = new DispatcherTimer();
    
        RenderTargetBitmap _renderSurface = 
          new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);
    
        Random _rnd = new Random();
    
        public MainWindow()
        {
          InitializeComponent();
    
          _timer = new DispatcherTimer();
          _timer.Interval = TimeSpan.FromMilliseconds(100);
          _timer.Tick += new EventHandler(_timer_Tick);
          _timer.Start();      
        }
    
        void _timer_Tick(object sender, EventArgs e)
        {
          DrawingVisual visual = new DrawingVisual();
          DrawingContext context = visual.RenderOpen();
          int value = _rnd.Next(40);
          context.DrawEllipse(
            new SolidColorBrush(Colors.Red), 
            new Pen(new SolidColorBrush(Colors.Black), 1), 
            new Point(value, value), value, value);
          context.Close();
    
          _renderSurface.Render(visual);
          frameImage.Source = _renderSurface;
        }    
      }