0

This is the code in form1 i'm using now. I'm capturing the desktop window using a timer each second. In the end i have a button click event and if i click on it, it will create animated gif file on my hard disk from all the saved gif files.

But when i'm loading/running the animated gif on internet explorer for example i see the animation on the whole screen even bigger so i need to use and move the scrollbars to see it all each time.

How can i save the desktop window but half size for example ? Now on my hard disk i have about 100 gif files each one at size 380-407kb.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO;
using unfreez_wrapper;

namespace CapturedDesktop
{
    public partial class Form1 : Form
    {
        int screens;
        string gifsdirectory;
        UnFreezWrapper unfreez;

        public Form1()
        {
            InitializeComponent();

            unfreez = new UnFreezWrapper();
            timer1.Enabled = false;
            screens = 0;
            gifsdirectory = @"C:\Temp\CapturedDesktop\";
        }


        private void CaptureScreenshot()
        {
            screens++;
            screenshots(gifsdirectory + screens.ToString("D6") + ".gif");
        }

        public static void screenshots(string filename)
        {
            //Create a new bitmap.
            var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                           Screen.PrimaryScreen.Bounds.Height,
                                           PixelFormat.Format32bppArgb);

            // Create a graphics object from the bitmap.
            var gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            // Take the screenshot from the upper left corner to the right bottom corner.
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                        Screen.PrimaryScreen.Bounds.Y,
                                        0,
                                        0,
                                        Screen.PrimaryScreen.Bounds.Size,
                                        CopyPixelOperation.SourceCopy);

            // Save the screenshot to the specified path that the user has chosen.
            bmpScreenshot.Save(filename, ImageFormat.Gif);
            bmpScreenshot.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            CaptureScreenshot();
        }

        private void AnimatedGifButton_Click(object sender, EventArgs e)
        {
            List<string> myGifList = new List<string>();
            FileInfo[] fi;
            DirectoryInfo dir1 = new DirectoryInfo(gifsdirectory);
            fi = dir1.GetFiles("*.gif");
            for (int i = 0; i < fi.Length; i++)
            {
                myGifList.Add(fi[i].FullName);
            }
            unfreez.MakeGIF(myGifList, gifsdirectory + "agif", 100, true);
        }
    }
}

I want to capture take screenshots of the whole desktop window but when displaying it to show it not so big.

James Aharon
  • 223
  • 1
  • 4
  • 13

2 Answers2

2

You can change the size from here

public static void screenshots(string filename)
{
//Create a new bitmap.
var bmpScreenshot = new Bitmap(
                                (Screen.PrimaryScreen.Bounds.Width - 100),
                                (Screen.PrimaryScreen.Bounds.Height - 100),
                                PixelFormat.Format32bppArgb
                              );
// other code
}

This will create a Bitmap object which is 100px shorter than the screen.

yogi
  • 19,175
  • 13
  • 62
  • 92
1

You can change your screenshots function like this:

public static void screenshots(string filename)
{
    //Create a new bitmap.
    var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                   Screen.PrimaryScreen.Bounds.Height,
                                   PixelFormat.Format32bppArgb);

    // Create a graphics object from the bitmap.
    var gfxScreenshot = Graphics.FromImage(bmpScreenshot);

    // Take the screenshot from the upper left corner to the right bottom corner.
    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                Screen.PrimaryScreen.Bounds.Y,
                                0,
                                0,
                                Screen.PrimaryScreen.Bounds.Size,
                                CopyPixelOperation.SourceCopy);

    // Save the screenshot to the specified path that the user has chosen.
    //bmpScreenshot.Save(filename, ImageFormat.Gif);
    //bmpScreenshot.Dispose();

    var bmpHalfSize = new Bitmap(Screen.PrimaryScreen.Bounds.Width / 2,
        Screen.PrimaryScreen.Bounds.Height / 2,
        System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    var g = Graphics.FromImage(bmpHalfSize);

    g.DrawImage(bmpScreenshot, 0, 0, CInt(Screen.PrimaryScreen.Bounds.Width / 2), CInt(Screen.PrimaryScreen.Bounds.Height / 2));

    bmpHalfSize.Save(filename, ImageFormat.Gif);
    bmpHalfSize.Dispose();

    bmpScreenshot.Dispose();

}

Just a quick note: when playing with Graphics is better adopt Using statement in order to provides a convenient syntax that ensures the correct use of IDisposable objects.

For example:

using (Graphics g = Graphics.FromImage(bmpHalfSize)) {
    g.DrawImage(bmpScreenshot, 0, 0, CInt(Screen.PrimaryScreen.Bounds.Width / 2), CInt(Screen.PrimaryScreen.Bounds.Height / 2));
}
tezzo
  • 10,858
  • 1
  • 25
  • 48