用C#画一个五线谱

发布于 2024-09-29 23:18:46 字数 231 浏览 10 评论 0原文

我希望在 .NET (C#) 表单上绘制音乐五线谱。我正在使用 Microsoft Visual C# 2010 Express。我想知道是否有人知道现有的代码或现有的免费 .NET 库可以帮助解决这个问题。我正在考虑绘制高音和低音谱号五线谱,并在五线谱的某些位置添加四分之一音符。我正在为我儿子使用 C# 制作一个钢琴测试器应用程序。如果我自己编码,我可能会重写 onPaint 方法。但我想我会看看是否有人看到一些免费的代码或库可以帮助我开始。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

随遇而安 2024-10-06 23:18:46

在 Unicode 代码集中(从 U+1D100 开始)有生成音乐输出所需的原语。例如,U+1D11A 是 5 行五线谱,U+1D158 是封闭符头。

请参阅http://www.unicode.org/charts/PDF/U1D100.pdf

..那么问题就变成了确保你有一个包含适当字形的字体(并处理正确间距的问题等)

如果你想生成打印输出,你应该看看 Lilypond,这是一个OSS乐谱包,使用文本文件格式定义音乐内容,然后生成华丽的输出。

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.

紫﹏色ふ单纯 2024-10-06 23:18:46

您可能会看到几年前用 C# 编写的音乐编辑程序。我看起来有点有希望:
音乐编辑程序

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

岛歌少女 2024-10-06 23:18:46

这将是一个困难的项目。 Finale 对音符和其他符号使用自定义字体。这可能是让您入门的有效方法。

您还可以查看 Niffty。它是开源的并用 Java 编写。你或许可以翻译重要的部分,或者借用概念。

编辑:这也可能有用:http: //www.c-sharpcorner.com/UploadFile/mgold/musicmaker09242005015433AM/musicmaker.aspx

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

巡山小妖精 2024-10-06 23:18:46

如果您仔细想想,绘制乐谱所需的基元数量相当小,特别是如果您不太喜欢的话。您基本上需要的是:

  • 垂直线(音符干)
  • 水平线(五线谱线)
  • 完整且轮廓化的椭圆形(代表音符)
  • 升号和降号已经为您提供了 # 和 b

我省略了一些更奇特的符号,例如高音,低音谱号标记,但您可以将它们与 T 和 B 搭配使用,或者找到可能有用的更高级的字体。

非常简单的示例代码可以帮助您入门:

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);
    }
}

这个绘制函数当然必须更加动态:在基元集合上充满循环......

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...

一瞬间的火花 2024-10-06 23:18:46

之前的评论者提到为此目的将 java 的 Niffty 工具移植到 c#。

事实证明它已经完成了,效果很好。

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

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

未央 2024-10-06 23:18:46

如果您想在自定义 C# 应用程序中使用 1D100.pdf 中所述的 Unicode 音乐符号。以下内容将帮助您开始:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文