Capture Screen But Not Current Form In C#?

旧街凉风 提交于 2019-12-11 09:09:15

问题


I want to capture the screen but without the current form, I tried to minimize -> capture -> maximize, but I'm looking for a better solution if exists. This was my code::

    int w = this.Width;
    int h = this.Height;
    Size sz = this.Size;
    Point loc = this.Location;

    this.WindowState = FormWindowState.Minimized;

    System.Threading.Thread.Sleep(500);

    using (Bitmap b = new Bitmap(w, h))
    {
        using (Graphics g = Graphics.FromImage(b))
        {
            g.CopyFromScreen(loc, new Point(0, 0), sz);
        }

        b.Save(Environment.CurrentDirectory + "\\slides\\screen.jpeg");

        this.BackgroundImage = System.Drawing.Image.FromFile(@"slides\screen.jpeg");
    }

    this.WindowState = FormWindowState.Maximized;

And I ask if there is a direct way to put the captured image as form background, without saving it.


回答1:


Try, although I did notice both yours and mine is marginally offset by the form border.

    int w = this.Width; 
    int h = this.Height; 
    Size sz = this.Size; 
    Point loc = this.Location; 
    Hide();
    System.Threading.Thread.Sleep(500);
    using (Image b = new Bitmap(w, h)) 
    { using (Graphics g = Graphics.FromImage(b)) 
    { 
        g.CopyFromScreen(loc, new Point(0, 0), sz); }

        Image x = new Bitmap(b);
        this.BackgroundImage = x;
    } 
    Show();



回答2:


  1. Set the forms Opacity to 0.

    This will make the form completely transparent and hence invisible.

  2. Make the form invisible. Set the Visible property to `false.

    This will hide the form without changing it's state, though on XP this won't make the window disappear straight away.



来源:https://stackoverflow.com/questions/10025151/capture-screen-but-not-current-form-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!