Sunday, 30 April 2017

c# soundcard speaker / microphone audio recorder, screenshot images






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CSCore;
using CSCore.SoundIn;
using CSCore.Codecs.WAV;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        //microphone souund record

        [DllImport("winmm.dll")]
        private static extern int mciSendString(string MciComando, string MciRetorno, int MciRetornoLeng, int CallBack);

        //sound card record

        WasapiCapture capture;
        WaveWriter w;

        public Form1()
        {
            InitializeComponent();
        }

        private int picnum = 1;
        private int videonum = 1;

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Rectangle bounds = this.Bounds;
            //Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
            Bitmap bitmap = new Bitmap(640, 360);
            Graphics g = Graphics.FromImage(bitmap);

            g.CopyFromScreen(new Point(0, 209), Point.Empty, new Size(640, 360));
            bitmap.Save("test" + picnum + ".jpg");
            picnum++;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            textBox1.Text = Cursor.Position.X.ToString();
            textBox2.Text = Cursor.Position.Y.ToString();
        }

        private void record_Click(object sender, EventArgs e)
        {
            record_label.Visible = true;

            //record sound with sound card

            capture = new WasapiLoopbackCapture();

            //initialize the selected device for recording
            capture.Initialize();

            //create a wavewriter to write the data to

            w = new WaveWriter("soundcard" + videonum + ".wav", capture.WaveFormat);

            //setup an eventhandler to receive the recorded data
            capture.DataAvailable += (s, e1) =>
            {
                //save the recorded audio
                w.Write(e1.Data, e1.Offset, e1.ByteCount);
            };

            //start recording
            capture.Start();

            videonum++;
        }

        private void stop_save_Click(object sender, EventArgs e)
        {
            record_label.Visible = false;

            //sound card record

            //stop recording
            capture.Stop();

            w.Dispose();
            capture.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            record_label.Visible = false;
            record_label1.Visible = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            w.Dispose();
            capture.Dispose();
        }

        private void record1_Click(object sender, EventArgs e)
        {
            record_label1.Visible = true;

            //record sound with microphone

            mciSendString("open new type waveaudio alias Som", null, 0, 0);
            mciSendString("record Som", null, 0, 0);
        }

        private void stop_save1_Click(object sender, EventArgs e)
        {
            record_label1.Visible = false;

            //microphone record

            mciSendString("pause Som", null, 0, 0);
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "wave|*.wav";

            if (save.ShowDialog() == DialogResult.OK)
            {

                mciSendString("save Som " + save.FileName, null, 0, 0);
                mciSendString("close Som", null, 0, 0);
            }
        }

        private void start_video_Click(object sender, EventArgs e)
        {
            button1.PerformClick();
            record.PerformClick();
        }

        private void stop_video_Click(object sender, EventArgs e)
        {
            button2.PerformClick();
            stop_save.PerformClick();
        }
    }
}

No comments:

Post a Comment