Was following a tutorial on YouTube and tried to get my program to display the live video feed from my webcam into the forms picture box but for some reason the webcam comes on but the picture box isn't displaying anything
I have tried checking if the Mat class was returning null.
but it isn't this 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;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
namespace FaceRecognitionAttandanceSystem
{
public partial class StudentAdd : Form
{
public string fileName { get; set; }
VideoCapture capture;
public StudentAdd()
{
InitializeComponent();
}
//Open Folder
private void metroButton3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false };
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//open file explorer
fileName = openFileDialog.FileName;
//pick image from file
Image img = Image.FromFile(fileName);
//Rotates Image by 90degrees
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox2.Image = img;
}
}
}
//Capture Image
private void metroButton4_Click(object sender, EventArgs e)
{
//If capture is empty start new video capture
if(capture == null)
{
capture = new Emgu.CV.VideoCapture(0);
}
capture.ImageGrabbed += Capture_ImageGrabbed;
capture.Start();
}
private void Capture_ImageGrabbed(object sender, EventArgs e)
{
try
{
Mat mat = new Mat();
if(mat == null)
{
Console.WriteLine("Here you Go");
}
capture.Retrieve(mat);
pictureBox1.Image = mat.ToImage<Bgr, Byte>().ToBitmap<Bgr, Byte>();
}
catch(Exception)
{
}
}
private void metroButton5_Click(object sender, EventArgs e)
{
}
}
}
The problem is very likely that the Capture_ImageGrabbed event is raised on a background thread. And you can only update the UI on the UI thread. You can place a break point in the event-handler and check the thread-debug window to confirm. To fix this you need to move execution to the UI thread. Try:
capture.Retrieve(mat);
var bitmap = mat.ToImage<Bgr, Byte>().ToBitmap<Bgr, Byte>();
pictureBox1.Invoke(() => pictureBox1.Image = bitmap );
You might need to write Invoke((Action)(() => pictureBox1.Image = bitmap) ), since I think there is some issues with the overload resolution of lambdas with the invoke-method.
Related
I am just learning C# and I was trying to implement a webcam picture capture program. I'm using the Aforge library, the thing is that my picturebox is not displaying the webcam image and I don't understand why. If anyone knows my error, please let me know. Thank you in advance.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DarrenLee.Media;
namespace test4
{
public partial class MainForm : Form
{
int count = 0;
Camera myCamera = new Camera();
public MainForm()
{
InitializeComponent();
GetInfo();
myCamera.OnFrameArrived += myCamera_OnFrameArrived;
}
private void GetInfo()
{
var cameraDevices = myCamera.GetCameraSources();
var cameraResolutions = myCamera.GetSupportedResolutions();
foreach (var d in cameraDevices)
cmbCameraDevices.Items.Add(d);
foreach (var r in cameraResolutions)
cmbCameraResolutions.Items.Add(r);
cmbCameraDevices.SelectedIndex = 0;
cmbCameraDevices.SelectedIndex = 0;
}
private void myCamera_OnFrameArrived(object source, FrameArrivedEventArgs e)
{
Image img = e.GetFrame();
picCamera.Image = img;
}
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
myCamera.ChangeCamera(cmbCameraDevices.SelectedIndex);
}
void ComboBox2SelectedIndexChanged(object sender, EventArgs e)
{
myCamera.Start(cmbCameraDevices.SelectedIndex);
}
void Form1FormClosing(object sender, FormClosingEventArgs e)
{
myCamera.Stop();
}
void BTTsaveClick(object sender, EventArgs e)
{
string filename = Application.StartupPath + #"\" + "Image" + count.ToString();
myCamera.Capture(filename);
count++;
}
}
}
Picture of how it looks when I compile it:
https://i.gyazo.com/6956a07405cd4bf5e74c20bc321bd32e.png
I am connecting the picture box with the content in this line:
Image img = e.GetFrame();
picCamera.Image = img;
Make sure you click on the object before writing the code.
Example: Click on the button in the design form then paste the code for BTTsaveClick in the newly created method after clicking on the button.
If you are using Visual Studio, you can check if the methods are referenced. If it's not referenced, that means the code won't be read.
I created a c# program for convert image to binary,binary_inverse and such .
the porgram somehow only works for the image i used first but crashed whenever i tried other image .
I appreciate if someone could help me find the problem .
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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.CV.CvEnum;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
resetui();
}
private Image<Bgr, Byte> ori;
private Image<Gray, Byte> edited;
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pB.ImageLocation = openFileDialog1.FileName ;
ori = new Image <Bgr,Byte> (openFileDialog1.FileName) ;
edited = new Image<Gray, Byte>(ori.Width, ori.Height) ;
}
}
private void button2_Click(object sender, EventArgs e)
{
CvInvoke.cvCvtColor(ori, edited, COLOR_CONVERSION.CV_BGR2GRAY);
pB.Image = edited.ToBitmap();
groupBox1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
pB.Image = ori.ToBitmap();
groupBox1.Enabled = false;
button1.Enabled = false;
button2.Enabled = true;
}
private void resetui()
{
button1.Enabled = true;
button2.Enabled = true;
groupBox1.Enabled = false;
}
private void CalcThresh()
{
if (cB.SelectedIndex == 0)
{
pB.Image = edited.ToBitmap();
return;
}
Image<Gray, byte> temp = new Image<Gray, Byte>(edited.Height, edited.Width);
double threshold = tB.Value;
double maxval = (double) max.Value;
THRESH mode = THRESH.CV_THRESH_BINARY;
switch (cB.SelectedIndex)
{
case 1:
mode = THRESH.CV_THRESH_BINARY ;
break ;
case 2:
mode = THRESH.CV_THRESH_BINARY_INV ; break ;
case 3:
mode = THRESH.CV_THRESH_TOZERO; break ;
case 4 :
mode = THRESH.CV_THRESH_TOZERO_INV; break ;
case 5:
mode = THRESH.CV_THRESH_TRUNC; break ;
}
CvInvoke.cvThreshold(edited, temp, threshold, maxval, mode );
pB.Image = temp.ToBitmap();
}
private void cB_SelectedIndexChanged(object sender, EventArgs e)
{
CalcThresh();
}
private void max_ValueChanged(object sender, EventArgs e)
{
CalcThresh();
}
private void tB_Scroll(object sender, EventArgs e)
{
CalcThresh();
}
}
}
Heres the first image i used
It works fine like this for example :
But for other image it crash whenever i picked a mode :
This one of the pic that made my program crashed :
This how the form deisgn looks like :
Im studying this for test so if someone can help me find the problem i really
appreciate it.
Thx Before
Replace this:
Image<Gray, byte> temp = new Image<Gray, Byte>(edited.Height, edited.Width);
With this:
Image<Gray, byte> temp = new Image<Gray, byte>(edited.Width, edited.Height);
I'm using Web browser in my form which navigates to multiple pages. I want to take screenshot of Last page once loaded completely. I've coded taking screen shot inside the webBrowser1_DocumentCompleted. However it is taking screenshot before page getting loaded. I'm confused where I can take screenshot. I found LoadCompleted event in google but not sure how to use. Please help me out.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace CC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
string url = "http://www.lll.com";
webBrowser1.Navigate(new Uri(url));
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
catch (Exception ex)
{
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string AbsoluteURL = e.Url.AbsolutePath.ToString();
int count = 0;
switch (AbsoluteURL)
{
case "Page1":
var loginControl = webBrowser1.Document.GetElementById("user");
var passwordControl = webBrowser1.Document.GetElementById("password");
var btn = webBrowser1.Document.GetElementById("Submit");
if (loginControl != null)
loginControl.SetAttribute("value", "XXX");
if (passwordControl != null)
passwordControl.SetAttribute("value", "YYY");
var elems = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("tabindex") == "3")
{
elem.InvokeMember("click");
}
}
break;
case"Page2": // THIS IS NOT WORKING. TAKING SCREEN SHOT BEFORE PAGE GETTING LOADED FULLY. NEED HELP HERE
using (Bitmap bitmap = new Bitmap(webBrowser1.Width, webBrowser1.Height, PixelFormat.Format24bppRgb))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(
PointToScreen(webBrowser1.Location),
new Point(0, 0),
webBrowser1.Size);
bitmap.Save(#"C:\123.bmp");
}
break;
default:
Console.WriteLine("Better try again");
break;
}
}
}
}
}
Try this one.
protected void Capture(object sender, EventArgs e)
{
string url = "www.google.com";
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
//do what do you want
}
In my experience, navigation and document loading are distinct from having the document be actually rendered. I would look at ReadyState https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowserreadystate%28v=vs.110%29.aspx to determine of the document is actually rendered and not just loaded.
I wrote a code for acessing the webcam with two buttons and a picture box
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 AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
namespace cam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection webcam;
private VideoCaptureDevice cam;
Bitmap bitmap;
private void Form1_Load(object sender, EventArgs e)
{
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in webcam)
{
comboBox1.Items.Add(VideoCaptureDevice.Name);
}
comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
cam = new VideoCaptureDevice(webcam[comboBox1.SelectedIndex].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
cam.Start();
}
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bitmap;
}
private void button3_Click(object sender, EventArgs e)
{
if (cam.IsRunning)
{
cam.Stop();
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image = bitmap;
}
}
}
The code build up successfully. but on debugging the picture box is not working. start and stop is working properly. Can anyone help?
you have to delete first
Bitmap bitmap;
because its null and that what you show in picture box
not the picture that come from camera
a long time ago i had problems too with aforge but got it working
see my code inhere : How initialize AForge webcam
Trying to put icons in ObjectListview, here's my piece of code where icon should have been put:
objectListView1.SmallImageList = imageList1;
deleteColumn.IsEditable = true;
deleteColumn.ImageGetter = delegate
{
return 0;
};
deleteColumn.AspectGetter = delegate
{
return "Delete";
};
imageList1 already have an image, this code should have put an icon next to "Delete", but it did not appear at all, looked through cookbooks and Google and I still have no idea. Can anyone help me?
this is the full form code in case needed:
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.Security.Cryptography;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
objectListView1.AllowDrop = true;
objectListView1.DragEnter += new DragEventHandler(objectListView1_DragEnter);
objectListView1.DragDrop += new DragEventHandler(objectListView1_DragDrop);
objectListView1.CellEditActivation = BrightIdeasSoftware.ObjectListView.CellEditActivateMode.SingleClick;
objectListView1.CellEditStarting += deleteItems;
objectListView1.SmallImageList = imageList1;
deleteColumn.IsEditable = true;
deleteColumn.ImageGetter = delegate
{
return 0;
};
deleteColumn.AspectGetter = delegate
{
return "Delete";
};
}
private void objectListView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void objectListView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
{
string[] droppedFiles = (string[]) e.Data.GetData(DataFormats.FileDrop);
foreach (string path in droppedFiles)
{
if (File.Exists(path))
{
FileObject fo = new FileObject(path, "added later");
objectListView1.AddObject(fo);
}
}
}
}
private void deleteItems(object sender, BrightIdeasSoftware.CellEditEventArgs e)
{
if(e.Column == deleteColumn)
{
e.Cancel = true;
objectListView1.RemoveObject(e.RowObject);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
In order for images to appear next to the text in a column, you must:
Connect the ObjectListView to an ImageList (using the SmallImageList property);
Install an ImageGetter delegate for the column that must show the images;
Make sure that there are actually images in the ImageList.
With this done, images will appear (I just tested this).
There is one catch, though. From your question, I suspect that the "Delete" column may not be the first column in the ObjectListView. The above steps only allow you to show an image in the very first column. For subsequent columns, you will have to set the ShowImagesOnSubItems property to True. Could that be it?