Why is my webcam only taking one frame upon execution via Emgu? - c#

I have some code that takes opens my webcam via Emgu upon loading Form1. The problem I have, though, is that I can only load the first frame.
I have tried to use Webcam() to create a video capture, but it only takes the first frame. If I assign this code to a button, it will take only 1 frame upon each button press. I have tried to create a continuation of the video capture using Webcam2(), but the the picturebox had 1 FPS and would exponentially use more memory overtime.
I know it is not my computer as I was able to use webcam via emgu using its facial recognition viewer (I used the guide at https://www.youtube.com/watch?v=ZSxHyp4OZx0 to test this out). Unfortunately, while I am creating a a similar project, I am using AWS for the facial recognition. Still, I was able to run the Emgu camera viewer without any issues or bad framerates. My Laptop is a 2022 high end gaming laptop.
Here is the code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
showit();
//Application.Idle += Webcam2;
//Webcam();
}
private void Webcam2(object sender, EventArgs e)
{
VideoCapture capture = new VideoCapture();
pictureBox1.Image = capture.QueryFrame().ToBitmap();
}
private void Webcam()
{
VideoCapture capture = new VideoCapture();
pictureBox1.Image = capture.QueryFrame().ToBitmap();
}
}
Any idea what is going on?

Related

C#, Drawing on button click, not before

I'm fairly new to using C#. I started on monday to be honest and prior to this only had basic MATLAB level of experience, so I'm still some kind of newb scrub. The question I will ask has already been asked, according to my googling skills, but I have yet to find a working solution.
I'm currently writing a program to make the image acquisition from a Vimba camera, and the subsequent image tratment, and that program includes a UI, so a form1. On this form, there is a pictureBoxLiveCamera that shows the live recording from the camera.
What I am trying to do is :
draw two rectangles on top of the pictureBoxLiveCamera, indicating specific zones of the image (THIS WORKS LIKE A CHARM)
have those two rectangles NOT drawn when I launch the program, but when I click a specific drawRectanglesButton (AND HERE I HAVE A PROBLEM)
have that same button hide the rectangles if they are drawn, and then draw them if they are hidden (I'M NOT THERE YET)
I have already explored a lot of similar threads, and this one particularly shaped the way I wrote the code, though it is not working as intended yet.
Here are the protions of my code that are relevant here :
form loading :
// Form loading
private void Form1_Load(object sender, EventArgs e)
{
pictureBoxLiveCamera.Paint += pictureBoxLiveCamera_Paint;
... //Vimba API startup and so on
}
button code :
private void drawRectanglesButton_Click(object sender, EventArgs e)
{
this.Invalidate(); // force Redraw the form
}
rectangles drawing code :
public void pictureBoxLiveCamera_Paint(object sender, PaintEventArgs e)
{
Rectangle ee = new Rectangle(-5, 120, 790, 100);
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, ee);
}
Rectangle eee = new Rectangle(-5, 364, 790, 100);
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, eee);
}
}
My problem is the following : as it is, when I start the program, the rectangles are already drawn, so the button does nothing.
I'm pretty sure the mistake will be obvious to most of you, but this has been driving me crazy all day.
Thanks a lot !
Trion

How to show an array of images, without freezing the whole programm for seconds

I have the following problem:
I want to make a little 2d-game in c#.
To display a background that consists of several images. To make animated gifs possible, i used MediaElement objects.
I added these MediaElements to my Canvas.
But when i run my programm, the performance is a disaster. It takes almost half a minute to display the images.
I obviously need a better idea to display an array of images, without slowing down everything.
This is my Starting-Point:
class BackgroundMediaElement : MediaElement
{
public BackgroundMediaElement(string imageSourcePath, int rowInCanvas, int columnInCanvas, int lengthOfSquare)
{
this.UnloadedBehavior = MediaState.Manual;
this.Source = new Uri(imageSourcePath, UriKind.Relative);
Canvas.SetTop(this, rowInCanvas*lengthOfSquare);
Canvas.SetLeft(this, columnInCanvas*lengthOfSquare);
this.Play();
this.MediaEnded += method_MediaEnded;
}
private void method_MediaEnded(object sender, RoutedEventArgs e)
{
//MediaElement m = (MediaElement)sender;
this.Position = TimeSpan.FromMilliseconds(1);
}
}
I created more than 200 elements and added them to my Canvas.
But it even slows down with 40 elements.
One image does not even have one kByte.
This is simply not the technology you are looking for. You can use C# very well to create games, but you should be looking for an appropriate Game Engine. Unity supports 2D games and allows you to code in C#. Click here for more information.
Focus on creating games rather than fighting with irrelevant stuff :)

How can i switch between two icons to make them flash smooth?

In top of form1 constructor i did:
filePaths = Directory.GetFiles(#"C:\Users\bout0_000\Downloads\", "imagenew*.ico");
timer3.Start();
Then:
private void timer3_Tick(object sender, EventArgs e)
{
for (int i = 0; i < filePaths.Length; i++)
{
if (!filePaths[i].Equals(currentIcon))
{
this.Icon = new Icon(filePaths[i]);
currentIcon = filePaths[i];
break;
}
}
}
In the designer of form1 i added the Icon of this icon and when I'm running the program the icons switch and flashing fast and good.
But in the taskbar the icon is not switching fast and smooth enough it seems for some seconds it's flashing good and then some seconds it's like hanging for milisecond or some it's not switching fast and smooth like in the form1 Icon.
Well, the System.Timers.Timer operates on an independent thread, so your main thread and whatever work it's doing wouldn't be blocking the +Elapsed event.
Maybe it's an issue with constantly, over and over and over, reading the file from the drive to toggle the icon graphic? My suggestion would be to load your targeted images into a resource file for your project, and draw the swapping of images from that, instead of the physical drive.
If those files are dynamic, maybe cache them up on program initialization.
Is the image loaded from url in winforms picturebox stored in cache?

Webcam frame from Aforge always gray image

I am using Aforge .NET framework to get a webcam in my app but I always get an all gray image frame.
this.videoSource = new VideoCaptureDevice(this.Moniker);
this.videoSource.DesiredFrameSize = GetHighestResolution(this.videoSource.VideoCapabilities);
this.videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
this.videoSource.Start();
The vent handler:
private void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
this.outputBox.Image = (Bitmap)eventArgs.Frame.Clone();
}
This should do the trick. DesiredFrameSize is set to the highest value the cam can support.
Any hints why the image is always gray? Even when writing it do disk...
Edit: To add: The same problem occurs with the sample application SimplePlayer from Aforge samples. My webcam is a Logitech QuickCam Pro 9000.
Edit2: The same goes for small DesiredFramesizes.
maybe the camera isnt wrapped around directshow (some sort of directx stuff).
some camera's come with their own driver, or different inner working.
I have some highspeed industrial camera's, with simmilar issues.
I had to retrieve the raw images from them using diffrent methods, then do aforge stuff on those images.
Maybe check your code using older cheap USB2 camera's

.NET code to record a specific form activity + audio to avi

I am looking for some C# code .. or .NET component to record the activity of a form on my screen (not the whole desktop) along with audio, similar to what programs like Camtasia allow to do.
The video part is actually pretty easy. All you need to have is a timer running 20 times a second that will save form's canvas to a image files as frames. Then create an animation out of these pictures.
To capture image:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tVideo.Start();
}
int i = 0;
private void tVideo_Tick(object sender, EventArgs e)
{
String lFile = String.Format("c:\\{0}.bmp", i);
SaveAsBitmap(this, lFile);
i++;
}
public void SaveAsBitmap(Control aCtrl, string aFileName)
{
if (File.Exists(aFileName))
File.Delete(aFileName);
Graphics lGraphics = aCtrl.CreateGraphics();
Bitmap lImage = new Bitmap(aCtrl.Width, aCtrl.Height);
aCtrl.DrawToBitmap(lImage, new Rectangle(0, 0, aCtrl.Width, aCtrl.Height));
lImage.Save(aFileName);
lImage.Dispose();
}
}
This is just a light sample, of course you would have to add some compression and try to avoid saving the same image twice. Knowing how many images are the same + knowing about the framerate, you know how long to display the same frame.
To add a cursor you would have to keep some variables with mouse x,y and an event on mouse click. And then just add it to pictures.
Of course, this won't work for 3d games inspite of overlay which is drawn after the win32 paints.
For that you would have to go with DirectX/OpenGl/XNA. I think the idea is the same.
For audio, DirectX also.
My complete source:
http://deathsquad.pl/archiwum/Inne/so/answer-1934452.rar
Some DirectX audio samples:
http://www.codeproject.com/KB/directx/audiosav.aspx
Check out Gallio: It is a great testing framework, and it has built in screen recording API.
This post shows a few examples:
http://blog.bits-in-motion.com/2009/09/announcing-gallio-and-mbunit-v31.html

Categories

Resources