Having decided to try AForge for video and imaging stuff, I tried to implement this simple demo:
private void Main_Load(object sender, EventArgs e)
{
// enumerate video devices
FilterInfoCollection videoDevices = new FilterInfoCollection(
FilterCategory.VideoInputDevice);
// create video source
VideoCaptureDevice videoSource = new VideoCaptureDevice(
videoDevices[0].MonikerString);
// set NewFrame event handler
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
// start the video source
videoSource.Start();
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
this.pictureBox1.Image = eventArgs.Frame;
}
The problem is that I always get an ArgumentException, though doesn't always happen right away. It pops up on Application.Run(new Main());, but the top of the stacktrace looks like this:
at System.Drawing.Image.get_Width() at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
Not sure if this is relevant, but the ParamName attribute of the exception is null. I've tried wrapping the image assignment in a try...catch block, but this didn't help. I've also checked to make sure that the image is not null before assignment. I've also checked for non-null, but 0x0 sized images.
What have I done wrong? Can anyone suggest a workaround?
I think the problem is that you do not make a copy
of the passed bitmap (frame) in your event handler.
The AForge documentation says:
Since video source may have multiple clients, each client is responsible
for making a copy (cloning) of the passed video frame, because the video source
disposes its own original copy after notifying of clients.
So, if you directly assign the frame to the picture box
the bitmap could be disposed by the AForge framework while the PictureBox
is trying to draw the bitmap.
Related
I have an existing C# winforms app with live videostreams from 4 ip cameras via VLC.net (rtsp) and we take every 100ms a snapshot to analize. This is working fine. Now we like to do this instead of VLC.net with FFmpeg to have more possibilities with the (faster) captured frame. I have already a working ffmpeg wrapper sending every frame as bitmap via an event. Then I have a class "FmpegVideoWindow" inherits from PictureBox, here we listen for incoming frames and draw them on the picturebox. So instead of using the vlc-control in the form we use now a picturebox :
private void ffMpegWrapper_NewFrame1(object sender, NewFrameEventArgs e)
{
if (e != null)
{
try
{
using (Bitmap bm = new Bitmap(e.Frame))
{
frame = (Bitmap)bm.Clone();
}
}
catch
{
}
}
Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (frame != null)
{
pe.Graphics.DrawImage(frame, 0, 0, this.Width, this.Height);
//todo : disposing frame
}
}
This is working ok to see the cameras live, they have #25fps and a D1 resolution 704*576.
The problem is that every time we have other processing like opening a (settings)form, or analyzing the snapshot, the picture in the picturebox is freezing for a short time. The same program with VLC.net was never a problem.
The Ffmpeg process is done in another thread. Why does the VLC.net control running smooth in the GUI thread, and this new approach not? And what can we do to fix it? The analyzing is also done in another thread, writing the results from the analyzer to the database is done in the GUI thread. And I don't like to change that because with VLC.net it was also not needed to do that.
Thank you in advance.
I am making use of the AForge class library.
From this library I am using VideoSourcePlayer to take photos with the webcam.
My purpose is to create a function that allows the user to photograph images to establish them as company logo.
Not only can you choose images from the computer, but you can also capture images from the outside through the camera, since you may only want to transfer a logo of a physical support (paper) to the program.
As commented earlier in SO, (how to pause a video file played using videosourceplayer), VideoSourcePlayer does not have a Pause method or any function that allows to freeze the image.
Yes, it is true that it has the GetCurrentFrame() method, but that only gets a Bitmap from the current frame that must be passed to a PictureBox.
But I want that when the user clicks the button Capture the image of the VideoSourcePlayer simulate being frozen, and when the user presses the Delete button because he did not like the photo, then the image stops being frozen and recovers its movement.
Logic is like pausing or playing a video.
Well, there's no method for it, so I decided to look for another way to get this, and ...
If a picture is taken, use a PictureBox that contains the last frame and that is displayed on the VideoSourcePlayer, but if it is deleted, then the PictureBox is removed and the VideoSourcePlayer is returned with video.
private readonly Bitmap EmptyBitmap;
private void CaptureBtn_Click(object sender, EventArgs e)
{
Bitmap bitmap = this.VideoSource.GetCurrentVideoFrame();
ShowTakedFrame(bitmap, false);
}
private void ShowTakedFrame(Bitmap Frame, bool remove)
{
var picture = new System.Windows.Forms.PictureBox();
picture.Size = this.VideoSource.Size;
picture.Location = this.VideoSource.Location;
if (!remove)
{
this.VideoSource.Stop();
picture.Image = Frame;
this.Controls.Remove(VideoSource);
this.Controls.Add(picture);
}
else
{
this.Controls.Remove(picture);
this.Controls.Add(VideoSource);
this.VideoSource.VideoSource = this.CaptureDevice;
this.VideoSource.Start();
}
}
private void DeleteBtn_Click(object sender, EventArgs e)
{
ShowTakedFrame(EmptyBitmap, true);
}
My problem is that when capturing the photo, the image is a few seconds after the moment when you press the Capture button and when you delete the captured image, using the Delete button, the video of the VideoSourcePlayer is frozen.
Can someone help me with this?
The problem is that when you remove the PictureBox and add the VideoSourcePlayer, it creates a new object, that is, one that does not have the configuration properties of the previous one. My recommendation is that you create the capture in a different form.
I am using a webcam to show live stream, capture images, record video etc using AForge library.
Each time I get a new frame, I assign it to a picture Box to show the live stream and i have added a tool strip for recording video when it is checked. so i did it in the same following method.
private void video_NewFrame( object sender, NewFrameEventArgs eventArgs)
{
// Assigning new frame to a picturebox
pictureBox1.Image = newFrame;
if(recordVideo.Checked)
{
writer.AddFrame(newFrame);
}
}
when the record Video tool strip is checked. I open the avi file.
writer.Open("test.avi", pictureBox1.Width, pictureBox1.Height);
and when it is unchecked i close the file.
writer.Close();
but when i start recording the video i get the following error:
Invalid Operation Exception - object is currently in use elsewhere.
Just use it like this
pictureBox1.Image = newFrame.Clone() as Bitmap;
I read that you can increase the drawing speed/time by drawing with the paint event, and with that you can also draw unscaled.
So i would very much like to try it on my panel.
The problem is though, the image is recieved in another Thread then the GUI, and i don´t know how to give it to the paint event.
I really don´t want to invoke and stuff (as that is incredibly slow, at least when i have used it).
The code will look, something like this.
protected override void panel1_Paint(object sender, PaintEventArgs e, Image u)
{
e.Graphics.DrawImageUnscaled(u, Point.Empty);
}
Though there i tried using override to add to add an image in the field, ald i wanted to make it static, so i could call it from a Thread. Sadly i t didn´t work.
But well, i tried.
private void panel1_Paint(object sender, PaintEventArgs e)
{
// e.Graphics.DrawImageUnscaled(u, Point.Empty);
}
There is the "working" one, except i can´t get the image to it.
I tried making an image variable, then save the image in that variable, and paint it.
but paint does never see the image in it, it can´t access the Image, guess cause it´s written from another thread.
//initialize
private Image Im;
////////
my Thread
Im = Image.FromStream(....);
////////////7
void panel1_Paint(object sender, PaintEventArgs e)
{
if(Im !=null)
e.Graphics.DrawImageUnscaled(Im, Point.Empty);
}
This is how i tried, and failed.
I've some huge images (7000*5000) to load simultaneously in my program, which I'm displaying in picturebox one by one. These images take some time to load in the PictureBox. At first I'm loading all the images in an Image array as Bitmap, then I'm just showing the first image in picturebox picturebox.Image = imageArray[0]. So I want to show wait cursor until first image is shown in Picturebox. Is there any way to know when the first image is shown on Picturebox?
You can use the PictureBox events : LoadProgressChanged to show the loading progress and LoadCompleted to do something when it is finished.
private void pictureBox1_LoadProgressChanged(object sender, ProgressChangedEventArgs e)
{
// animate a progressbar...
}
private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
// done !
}
To make this work, you have to keep the .WaitOnLoad value property to False, and you have to use one of the LoadAsync method.