How do I know when an Image is loaded in Picturebox - c#

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.

Related

Failed to take photo with the webcam using the library AForge

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.

Gif image looping only once

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.

Paint Draw Image from another Thread?

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.

C# How to display a wait gif over the screen for 5 seconds when i click on the screen

hi
How to display a wait gif over the screen for 5 second
i want to show a loading image(Gif) over a picturebox. eg when i click on the picturebox , before load another image into this picturebox my loading image(animation image(gif)) showing on the screen for 3 second and this gif image unvisible then another image load into picturebox.
on the other hand How to display a wait gif until image is fully loaded in c#.net 3.5
please help me
thanks
You could use the PictureBox.LoadAsync(string url). The PictureBox will then raise
LoadProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
and
LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
events.

Load image in PictureBox before make control visible

I have a form with black background, with 9 picture boxes.
When the program starts, I want to show 9 images using these picture boxes.
However, the picture boxes take time to load the picture. It is quite ugly that the picture boxes show up first while waiting.
Is there a way I can move from blank black screen to straightaway 9 images, without the visible loading in between?
Thanks.
How are you loading your Form (I guess you are talking about Windows.Forms here)?
You can just create a new Window class and load your pictures and then after all is done call the Show method.
MyForm form = new MyForm ();
form.DoLoadImages ();
form.Show ();
Or you can just set the WaitOnLoad property of the PictureBox to true.
private void startButton_Click(object sender, EventArgs e)
{
// Ensure WaitOnLoad is false.
pictureBox1.WaitOnLoad = false;
// Load the image asynchronously.
pictureBox1.LoadAsync(#"http://localhost/print.gif");
}
Courtesy of MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.waitonload(v=VS.100).aspx1

Categories