Question

I am looking to draw a music staff on a .NET (C#) form. I am using Microsoft Visual C# 2010 Express. I was wondering if anyone knew of existing code or existing free .NET libraries that can help with that. I am looking at drawing both the treble and bass clef staff and adding one quarter note to some where in the staff. I am making a Piano Tester app using C# for my son. If I code it myself, I will proably just override the onPaint method. But I thought I would see if anyone has seen some free code or library available to get me started.

Was it helpful?

Solution

There are the required primitives to generate musical output in the Unicode code set (starting at U+1D100). For example, U+1D11A is a 5-line staff, U+1D158 is a closed notehead.

See http://www.unicode.org/charts/PDF/U1D100.pdf

..then the issue becomes making sure that you have a typeface with the appropriate glyphs included (and dealing with the issues of spacing things correctly, etc.)

IF you're looking to generate printed output, you should look at Lilypond, which is an OSS music notation package that uses a text file format to define the musical content and then generates gorgeous output.

OTHER TIPS

You might look at a music editing program written in C# a few years ago. I looks somewhat promising: Music Editing Program

This will be a difficult project. Finale uses a custom font for notes and other symbols. That might be an efficient way to get you started.

You might also check out Niffty. It is open source and written in Java. You could probably translate the important parts over, or borrow concepts.

Edit: This may also be useful: http://www.c-sharpcorner.com/UploadFile/mgold/musicmaker09242005015433AM/musicmaker.aspx

If you think about it, the number of primitives needed to draw music notation is fairly small, especially if you don't get too fancy. All you basically need is:

  • Vertical lines (note stems)
  • Horizontal lines (staff lines)
  • Ovals full and outlined (representing notes)
  • Sharp and Flats are already provided for you with # and b

There are some fancier symbols that i omitted such as treble, bass clef marks but those you could cheese with T and B or find a fancier font that might work.

Very simple, sample code to get you started:

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

public partial class MusicForm : Form
{
    public MusicForm()
    {
        InitializeComponent();
    }

    private int _staffHght = 15;
    private int _noteHght = 12;
    private int _noteWdth = 20;
    private Pen _notePen = new Pen(Color.Black, 2);
    private Brush _noteBrush = Brushes.Black;

    private void musicPanel_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = SmoothingMode.HighQuality;

        // draw some staff lines
        for (int i = 1; i < 6; i++)
            g.DrawLine(Pens.Black, 0, i * _staffHght, musicPanel.Width, i * _staffHght);

        // draw four semi-random full and quarter notes
        g.DrawEllipse(_notePen, 10, 2 * _staffHght, _noteWdth, _noteHght);
        g.DrawEllipse(_notePen, 50, 4 * _staffHght, _noteWdth, _noteHght);

        g.FillEllipse(_noteBrush, 100, 2 * _staffHght, _noteWdth, _noteHght);
        g.FillEllipse(_noteBrush, 150, 4 * _staffHght, _noteWdth, _noteHght);
    }
}

This paint function would of course have to be much more dynamic: full of loops on your collections of primitives...

If you like to use the unicode Musical Symbols as descibed in the 1D100.pdf in a custom C# application. The following will get you started:

A previous commentator mentioned porting java's Niffty tool to c# for this purpose.

Turns out it's already been done, works nicely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top