簡體   English   中英

隱藏Form1也會被捕獲

[英]Form1 is being captured even after hiding it

我正在使用c#開發一個截圖工具應用程序。 單擊Form1中的捕獲按鈕時,我將隱藏Form1並獲取背景屏幕的屏幕截圖,但是問題是即使我執行this,屏幕上仍顯示Form1窗口的屏幕截圖。 聲明,然后截取屏幕截圖。 請幫助我如何使Form1不出現在屏幕截圖中。

這是我的代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public static Image fullDesktop;
        public static Image partIamge;
        public static Form backgroundForm;
        public static PictureBox picbox1;

        public static Point mouseDownAt;
        public static Point mouseIsAt;
        public static bool isMouseDown = false;

        public static Color backgroundColor = Color.DarkGray;
        public static int backgroundAlpha = 150;
        public static Color outlineColor = Color.Red;
        public static int outlineWidth = 1;

        public Form1()
        {
            InitializeComponent();
        }

        public void Initialize()
        {
            backgroundForm = new Form();
            backgroundForm.FormBorderStyle = FormBorderStyle.None;
            backgroundForm.WindowState = FormWindowState.Maximized;
            backgroundForm.Cursor = Cursors.Cross;

            picbox1 = new PictureBox();
            picbox1.Size = Screen.FromControl(backgroundForm).Bounds.Size;
            picbox1.Location = new Point(0, 0);
            picbox1.Image = fullDesktop;
            picbox1.BorderStyle = BorderStyle.None;
            picbox1.Paint += new PaintEventHandler(OnPaint);
            picbox1.MouseDown += new MouseEventHandler(OnMouseDown);
            picbox1.MouseMove += new MouseEventHandler(OnMouseMove);
            picbox1.MouseUp += new MouseEventHandler(OnMouseUp);
            backgroundForm.KeyDown += new KeyEventHandler(OnKeyDown);
            backgroundForm.KeyPreview = true;
            backgroundForm.Controls.Add(picbox1);
        }

        public void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                backgroundForm.Close();
        }
        public void OnPaint(object sender, PaintEventArgs e)
        {
            SolidBrush opaqueWhiteBrush = new SolidBrush(Color.FromArgb(backgroundAlpha, backgroundColor.R, backgroundColor.G, backgroundColor.B));
            e.Graphics.FillRectangle(opaqueWhiteBrush, 0, 0, picbox1.Width, picbox1.Height);

            if (isMouseDown)
            {
                Rectangle pos = getMouseMoveRect;
                e.Graphics.DrawRectangle(new Pen(outlineColor, outlineWidth), new Rectangle(pos.X - outlineWidth, pos.Y - outlineWidth, pos.Width + outlineWidth, pos.Height + outlineWidth));
                e.Graphics.DrawImage(partIamge, pos.Location);

                //string displayText = "W: " + pos.Width + " H: " + pos.Height;
                //Font font = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Pixel);
                //e.Graphics.DrawString(displayText, font, Brushes.White, pos.X, pos.Y - font.Size - 6, StringFormat.GenericDefault);
            }
        }

        public void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && !isMouseDown)
            {
                //Application.Exit();
                //backgroundForm.Close();
            }
            else if (e.Button == MouseButtons.Left)
            {
                mouseDownAt = e.Location;
                Cursor.Position = new Point(e.Location.X + 1, e.Location.Y + 1);
                mouseIsAt = new Point(e.Location.X + 1, e.Location.Y + 1);
                partIamge = ((Bitmap)fullDesktop).Clone(getMouseMoveRect, fullDesktop.PixelFormat);
                isMouseDown = true;
                picbox1.Refresh();
            }
        }

        public void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                mouseIsAt = e.Location;
                Rectangle rect = getMouseMoveRect;
                if (rect.Width != 0 && rect.Height != 0)
                {
                    partIamge = ((Bitmap)fullDesktop).Clone(rect, fullDesktop.PixelFormat);
                    picbox1.Refresh();
                }
            }
        }

        public void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                bool hasMoved = false;

                if (isMouseDown)
                {
                    hasMoved = true;
                    isMouseDown = false;
                }

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "PNG image (*.png)|*.png";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    partIamge.Save(sfd.FileName, ImageFormat.Png);
                    Clipboard.SetImage(partIamge);
                }
                else
                {
                    hasMoved = false;
                    picbox1.Refresh();
                }

                if (hasMoved)
                {
                    backgroundForm.Close();
                }
            }
        }

        public Rectangle getMouseMoveRect
        {
            get
            {
                int x = 0;
                int y = 0;
                int width = 0;
                int height = 0;

                if (mouseIsAt.X > mouseDownAt.X)
                {
                    x = mouseDownAt.X;
                    width = mouseIsAt.X - mouseDownAt.X;
                }
                else
                {
                    x = mouseIsAt.X;
                    width = mouseDownAt.X - mouseIsAt.X;
                }

                if (mouseIsAt.Y > mouseDownAt.Y)
                {
                    y = mouseDownAt.Y;
                    height = mouseIsAt.Y - mouseDownAt.Y;
                }
                else
                {
                    y = mouseIsAt.Y;
                    height = mouseDownAt.Y - mouseIsAt.Y;
                }

                return new Rectangle(x, y, width, height);
            }
        }

        public Image CaptureScreen
        {
            get
            {
                Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                using (Graphics g = Graphics.FromImage(image))
                {
                    g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                             Screen.PrimaryScreen.Bounds.Y,
                             0, 0,
                             image.Size,
                             CopyPixelOperation.SourceCopy);
                }

                return image;
            }
        }

        private void cmdCapture_Click(object sender, EventArgs e)
        {

            this.Hide();

            fullDesktop = CaptureScreen;
            Initialize();

            backgroundForm.ShowDialog();
            this.Show();
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Closed(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cmdFullCapture_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Hide();

            while (IsOnScreen(this)) ;
            fullDesktop = CaptureScreen;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "PNG image (*.png)|*.png";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                fullDesktop.Save(sfd.FileName, ImageFormat.Png);
                Clipboard.SetImage(partIamge);
            }
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

    }
}

提前致謝。

在舊的Windows版本上,這是一個問題,您捕獲了視頻適配器的幀緩沖區中的像素。 隱藏窗口時,其他窗口需要先重新繪制自身,以更新曾經由窗口繪制的緩沖區中的像素。 這需要時間,這是不可預測的時間。 您根本不需要等待,因此仍然可以看到窗口的舊像素。

有一個簡單的解決方法,您也可以使用表單的Opacity屬性來隱藏窗口。 不透明度是通過在視頻適配器本身內部分層來實現的。 在設計器中將“不透明度”設置為99。 實際上這已經足夠,您的窗口將不再被捕獲。 這實際上是一個錯誤,已在Windows 8中修復。因此,不要那樣做,在截屏之前,在代碼中將不透明度設置為0,然后再將其設置為0.99(而不是錯字)。

也許您可以嘗試以編程方式更改Form的WindowState

代替:

this.Hide();

嘗試:

this.WindowState = FormWindowState.Minimized;

然后截取屏幕截圖:

this.WindowState = FormWindowState.Normal;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM