Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:05:30+00:00 2026-06-13T08:05:30+00:00

Here’s a Windows Forms program which draws a two-dimensional grid of squares that are

  • 0

Here’s a Windows Forms program which draws a two-dimensional grid of squares that are randomly colored black or red:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Forms_Panel_Random_Squares
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Width = 350;
            Height = 350;

            var panel = new Panel() { Dock = DockStyle.Fill };

            Controls.Add(panel);

            var random = new Random();

            panel.Paint += (sender, e) =>
                {
                    e.Graphics.Clear(Color.Black);

                    for (int i = 0; i < 30; i++)
                        for (int j = 0; j < 30; j++)
                        {
                            if (random.Next(2) == 1)
                                e.Graphics.FillRectangle(
                                    new SolidBrush(Color.Red),
                                    i * 10,
                                    j * 10,
                                    10,
                                    10);
                        }
                };
        }
    }
}

The resulting program looks something like this:

enter image description here

Here’s a (naive) translation to WPF using Rectangle objects for each square:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WPF_Canvas_Random_Squares
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Width = 350;
            Height = 350;

            var canvas = new Canvas();

            Content = canvas;

            Random random = new Random();

            Rectangle[,] rectangles = new Rectangle[30, 30];

            for (int i = 0; i < rectangles.GetLength(0); i++)
                for (int j = 0; j < rectangles.GetLength(1); j++)
                {
                    rectangles[i, j] =
                        new Rectangle()
                        {
                            Width = 10,
                            Height = 10,
                            Fill = random.Next(2) == 0 ? Brushes.Black : Brushes.Red,
                            RenderTransform = new TranslateTransform(i * 10, j * 10)
                        };

                    canvas.Children.Add(rectangles[i, j]);
                }
        }
    }
}

The WPF version seems to be way more memory inefficient due to the fact that each cell in the world has the overhead of a Rectangle object.

Is there a way to write this program in a style that’s as efficient as the Forms version? Or is there no way around creating all those Rectangle objects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:05:35+00:00Added an answer on June 13, 2026 at 8:05 am

    Here’s a pure WPF solution. FrameworkElement is subclassed. This new subclass (DrawingVisualElement) exposes a DrawingVisual object which can be used to draw.

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    
    namespace DrawingVisualSample
    {
        public class DrawingVisualElement : FrameworkElement
        {
            private VisualCollection _children;
    
            public DrawingVisual drawingVisual;
    
            public DrawingVisualElement()
            {
                _children = new VisualCollection(this);
    
                drawingVisual = new DrawingVisual();
                _children.Add(drawingVisual);
            }
    
            protected override int VisualChildrenCount
            { 
                get { return _children.Count; } 
            }
    
            protected override Visual GetVisualChild(int index)
            {
                if (index < 0 || index >= _children.Count)
                    throw new ArgumentOutOfRangeException();
    
                return _children[index];
            }
        }
    
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                Width = 350;
                Height = 350;
    
                var stackPanel = new StackPanel();
    
                Content = stackPanel;
    
                var drawingVisualElement = new DrawingVisualElement();
    
                stackPanel.Children.Add(drawingVisualElement);
    
                var drawingContext = drawingVisualElement.drawingVisual.RenderOpen();
    
                var random = new Random();
    
                for (int i = 0; i < 30; i++)
                    for (int j = 0; j < 30; j++)    
                        drawingContext.DrawRectangle(
                            random.Next(2) == 0 ? Brushes.Black : Brushes.Red,
                            (Pen)null,
                            new Rect(i * 10, j * 10, 10, 10));
    
                drawingContext.Close();
            }
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the problem that I am trying to solve. I have two folders
Here is my code, which takes two version identifiers in the form 1, 5,
Here is the two scripts I have Script 1: <? include('config.php'); $json = $_POST['payload'];
Here is my program to find all the subsets of given set. To solve
Here is my scenario. I have a website running under AppPool1 and that works
Here is my problem.. I have the option to create(add) two new input fields,
Here are the tables I have: Table A which has entries with item and
Here's the code I have. It works. The only problem is that the first
Here's the two scenarios. We are using a manually built xml soap request with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.