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.
Related
I need to show an animated gif, which I retrieve as a BLOB from a database (rather than a file), on a PictureBox, but I can't block the UI. The user can press a button that stops the referred animation.
So I create a UserControl that contains the PictureBox, where I can set the image to be shown. When I put this control on the form and starts the animation, everything occurs as expected, except for the fact that I can't press the button, 'cause the form is blocked.
When I start the animation from a Thread, the button are enabled and clickable, but in this case the animation "flickers".
I can easily change the PictureBox to another component, since it can show animated gifs. I can also transform the animated gif to some other format, if needed...
Note that
My images are animated gifs that came from BLOB's on a database and not normal files
I do not want (in fact, It's forbidden, by requirements) create a file with the images (even a temporary file)
Any thoughts?
My images are retrieved from a SQLite database, where they are stored as BLOBs. My requirements forbid the creation of files, even temporary ones. So I did it working exclusively on memory.
This code gets the image, decomposes it on frames and sets the image of the PictureBox to be each frame at a time:
byte[] img = limg.getImage();
if (img != null)
{
MemoryStream ms = new MemoryStream(img);
newImg = System.Drawing.Image.FromStream(ms);
GifImage gi = new GifImage(newImg);
for (int i = 0; i < gi.GetFrameCount; i++)
{
this.SetPicture(gi.GetNextFrame(), limg.getWord());
}
}
And this is the code that sets the image to the PictureBox (note the Sleep, that allows adjust the speed of the animation):
private void SetPicture(System.Drawing.Image img, string label)
{
if (pbOutput.InvokeRequired)
{
pbOutput.Invoke(new MethodInvoker(
delegate()
{
pbOutput.Image = img;
pbOutput.Refresh();
Thread.Sleep(Interval);
}));
}
else
{
pbOutput.Image = img;
pbOutput.Invalidate();
Thread.Sleep(Interval);
}
}
The problem, as said on the question, is the flickering of the image caused by this code.
The solution: substitutes the call of SetPicture by the code of the function. That way, I don't call a function, and put the code of it "inline" the main loop.
The flickering does not occurs anymore.
I would like to test around a bit with augmented reality.How to get the picture that comes from the camera as background for my view? For what I want to do, I don't need to access the picture, I just need it as background.
You can get the preview-buffer from the camera from the page. We need this to capture a QR-Code without taking pictures with the camera.
Initialize the camera object
_phoneCamera = new PhotoCamera();
_phoneCamera.Initialized += CamInitialized;
In the Initialized - Event. Just create the buffer
private void CamInitialized(object sender, CameraOperationCompletedEventArgs e)
{
_previewBuffer = new WriteableBitmap((int)_phoneCamera.PreviewResolution.Width, (int) _phoneCamera.PreviewResolution.Height);
}
Ant then you can take everytime a snapshot of the current view:
//grab a camera snapshot
_phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels);
_previewBuffer.Invalidate();
And then you can do what you want with this WriteableBitmap (show as background, or whatever).
Or (if I misunderstood your question) here a link with the info, how to add the camera-view to your page (this is also needed for the above solution): How to create a base camera app for Windows Phone 8
I was trying to figure out how to make sure that a GIF image what I put to my WindowsForm project through picturebox going to loop only once and still without finding an answer even after going through as many sites with this problem as I could find.
I know that with using of picturebox programmer lossing control of looping (in case that you set looping only once when creating the gif animation in editor) and I somehow have to get a code which will basically say when gif animation gets to some certain frame...in my case to last one which is number 7 and gets there in 430 milliseconds (190,40,40,40,40,40,40) the gif animation going to be Disposed.
So the whole idea was to make a simple game with balloons which will have an animation with flying and animation with exploding after pressing of the mouse on the ballon_grey picture box.
It would look like that:
private void timer1_Tick(object sender, EventArgs e)
{
//makes balloon going up
balloon_grey.Location = new Point(balloon_grey.Location.X, balloon_grey.Location.Y - 3);
}
private void balloon_grey_Click(object sender, EventArgs e)
{
//forces balloon_grey picturebox to change animation to balloon_grey_explosion.gif
balloon_grey.Image = Image.FromFile("balloon_grey_explosion.gif");
//gets the amount of frames from the gif animation...don't know if this is needed to reach the goal
int frames = Properties.Resources.balloon_grey_explosion.GetFrameCount(FrameDimension.Time);
}
Thank you for your time and any advice about the problem.
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.
So I have a custom Button class in a Winforms app written in C# 4.0. The button holds a static image normally, but when a refresh operation takes place it switches to an animated AJAX-style button. In order to animate the image I have a timer set up that advances the image animation on every tick and sets that as the button image. That's the only way I can get it to work. The solution I have isn't that efficient; any advice would be helpful.
So two questions:
1. Is there an easier way - as in am I overlooking some feature I should be using?
2. Is there a better way to manually animate the image?
Find code below of what I'm doing on each tick. The first area of improvement: maybe copy each frame of the image to a list? Note the _image.Dispose(); failing to dispose of a local Image was causing memory leaks.
Thanks for any advice here. I will post something online and link it once I have an efficient solution.
private void TickImage()
{
if (!_stop)
{
_ticks++;
this.SuspendLayout();
Image = null;
if(_image != null)
_image.Dispose();
//Get the animated image from resources and the info
//required to grab a frame
_image = Resources.Progress16;
var dimension = new FrameDimension(_image.FrameDimensionsList[0]);
int frameCount = _image.GetFrameCount(dimension);
//Reset to zero if we're at the end of the image frames
if (_activeFrame >= frameCount)
{
_activeFrame = 0;
}
//Select the frame of the animated image we want to show
_image.SelectActiveFrame(dimension, _activeFrame);
//Assign our frame to the Image property of the button
Image = _image;
this.ResumeLayout();
_activeFrame++;
_ticks--;
}
}
I guess Winforms is not so reach in animation capabilities on its own. If you want more advanced animation use may consider some third-party solutions.
I think you should not load image from resource every tick. The better way is to preload image frames one and hold the reference. Then use it to set appropriate frame every tick.
As I've recently tested, animated gifs are animating well without any additional coding, at least on standard buttons. But if you still need to aminate frames manually, you may try something like this:
// put this somewhere in initialization
private void Init()
{
Image image = Resources.Progress16;
_dimension = new FrameDimension(image.FrameDimensionsList[0]);
_frameCount = image.GetFrameCount(_dimension);
image.SelectActiveFrame(_dimension, _activeFrame);
Image = image;
}
private void TickImage()
{
if (_stop) return;
// get next frame index
if (++_activeFrame >= _frameCount)
{
_activeFrame = 0;
}
// switch image frame
Image.SelectActiveFrame(_dimension, _activeFrame);
// force refresh (NOTE: check if it's really needed)
Invalidate();
}
Another option is to use ImageList property with preloaded static frames and cycle ImageIndex propery just as you do it with SelectActiveFrame above.