I am still new at programming, I code as a hobby :)
I wanted to make a MJPEG streaming aplication with an option to record a video and save it on computer when it detects a moving. So far i've managed to get stream to videosourceplayer in WPF, but it seems that i can't manage to get the information whether moving is detected or not. What am i doing wrong? i've tried many things ( commented ). but it seems that i can't compare float MotionDetected to limited (in my case 0.02). my label5 simply doesnt change.
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.Vision.Motion;
using AForge.Video;
using AForge.Video.DirectShow;
using Accord.Video.FFMPEG;
using System.Globalization;
using AForge.Controls;
namespace AnotherAForge
{
public partial class Form1 : Form
{
MotionDetector Detector;
float f;
float MotionDetected;
int frames;
private VideoFileWriter _writer;
private MJPEGStream stream = new MJPEGStream("link to the cam i can't share");
int uporabimotiondetection = 0;
private IVideoSource _videoSource;
public Form1()
{
InitializeComponent();
}
Form form1 = new Form();
public void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
if (uporabimotiondetection == 1)
{
MotionDetected = Detector.ProcessFrame(image);
//if (label5.InvokeRequired) {
// label5.Text = NivelDeDeteccion.ToString();
//}
//if (Detector.ProcessFrame(image) > 0.02)
//{
// label5.Text = "1";
//}
}
frames++;
}
private void alarm() {
label2.Text = MotionDetected.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
//VideoStream.NewFrame += VideoStream_NewFrame;
//VideoStream.NewFrame += new NewFrameEventHandler(ProcessNewFrame);
stream.Start();
//stream.NewFrame += Stream_NewFrame;
//videoSourcePlayer1.NewFrame += videoSourcePlayer1_NewFrame;
videoSourcePlayer1.VideoSource = stream;
timer1.Enabled = true;
timer1.Start();
timer2.Start();
//pictureBox1.Image = image;
GC.Collect();
}
private void Form1_Load(object sender, EventArgs e)
{
Detector = new MotionDetector(new TwoFramesDifferenceDetector(),new MotionAreaHighlighting());
MotionDetected = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
// textBox1.Text = NivelDeDeteccion.ToString();
label3.Text = frames.ToString() + " frames";
label2.Text = MotionDetected.ToString();
if (MotionDetected <= 0.02 && MotionDetected >= 0)
{
label5.Text = "1";
}
else {
label5.Text = "0";
}
}
private void button2_Click(object sender, EventArgs e)
{
Console.Beep();
}
int time;
private void timer2_Tick(object sender, EventArgs e)
{
time++;
label4.Text = time.ToString();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
stream.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
stream.Stop();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
uporabimotiondetection = 1;
}
else {
uporabimotiondetection = 0;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//textBox1.Text = string.Format("{0:0.00000}", textBox1.Text);
//float motionlevel = float.Parse(MotionDetected);
//float a = 1;
//textBox2.Text = a.ToString();
//float b = MotionDetected / 100;
//textBox1.Text = b.ToString();
//double limiter = Convert.ToDouble("0,02");
//textBox2.Text = limiter.ToString();
//if (MotionDetected < (0.02f / 1000))
//{
// label5.Text = "gibanje";
//}
//else
//{
// label5.Text = "ni gibanja!";
//}
}
}
}
Related
I need a bit of help with this code I am writing to produce and test the different types of loop conditions It has several lines that have errors that are causing the compiler to not build the solution. Don't know if it is the specific library that I am not calling right or if I am missing something in particular with the code. I created a particular folder that stores the correct images that I want the loop to use for the pictureBox, but it still has errors where it states the variable is out of text; it now states that System.OutOfMemoryException: 'Out of memory.'
:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PictureBox
{
public partial class Form1 : Form
{
int num1, num2;
Image[] Bully = new Image[10];
int randomNum;
Image dog= Image.FromFile(#"C:\Users\Empiric Library\source\repos\PictureBox\PictureBox\Form1.cs");
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
pictureBox1.Image = Bully[i];
i++;
Bully[0] = Image.FromFile("Bully1.jpeg");
Bully[1] = Image.FromFile("Bully2.jpeg");
Bully[2] = Image.FromFile("Bully3.jpeg");
Bully[3] = Image.FromFile("Bully4.jpeg");
Bully[4] = Image.FromFile("Bully5.jpeg");
Bully[5] = Image.FromFile("Bully6.jpeg");
if (int.TryParse(textBox1.Text, out num1))
{
if (int.TryParse(textBox2.Text, out num2))
{
while ( i < num2)
{
MessageBox.Show("i=" + i);
pictureBox1.Image = Bully[i];
i++;
}
}
}
}
private void Form2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Bully[0] = Image.FromFile("Bully1.jpeg");
Bully[1] = Image.FromFile("Bully2.jpeg");
Bully[2] = Image.FromFile("Bully3.jpeg");
Bully[3] = Image.FromFile("Bully4.jpeg");
Bully[4] = Image.FromFile("Bully5.jpeg");
Bully[5] = Image.FromFile("Bully6.jpeg");
if (int.TryParse(textBox2.Text, out num2))
{
for (int i = num1; i <= num2; i++)
{
MessageBox.Show("Bully Number= " + i);
pictureBox1.Image = Bully[i];
}
}
}
private double Average(int num1, int num2)
{
double result = (num1 + num2) / 2;
return result;
}
private void avg1_Click(object sender, EventArgs e)
{
double res = Average(1, 100);
textBox4.Text = res.ToString();
}
private void button4_Click(object sender, EventArgs e, string textLine)
{
StreamReader fileIn = new StreamReader(#"C:\Users\Empiric Library\source\repos\PictureBox\PictureBox\Form1.cs");
// This Reads The First Line Of Text
// Read The Next Line Of Text
textLine = fileIn.ReadLine();
MessageBox.Show("From File ..." + textLine);
fileIn.Close();
}
private void button5_Click(object sender, EventArgs e)
{
StreamWriter FileOut = new StreamWriter(#"C:\Users\Empiric Library\source\repos\PictureBox\PictureBox\Form1.cs");
string str = "BullyProof";
int number = 16;
FileOut.WriteLine(str);
FileOut.WriteLine(number);
// To Write In Output Stream
FileOut.Flush();
// To Close The Stream
FileOut.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Random random = new Random();
randomNum = random.Next(1, 42);
textBox3.Text = randomNum.ToString();
}
}
}
We just started using c# and the whole class knows nothing.
Yet, the teacher told us to use WinForms and c# to make a program that used bubblesort.
I looked around the web and only found validation for the text boxes (numbers only).
I'm new here but I would like to ask for help if possible.
We have to deliver this work today and we've got nothing so far.
This is the code I have.
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;
namespace maedofeixeira
{
public partial class Form1 : Form
{
int[] elementos = new int[5];
public Form1()
{
InitializeComponent();
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
}
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox2.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - 1);
}
}
private void TextBox3_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox3.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox3.Text = textBox3.Text.Remove(textBox3.Text.Length - 1);
}
}
private void TextBox4_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox4.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox4.Text = textBox4.Text.Remove(textBox4.Text.Length - 1);
}
}
private void TextBox5_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox5.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox5.Text = textBox5.Text.Remove(textBox5.Text.Length - 1);
}
}
private void Bubblesort()
{
int refos = 0;
for (int i=0;i<elementos.Length-1;i++)
{
for (int j = 0; j < elementos.Length - (i + 1); j++)
{
if (elementos[j] > elementos[j + 1])
{
refos = elementos[j];
elementos[j] = elementos[j + 1];
elementos[j + 1] = refos;
}
}
}
}
private void Button1_Click(object sender, EventArgs e)
{
Bubblesort();
for(int i=0;i<elementos.Length;i++)
{
lbOrdenada.Items.Add(elementos[i]);
}
}
}
}
Screenshot:
Problem so far: When we hit the "Ordenar" button, it shows 5 zeros.
You need to put the values from the text boxes into the elementos array. Because of the general messiness of the code, this can't be done elegantly, but something like this should do it:
private void Button1_Click(object sender, EventArgs e)
{
elementos[0] = Convert.ToInt32(TextBox1.Text);
elementos[1] = Convert.ToInt32(TextBox2.Text);
elementos[2] = Convert.ToInt32(TextBox3.Text);
elementos[3] = Convert.ToInt32(TextBox4.Text);
elementos[4] = Convert.ToInt32(TextBox5.Text);
Bubblesort();
for(int i=0;i<elementos.Length;i++)
{
lbOrdenada.Items.Add(elementos[i]);
}
}
Im trying to make a cookie maker!
it works great and all but every time i click on the cookie, it takes it about 2 clicks to add just one to the cookie count. I want to make the counting system go faster, since if i click 2 times a second, it will only count as one atm...
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 System.Threading;
namespace FormalCookie
{
public partial class Form1 : Form
{
int cnum = 0;
int add = 1;
public Form1()
{
InitializeComponent();
label1.Text = ("Price For Mouse:" + " 50");
textBox1.Text = ("Cookies:" + " " + cnum);
mouse.Enabled = false;
for (int i = 0; i< add; i++)
{
if (add > 1 && add % 2 == 0)
{
cnum += 1 * add;
}
Thread.Sleep(1000);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Mouse
if (cnum >= add * add * 50)
{
cnum -= (add * 100);
add+=2;
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (cnum >= (add * add * 50))
{
mouse.Enabled = true;
}
else
{
mouse.Enabled = false;
}
cnum += (add * add);
textBox1.Text = ("Cookies:" + " " + cnum);
label1.Text = ("Price For Mouse:" + " " + (add * add * 50));
}
private void label1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, MouseEventArgs e)
{
}
}
}
I am doing a simple clicker game, i know it is stupid to ask ,but i am still learning ,so my question is how to make the "cost2" integer rise his value +10 every button2 click.
Here is my 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;
namespace Diamond_Clicker
{
public partial class Form1 : Form
{
private int clicks = 0;
private int counter = 1;
const double factor = 0.95;
double interval = 1000;
int cost = 50;
int cost2 = 500;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void UpdateButton()
{
if (clicks >= cost)
button1.Enabled = true;
else button1.Enabled = false;
}
private void UpdateButton2()
{
if (clicks >= cost2)
button2.Enabled = true;
else button2.Enabled = false;
}
private void myDiamond_MouseUp(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamond.png");
}
private void myDiamond_MouseDown(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamondMouseUp.png");
clicks++;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
UpdateButton();
UpdateButton2();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
counter++;
clicks = clicks + 1;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
UpdateButton();
}
private void button1_Click(object sender, EventArgs e)
{
clicks = clicks - cost;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
timer1.Enabled = true;
UpdateButton();
button1.Enabled = false;
interval *= factor;
timer1.Interval = (int)interval;
cost++;
label2.Text = "Cost: " + cost.ToString() + "$";
}
private void button2_Click(object sender, EventArgs e)
{
clicks = clicks - cost2;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
timer2.Enabled = true;
UpdateButton2();
button2.Enabled = false;
interval *= factor;
timer2.Interval = (int)interval;
cost2++;
label4.Text = "Cost: " + cost2.ToString() + "$";
}
private void timer2_Tick(object sender, EventArgs e)
{
counter++;
clicks = clicks + 10;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
UpdateButton2();
}
}
}
Add the following line in your button2_Click method
cost2 += 10;
You will simply change cost2++; to cost2 += 10;
I have a program that gets the incoming number, date and time. I want to check however if the person who is ringing me has put the phone down, how can I do this?
Below is the code which I currently have:
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.Ports;
namespace CallerID
{
public partial class CallerID : Form
{
int timesTicked = 0;
Point defaultLocation = new Point();
Point newLocation = new Point();
public CallerID()
{
InitializeComponent();
port.Open();
SetModem(); // SetModem(); originally went after WatchModem();
WatchModem();
//SetModem();
telephoneTimer.Interval = 16;
telephoneTimer.Tick += new EventHandler(telephoneTimer_Tick);
defaultLocation = pictureBox1.Location;
newLocation = pictureBox1.Location;
}
void telephoneTimer_Tick(object sender, EventArgs e)
{
if (timesTicked <= 2)
newLocation.X++;
if (timesTicked >= 4)
newLocation.X--;
if (timesTicked == 6)
{
timesTicked = 0;
pictureBox1.Location = defaultLocation;
newLocation = defaultLocation;
}
pictureBox1.Location = newLocation;
timesTicked++;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
WatchModem();
}
private SerialPort port = new SerialPort("COM3");
string CallName;
string CallNumber;
string ReadData;
private void SetModem()
{
port.WriteLine("AT+VCID=1\n");
//port.WriteLine("AT+VCID=1");
port.RtsEnable = true;
}
private void WatchModem()
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
public delegate void SetCallerIdText();
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
ReadData = port.ReadExisting();
//Add code to split up/decode the incoming data
//if (lblCallerIDTitle.InvokeRequired)
if (ReadData.Contains("NMBR"))
{
lblData.Invoke(new SetCallerIdText(() => lblData.Text = ReadData));
}
//else
// lblCallerIDTitle.Text = ReadData;
}
private void lblData_TextChanged(object sender, EventArgs e)
{
telephoneTimer.Start();
button1.Visible = true;
}
}
}
Please ignore the Timer Code as that is just for animation.
Have you tried the PinChanged event? Normally Carrier Detect will go low when the remote end disconnects.