Вывод изображений в форму

Я создаю пианино, которое при нажатии на музыкальную клавишу в форме выводится изображение музыкальной ноты, однако изображение не выводится. Я уверен, что путь к изображению для растровых изображений правильный, поэтому я не уверен, что я сделал что-то не так в коде или мне нужно добавить некоторые события в саму форму. Может кто-нибудь помочь мне с этим, пожалуйста?

Это класс Form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.Threading;

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

        SoundPlayer sp = new SoundPlayer();
        int count;
        private Control mn;
        int xLoc = 50;
        int yLoc = 30;
        List<MusicNote> notes = new List<MusicNote>();

        private void Form1_Load(object sender, System.EventArgs e)
        {

            MusKey mk;
            BlackMusKey bmk;

            int[] whitePitch = { 1, 3, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 24 };
            int[] blackPitch = new int[] { 2, 4, 7, 9, 11, 14, 16, 19, 21, 23 };
            int[] xPos = new int[] { 10, 30, 70, 90, 110, 150, 170, 210, 230, 250 };

            for (int k = 0; k < 7; k++)
            {
                int iNote = whitePitch[k];
                int xpos = k * 40;
                mk = new MusKey(iNote, xpos + 225, 300);
                mk.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
                mk.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
                this.panel1.Controls.Add(mk);
            }

            int xOffs = 20;

            for (int k = 0; k < 5; k++)
            {
                int iNote = blackPitch[k];
                int xpos = xPos[k] * 2;
                bmk = new BlackMusKey(iNote, xpos + 225, 300);
                bmk.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
                bmk.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
                this.panel1.Controls.Add(bmk);
                this.panel1.Controls[this.panel1.Controls.Count - 1].BringToFront();
            }
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            foreach(MusKey mk in this.panel1.Controls.OfType<MusKey>())
            {
                if(sender == mk)
                {
                    if(e.Button == MouseButtons.Left)
                    {
                        timer1.Enabled = true;
                        count = 0;
                        timer1.Start();
                        sp.SoundLocation = @"C:/Users/Kim/Desktop/Notes-Sound files/mapped/" + mk.musicNote + ".wav"; //change this to the location of your "mapped" folder
                        sp.Load();
                        sp.Play();
                    }
                }
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            count ++;
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            foreach(MusKey mk in this.panel1.Controls.OfType<MusKey>())
            {
                if (sender == mk)
                {
                    if (e.Button == MouseButtons.Left)
                        timer1.Enabled = false;

                    textBox1.Text = count.ToString();
                    sp.Stop();
                    string bNoteShape = null;
                    int duration = 0;

                    //Insert note lengths 

                    if (count >= 16)
                    {
                        bNoteShape = "SemiBreve";
                        duration = (16 + 20) / 2;
                    }
                    else if ((count >= 11) && (count <= 15))
                    {
                        bNoteShape = "DotMin";
                        duration = (11 + 15) / 2; //average
                    }
                    else if ((count >= 6) && (count <= 10))
                    {
                        bNoteShape = "Minim";
                        duration = (6 + 10) / 2; //average
                    }
                    else if ((count >= 3) && (count <= 5))
                    {
                        bNoteShape = "Crotchet";
                        duration = (3 + 5) / 2; //average
                    }
                    else if ((count == 2))
                    {
                        bNoteShape = "Quaver";
                        duration = 2; //average
                    }
                    else if ((count >= 0) || (count <= 1))
                    {
                        bNoteShape = "SemiQuaver";
                        duration = 1;
                    }

                    //copied from handout

                    MusicNote mn = new MusicNote(mk.musicNote, duration, bNoteShape, xLoc);
                    notes.Add(mn);
                    mn.Location = new Point(xLoc, yLoc);
                    this.panel1.Controls.Add(this.mn);

                    xLoc = xLoc + 40;


                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics graphicsObj;

            graphicsObj = this.panel1.CreateGraphics();

            Pen myPen = new Pen(System.Drawing.Color.Black, 1);

            for (int k = 0; k < 5; k++)
            {
                int ypos = (k * 15) + 20;
                graphicsObj.DrawLine(myPen, 20, ypos, 550, ypos);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (MusicNote mn in notes)
            {
                sp.SoundLocation = @"C:/Users/Kim/Desktop/Notes-Sound files/mapped/" + mn.pitch + ".wav"; //change this to the location of your "mapped" folder
                sp.Play();
                Thread.Sleep(mn.duration * 150);

            }
        }
    }
}

Это класс Music Note:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewPiano
{
    public class MusicNote : PictureBox
    {


        Boolean isDragging = false;
        public int pitch = 0;
        public string noteshape = "";
        public int duration = 0;
        internal Point Location;
        public int xLocation = 0;


        public MusicNote(int notePitch, int duration, string bNoteShape, int xLoc) : base()
        {

            this.noteshape = bNoteShape;
            this.duration = duration;
            this.pitch = notePitch;
            Location = new Point(xLoc, pitch);
            this.Size = new Size(30, 30);



            Bitmap bmp = new Bitmap(@"C:/Users/Kim/Desktop/Notes-Images/" + noteshape + ".bmp");
            this.Image = bmp;


            this.MouseDown += new MouseEventHandler(StartDrag);
            this.MouseUp += new MouseEventHandler(StopDrag);
            this.MouseMove += new MouseEventHandler(NoteDrag);

        }

        private void StartDrag(object sender, MouseEventArgs e) {

            if(e.Button == MouseButtons.Left) {
                isDragging = true;
                pitch = e.Y;
                this.Location = new Point(this.Location.X, pitch);
            }
        }

        private void StopDrag(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                isDragging = false;
                pitch = e.Y;
            }
        }


        private void NoteDrag(object sender, MouseEventArgs e)
        {

            if (isDragging)
            {
                this.Top = this.Top + (e.Y - this.pitch);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

    }
 }

0 ответов

Другие вопросы по тегам