Jenny Mchammer
Jenny Mchammer

Reputation: 95

Why does this code save the screenshot as bmp not gif, and why doesn't it save the whole area?

I have a timer tick event set to 250ms:

private void timer4_Tick(object sender, EventArgs e)
{
    if (pictureboximagestosavecount == 72)
    {
        timer4.Enabled = false;
    }
    else
    {
        Bitmap bmp = new Bitmap(this.Width, this.Height);
        Rectangle rect = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
        pictureboximagestosavecount++;
        savePictureBox(pictureBox1, @"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "pbimg.gif");
        this.DrawToBitmap(bmp, rect);
        bmp.Save(@"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "form.gif");
    }
}

First I'm saving the pictureBox as gif using the method savePictureBox. Second I save the form1:

bmp.Save(@"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "form.gif");

After timer4 is stopped I have abutton click event where I create animated gif from the saved files:

private void animatedgifbutton_Click(object sender, EventArgs e)
{
    DirectoryInfo di1;
    FileInfo[] fi1;
    di1 = new DirectoryInfo(@"c:\temp\pboximages\");
    fi1 = di1.GetFiles("*form.gif");
    List<string> newImages = new List<string>();
    for (int i = 0; i < fi1.Length; i++)
    {
        newImages.Add(fi1[i].FullName);
    }
    animatedgif.MakeGIF(newImages, @"c:\temp\pboximages\animated1.gif", 6, true);
}

When I'm doing *.form.gif and I see in the List newImages only the form gifs files the animatedgif.MakeGIF throw error since it need to get List of gifs files but I guess that when I save the form it's saving it as bitmap and not real gif.

How can I take a screenshot of form1 and save it as real gif ? When I save the pictureBox1 to the hard disk it is GIF and there are no problems.

The problem is with saving the form.

EDIT:

I also saw now that the way I'm saving a screenshot of the form1 is not good it's not saving the whole form area only part of it. I gues using this bmp and rect is not good. This is what I get for example when I'm saving a screenshot of the form:

Form screenshot

In other words I need in timer4 tick event to save the form screen the whole form to the hard disk as a gif file real gif file not bmp.

Upvotes: 0

Views: 234

Answers (1)

TaW
TaW

Reputation: 54453

It doesn't save as a proper GIF because you don't tell it to. The extension is not enough, you need to add the ImageFormat to the Save call!

And it doesn't save the whole area of the Form because you don't use the right Rectangle to control it.

Both issues go away very simply if you use the modified savePictueBox function:

void saveControl(Control Ctl, string fileName)
{
    Rectangle Rect = Ctl.ClientRectangle;
    // if (Ctl is Form) Rect = Ctl.Bounds; // (*)
    using (Bitmap bmp = new Bitmap(Rect.Width, Rect.Height))
    {
        Ctl.DrawToBitmap(bmp, Rect );
        bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
    }
}

Note that this uses the ClientRectangle, that is, it leaves out the Form border. If you want if to include the Border you can uncomment the line (*)

Now you can call it as

saveControl(pictureBox1, yourFileName);

and as

saveControl(this, yourOtherFileName);

Two notes on Animated Gifs:

  • Since you are recording the Gifs in real-time don't try to create the animation at the same time!
  • I found this post interesting. fireydude's answer does work, once you have included all those references from the WPF world..Not sure how to get the best quality and unfortunately there is no control over the timing, so far..

Upvotes: 7

Related Questions