I use naudio for generating a tone in a specified frequency like that:
private void gen_Sinus(double frequency)
{
WaveOut _myWaveOut = new WaveOut();
SignalGenerator mySinus = new SignalGenerator(44100, 1);//using NAudio.Wave.SampleProviders;
mySinus.Frequency = frequency;
mySinus.Type = SignalGeneratorType.Sin;
_myWaveOut.Init(mySinus);
_myWaveOut.Play();
}
I want that when clicking a button it will play that tone for a specific time that will be passed to this method. Let's call it for example:
double toneDuration
I prefer to prevent some sleep methods because it has to be as accurate as possible.
You can use OffsetSampleProvider to do this, and set the Take duration:
var trimmed = new OffsetSampleProvider(signalGenerator);
trimmed.Take = TimeSpan.FromSeconds(10);
waveOut.Init(trimmed);
waveOut.Play();
Use a timer and a wait handler
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.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
}
static AutoResetEvent autoEvent = new AutoResetEvent(false);
private void gen_Sinus(double frequency)
{
WaveOut _myWaveOut = new WaveOut();
SignalGenerator mySinus = new SignalGenerator(44100, 1);//using NAudio.Wave.SampleProviders;
mySinus.Frequency = frequency;
mySinus.Type = SignalGeneratorType.Sin;
_myWaveOut.Init(mySinus);
timer1.Interval = 5000;
timer1.Start()
_myWaveOut.Play();
autoEvent.Reset();
autoEvent.WaitOne();
_myWaveOut.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
autoEvent.Set();
}
}
}
Related
For this I have a Text Box, that would be the amount of copies that are going to be printed, that is to say if the user puts 2, 2 would be printed and so on.
But I can't figure out how to take the number typed by the user in the textbox and use it in the for condition, I tried to put For(int i =1;i>1;i++) before the If but it generates an infinite loop of the print box and still only prints one copy.
I use this code, and the class RawPrinterHelper
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Código_zpl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
string x = "^XA^LH30,30\n^FO20,10^ADN,90,50^AD^FDHello World^FS\n^XZ";
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName,x);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
I do not have much reputation to comment on the conversation between you and Trix. But I would have suggested you created a method that accepts a parameter. that when calling, you can pass the text.
void printcopies(int numberofcopies){
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
pd.PrinterSettings.Copies = numberofcopies;
....
}
}
....
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.Diagnostics;
using System.IO;
namespace StopwatchTimer
{
public partial class Form1 : Form
{
public string settingsPath = "Settings";
private string settingsFileName = "settings.txt";
private static readonly Stopwatch watch = new Stopwatch();
private long diff = 0, previousTicks = 0, ticksDisplayed = 0;
public Form1()
{
InitializeComponent();
richTextBox1.TabStop = false;
richTextBox1.ReadOnly = true;
richTextBox1.BackColor = Color.White;
richTextBox1.Cursor = Cursors.Arrow;
richTextBox1.Enter += RichTextBox1_Enter;
settingsPath = Path.Combine(Path.GetDirectoryName(Application.LocalUserAppDataPath), settingsPath);
if (!Directory.Exists(settingsPath))
Directory.CreateDirectory(settingsPath);
settingsFileName = Path.Combine(settingsPath, settingsFileName);
if (!File.Exists(settingsFileName))
File.Create(settingsFileName);
string[] settings = File.ReadAllText(settingsFileName).Split(',');
if(settings.Length > 0)
{
}
} radioButton1.Checked = true;
}
I didn't write yet to the text file. I want for example to write to the text file the radioButton1 and radioButton2 states somewhere else in the code and then reading the states back when running the application.
So I'm using Split with ',' but how do I check if the value when reading back is belong to the radioButton1 or radioButton2 or maybe a textBox or a button1 ?
You can use the built in winforms settings:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Username = txtUsername.Text;
Properties.Settings.Default.Password = txtPassword.Text;
Properties.Settings.Default.Save();
}
C# Setting load and save
https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-write-user-settings-at-run-time-with-csharp
"I have been trying to detect canny edge on an image and it has been successful, but I don't know how to detect its edge if I want it real time, here is my code, it has no error but the canny window can't be processed
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 KomCit;
using System.Drawing.Imaging;
using System.IO;
using System.Timers;
using AForge;
using AForge.Imaging.Filters;
using AForge.Video.DirectShow;
using System.Threading;
namespace canny_video
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
void FinalFrame_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
CameraBox.Image = (Bitmap)eventArgs.Frame.Clone();
}
private static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
private void Form1_Load(object sender, EventArgs e)
{
//timer1.Enabled = true;
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
listDevice.Items.Add(Device.Name);
}
listDevice.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
}
private void start_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[listDevice.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new AForge.Video.NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
//CannyBox.Image = (Bitmap)CameraBox.Image.Clone(); //capture image bitmap
//Bitmap gambar = new Bitmap(CameraBox.Image);
Bitmap gambar = new Bitmap(CameraBox.Image);
Grayscale gray = new Grayscale(0.2125, 0.7154, 0.0721);
CannyEdgeDetector cany = new CannyEdgeDetector(0, 70);
Bitmap hasil = cany.Apply(gray.Apply(gambar));
// BlobsFiltering blob = new BlobsFiltering(0, 0, 20, 20);
//Bitmap hasil = blob.Apply(gray.Apply(gambar));
//CannyBox.Width = gambar.Width;
//CannyBox.Height = gambar.Height;
CannyBox.Image = hasil;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}
}
}
"
My problem above is solved, thank you :)
but I have another problem on integral projection, I didn't know how to do it though.
please help, thank you in advance
Some comments:
Your two private variables are very confusingly named. Your CaptureDevice is a collection of all the capture devices on the computer, while your FinalFrame is a capture device.
You do not look for canny edges on the bitmap upon reception.
The NewFrame event handler runs in the streaming thread, so you should use a .BeginInvoke in there to make it usable by the UI thread.
What you may do:
Inside _NewFrame handler, use BeginInvoke to transfer the cloned image to a ProcessCameraImage method (that will hence run on the UI thread).
Apply canny edge detection within this method.
Then assign the result to your picture box.
Note: I don't know how CPU heavy canny edge detection is. If it's costly, doing it on the UI thread may block user interaction. In that case you could do it within the streaming thread and only transfer the processed image for display. But depending on the time it takes, this may force the camera to lower its framerate.
thank you for your answer, it has been solved just now, I've modified it and it somehow solved
here is the code :
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 AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging; //Save P2
using System.IO; //Save P2
namespace AForgeCamera
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap video;
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
int mode;
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
}
private void button1_Click(object sender, EventArgs e) //Tombol Start
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
video = (Bitmap)eventArgs.Frame.Clone();
Bitmap video2 = (Bitmap)eventArgs.Frame.Clone();
if (mode==1)
{
Grayscale gray = new Grayscale(0.2125, 0.7154, 0.0721);
Bitmap video3 = gray.Apply(video2);
CannyEdgeDetector canny = new CannyEdgeDetector(0, 70);
canny.ApplyInPlace(video3);
pictureBox2.Image = video3;
}
pictureBox1.Image = video;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}
private void btnTrackingObject_Click(object sender, EventArgs e)
{
mode = 1;
}
}
}
but now I am working on the next problem, I try to detect the location of the object using integral projection, is it possible to use aforge to do it?
I know how to upload a video to Youtube using Youtube API within C#
But I'd like to use the Youtube API to return a date when a particular user last uploaded a video.
My code for uploading a video using C# is below, but I really don't know how to do the above???
Form1.cs
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 Google.GData.Client;
using Google.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.GData.Extensions.Location;
namespace UploadLimit
{
public partial class Form1 : Form
{
string key = "somegarbage";
string appName = "SlowUpload";
string username = "blarg";
string password = "blarg";
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
txtFile.Text = dialog.FileName;
}
private void btnUpload_Click(object sender, EventArgs e)
{
YouTubeRequestSettings settings = new YouTubeRequestSettings(appName, key, username, password);
YouTubeRequest request = new YouTubeRequest(settings);
Video newVideo = new Video();
newVideo.Title = "My Test Movie";
newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "cars, funny";
newVideo.Description = "My description";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag",
YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
// alternatively, you could just specify a descriptive string
// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(txtFile.Text, "video/quicktime");
Video createdVideo = request.Upload(newVideo);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UploadLimit
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
You can get all the user's videos and get the latest date:
var videos = new YouTubeRequest().GetVideoFeed(userId).Entries
DateTime lastUploadDate = videos.Max(video => video.YouTubeEntry.Published)
To get the actual video's title:
var lastVideo = videos.Where(video => video.YouTubeEntry.Published == lastUploadDate).First();
var name = lastVideo.Title
I am new to c#. I have been trying to add values from my datagrid to mysql but in vain and i really need your help. The values are automatically generated after a time interval of 1 sec and i need to insert them to the mysql in the same time interval. Here are the codes.
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.IO;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace BLIND_SHOPPING_SYSTEM
{
public partial class Form2 : Form
{
private DataTable m_TagDataTable = new DataTable("Tag List");
long passiveID = 0;
public Form2()
{
InitializeComponent();
this.m_TagDataTable.Columns.Add("TagID");
}
private void btn_GenerateID_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
public void timer1_Tick(object sender, EventArgs e)
{
if (passiveID < 10)
{
passiveID = passiveID + 1;
//listBox1.Items.Add(passiveID);
//this.m_TagDataTable.Columns.Add("TagID");
this.dataGridView1.DataSource = m_TagDataTable;
DataRow dr = m_TagDataTable.NewRow();
dr["TagID"] = passiveID;
m_TagDataTable.Rows.Add(dr);
}
else
// Application.Exit();
Console.ReadLine();
}
private void btn_Stop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
}
}
Please kindly help me.
You probably want to use a DataTableAdapter and specify the Update command, see here for some examples ;) http://msdn.microsoft.com/en-us/library/ms233819(v=vs.80).aspx