Drawing Pixels in WPF

后端 未结 5 903
日久生厌
日久生厌 2020-12-01 06:37

how would I manage pixel-by-pixel rendering in WPF (like, say, for a raytracer)? My initial guess was to create a BitmapImage, modify the buffer, and display that in an Imag

5条回答
  •  半阙折子戏
    2020-12-01 06:55

    You can add small rects if you want, but each of those is a FrameworkElement, so it's probably a bit heavyweight. The other option is to create yourself a DrawingVisual, draw on it, render it then stick it in an image:

    private void DrawRubbish()
    {
        DrawingVisual dv = new DrawingVisual();
        using (DrawingContext dc = dv.RenderOpen())
        {
            Random rand = new Random();
    
            for (int i = 0; i < 200; i++)
                dc.DrawRectangle(Brushes.Red, null, new Rect(rand.NextDouble() * 200, rand.NextDouble() * 200, 1, 1));
    
            dc.Close();
        }
        RenderTargetBitmap rtb = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
        rtb.Render(dv);
        Image img = new Image();
        img.Source = rtb;
        MainGrid.Children.Add(img);
    }
    

提交回复
热议问题