如何保存桌面窗口但是只有一半?

时间:2014-10-01 08:51:55

标签: c# .net winforms

这是我现在正在使用的form1中的代码。 我每秒使用一个计时器捕获桌面窗口。 最后我有一个按钮点击事件,如果我点击它,它将在我的硬盘上从所有保存的gif文件创建动画gif文件。

但是当我在Internet Explorer上加载/运行动画gif时,我看到整个屏幕上的动画更大,所以我需要使用并移动滚动条以便每次都能看到它。

如何保存桌面窗口,例如一半尺寸? 现在在我的硬盘上我有大约100个gif文件,每个文件大小为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);
        }
    }
}

我想捕捉整个桌面窗口的截图,但是在显示它时显示它不是那么大。

2 个答案:

答案 0 :(得分:2)

您可以从此处更改尺寸

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
}

这将创建一个比屏幕短100px的Bitmap对象。

答案 1 :(得分:1)

您可以像这样更改screenshots功能:

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();

}

只需快速说明:在使用Graphics时,最好采用Using statement,以便提供方便的语法,以确保正确使用IDisposable个对象。

例如:

using (Graphics g = Graphics.FromImage(bmpHalfSize)) {
    g.DrawImage(bmpScreenshot, 0, 0, CInt(Screen.PrimaryScreen.Bounds.Width / 2), CInt(Screen.PrimaryScreen.Bounds.Height / 2));
}