diff options
Diffstat (limited to 'WindowsFormsApplication2/WindowsFormsApplication2/ChooseOptions.cs')
-rw-r--r-- | WindowsFormsApplication2/WindowsFormsApplication2/ChooseOptions.cs | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/WindowsFormsApplication2/WindowsFormsApplication2/ChooseOptions.cs b/WindowsFormsApplication2/WindowsFormsApplication2/ChooseOptions.cs new file mode 100644 index 0000000..2e4ae52 --- /dev/null +++ b/WindowsFormsApplication2/WindowsFormsApplication2/ChooseOptions.cs @@ -0,0 +1,239 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using System.Runtime.InteropServices; +using NAudio.Wave;//Reference for incorporating Record functionality (Open Source library available from CodePlex) NAudio.dll +using System.Diagnostics; + +namespace MSDNForums +{ + public partial class ChooseOptions : Form + { + public ChooseOptions() + { + InitializeComponent(); + } + int i=0; //used for Toggling the Record button on and off + string textBoxImagePath; + private WaveInStream waveInStream; + private WaveFileWriter writer; + private string outputFilename;//used to store the name of the saved record file + + + public ChooseOptions(LayoutItem item) + { + InitializeComponent(); + //Panel1 backcolor and padding used to give the PictureBox a border like effect + panel1.BackColor = Color.Black; + pictureBox1.BackColor = Color.White; + Cancelbtn.DialogResult = DialogResult.Cancel;//It works automatically. + Item = item; + if (!string.IsNullOrEmpty(item.ItemPcitureBox.ImageLocation)) + { + textBoxImagePath = item.ItemPcitureBox.ImageLocation; + } + } + public Image Image { get; set; } + private void Deletebtn_Click(object sender, EventArgs e) + { + pictureBox1.Image = null; + } + private void Pastebtn_Click(object sender, EventArgs e) + { + if (Clipboard.GetDataObject().GetDataPresent("Bitmap")) + { + object o = Clipboard.GetDataObject().GetData("Bitmap"); + if (o != null) + { + pictureBox1.Image = (Image)o; + } + } + pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; + } + private void Openbtn_Click(object sender, EventArgs e) + { + OpenFileDialog ofdButtonPicture = new OpenFileDialog(); + + ofdButtonPicture.Title = "Select Picture for the Button."; + ofdButtonPicture.Multiselect = false; + ofdButtonPicture.Filter = "Picture Files(BMP, JPG, GIF, PNG)|*.bmp;*.jpg;*.gif;*.png|All Files(*.*)|*.*"; + if (ofdButtonPicture.ShowDialog() == System.Windows.Forms.DialogResult.OK) + { + try + { + pictureBox1.Image = System.Drawing.Image.FromFile(ofdButtonPicture.FileName); + //Image path >>> Similarly the sound path will be specified + textBoxImagePath = ofdButtonPicture.FileName; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Failed Setting Image", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } + pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; + } + private void OKbtn_Click(object sender, EventArgs e) + { + //Check if image is selected + if (string.IsNullOrEmpty(textBoxImagePath)) + { + MessageBox.Show("Please select an image"); + return; + } + //We release unmanaged memory if image is not null + if (Item.ItemPcitureBox.Image != null) + { + Item.ItemPcitureBox.Image.Dispose(); + Item.ItemPcitureBox.Tag = null; + } + //We release unmanaged memory if SoundPath is not null + if (Item.SoundPath != null) + { + Item.SoundPath = null; + } + //Check if if the text of the object has been specified or Sound has been recorded + if (string.IsNullOrEmpty(txtRead.Text) && string.IsNullOrEmpty(outputFilename)) + { + MessageBox.Show("Specify the text or record a sound"); + return; + } + Item.ItemPcitureBox.Image = Image.FromFile(textBoxImagePath); + if (outputFilename == null) + { + Item.ItemPcitureBox.Tag = txtRead.Text; + } + else + { + Item.ItemPcitureBox.Tag = outputFilename; + } + //Item.ItemLabel.Text = txtShow.Text; + Item.pnlcolor.BackColor = panel1.BackColor; + Item.pnlcolor.Padding = new Padding { All = 2 }; + DialogResult = DialogResult.OK; + Close(); + } + private LayoutItem Item + { + get; + set; + } + private void button1_Click(object sender, EventArgs e) + { + //Record & Stop Toggle button + Button btnStrtStop = (Button)sender; + if (i == 0) + { + //Record code + if (waveInStream == null) + { + if (outputFilename == null) + { + SaveFileDialog saveFileDialog = new SaveFileDialog(); + saveFileDialog.Title = "Select output file:"; + saveFileDialog.Filter = "WAV Files (*.wav)|*.wav"; + + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + outputFilename = saveFileDialog.FileName; + btnStrtStop.BackgroundImage = System.Drawing.Image.FromFile("RecordPressed.png"); + toolTip1.SetToolTip(button1, "Stop"); + i = 1; + } + else + { + i = 0; + } + } + if (outputFilename == null) + { + return; + } + WaveFormat recordingFormat = new WaveFormat(8000, 16, 1); + writer = new WaveFileWriter(outputFilename, recordingFormat); + + waveInStream = new WaveInStream(0, recordingFormat, this); + waveInStream.DataAvailable += waveInStream_DataAvailable; + waveInStream.StartRecording(); + } + } + else + { + btnStrtStop.BackgroundImage = System.Drawing.Image.FromFile("RecordHot.png"); + toolTip1.SetToolTip(button1, "Start"); + i = 0; + if (waveInStream != null) + { + StopRecording(); + } + } + } + private void button2_Click(object sender, EventArgs e) + { + //Process.Start(outputFilename); + //Process can be used to play the file in Media Player + //Here I have used Sound Player instead of Media Player + System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(); + myPlayer.SoundLocation = outputFilename; + myPlayer.Play(); + } + private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) + { + //Apply the selected color to the border of the PictureBox + if (comboBox1.SelectedIndex == 0) + { + panel1.BackColor = Color.Black; + } + else if (comboBox1.SelectedIndex==1) + { + panel1.BackColor=Color.Blue; + } + else if (comboBox1.SelectedIndex==2) + { + panel1.BackColor = Color.Green; + } + else if (comboBox1.SelectedIndex==3) + { + panel1.BackColor = Color.Orange; + } + else if (comboBox1.SelectedIndex == 4) + { + panel1.BackColor = Color.Red; + } + else + { + panel1.BackColor = Color.Yellow; + } + } + private void ChooseOptions_Load(object sender, EventArgs e) + { + this.comboBox1.SelectedItem = "Black"; + //this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); + //pictureBox1.InitialImage = System.Drawing.Image.FromFile("NoImage.png"); + } + + //Methods + private void StopRecording() + { + waveInStream.StopRecording(); + waveInStream.Dispose(); + waveInStream = null; + writer.Close(); + writer = null; + } + private void waveInStream_DataAvailable(object sender, WaveInEventArgs e) + { + writer.WriteData(e.Buffer, 0, e.BytesRecorded); + int secondsRecorded = Convert.ToInt32((writer.Length / writer.WaveFormat.AverageBytesPerSecond)); + if (i == 0) + { + StopRecording(); + } + } + } +} |