Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/WindowsFormsApplication2/WindowsFormsApplication2/ChooseOptions.cs
blob: 2e4ae52964481c8bbd6dd938e9b05cb40b8fba7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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();
            }
        }
    }
}