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 9230729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:53:39+00:00 2026-06-18T05:53:39+00:00

I created a minimal project in order to get going with drawing with DrawingVisuals

  • 0

I created a minimal project in order to “get going” with drawing with DrawingVisuals in WPF (being very beginner so far).

My project contains only XAML and code behind for the main window. The only purpose of this project is open a window and display some “signal noise” filling the available space.

MainWindow.xaml is this:

<Window x:Class="MinimalPlotter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MinimalPlotter"
        Title="MainWindow" Width="1200" Height="800"
        WindowStartupLocation="CenterScreen">

        <Canvas Height="500" Width="700" x:Name="drawingArea" Margin="50" Background="Gray">
            <local:VisualHost Canvas.Top="0" Canvas.Left="0" x:Name="host" Width="700" Height="500" />
        </Canvas>
</Window>

And code behind is this:

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

    public class VisualHost : FrameworkElement
    {

        public VisualHost()
        {
            int h = (int)this.Height;
            int w = (int)this.Width;

            Random random = new Random();
            double r;

            DrawingVisual path = new DrawingVisual();

            StreamGeometry g = new StreamGeometry();
            StreamGeometryContext cr = g.Open();
            cr.BeginFigure(new Point(0,0), false, false);
            for (int i = 0; i < w; i++)
            {
                // ugly calculations below to get the signal centered in container
                r = (random.NextDouble()-0.5) * h -h/2;
                cr.LineTo(new Point(i, r), true, false);
            }
            cr.Close();
            g.Freeze();

            DrawingContext crx = path.RenderOpen();
            Pen p = new Pen(Brushes.Black, 2);
            crx.DrawGeometry(null, p, g);

            // Ellipse included for "visual debugging"
            crx.DrawEllipse(Brushes.Red, p, new Point(50,50), 45, 20);

            crx.Close();

            this.AddVisualChild(path);
        }
    }
}

The problem is: when the window opens, the Canvas shows up in the center, as expected (with a gray background), but no signal gets plotted. A previous version of this code worked fine using Path geometry, but with DrawingVisual no geometry is shown (not even the ellipse geometry included for debugging).

Thanks for reading!

  • 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-18T05:53:40+00:00Added an answer on June 18, 2026 at 5:53 am

    Your VisualHost class would also have to override the VisualChildrenCount property and the GetVisualChild method:

    public class VisualHost : FrameworkElement
    {
        private DrawingVisual path = new DrawingVisual();
    
        public VisualHost()
        {
            ...
            AddVisualChild(path);
        }
    
        protected override int VisualChildrenCount
        {
            get { return 1; }
        }
    
        protected override Visual GetVisualChild(int index)
        {
            return path;
        }
    }
    

    Please note also that it is considered good practice to use IDisposable objects like StreamGeometryContext and DrawingContext in using statements:

    var g = new StreamGeometry();
    using (var cr = g.Open())
    {
        cr.BeginFigure(new Point(0,0), false, false);
        ...
        // no need for cr.Close()
    }
    
    using (var crx = path.RenderOpen())
    {
        var p = new Pen(Brushes.Black, 2);
        crx.DrawGeometry(null, p, g);
        crx.DrawEllipse(Brushes.Red, p, new Point(50,50), 45, 20);
        // no need for crx.Close()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to prototype a GUI and created a minimal WPF project using
I have created a minimal RCP project with the Hello World example. I have
I have created a minimal maven project with a single child module in eclipse
Sample: I've created a minimal set of files that highlight the issue here: http://uploads.omega.org.uk/Foo3.zip
I've got a minimal app I just created, using VS 2008 SP1 on Vista
This is just for my weekend project/study, I am very new to Sinatra and
I'm implementing the Hungarian algorithm in a project. I managed to get it working
I'm implementing the Hungarian algorithm in a project. I managed to get it working
I have folowing problem. I got 2 views in my project,one is created programmicaly
Im working on a new minimal Project, but i've got an error, i dont

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.