如何图形CopyFromScreen方法复制到一个位图? [英] How does the Graphics CopyFromScreen method copy into a bitmap?

查看:197
本文介绍了如何图形CopyFromScreen方法复制到一个位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void startBot_Click(object sender, EventArgs e)
{
        Bitmap bmpScreenshot = Screenshot();
        this.BackgroundImage = bmpScreenshot;
}

private Bitmap Screenshot()
{

    // This is where we will store a snapshot of the screen
    Bitmap bmpScreenshot = 
        new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);

    // Creates a graphic object so we can draw the screen in the bitmap (bmpScreenshot);
    Graphics g = Graphics.FromImage(bmpScreenshot);

    // Copy from screen into the bitmap we created
    g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);

    // Return the screenshot
    return bmpScreenshot;
}



我用C#最近被玩弄,我只是遵循一些教程中,我只是不知道如何,如果我是删除图形摹它不会把图像作为背景,但在任何时候不代码分配任何关系变量,比图形G = Graphics.FromImage(bmpScreenshot),那么先按g 给出一些参数,但彼此之间那么我们返回bmpScreenshot 这只是没有任何意义,我希望要返回先按g

I've been recently playing around with C# and I'm just following some tutorial, I just don't understand how if I was to erase Graphics g it wouldn't put the image as the background, but at no point does the code assign any relation between the variables, other than Graphics g = Graphics.FromImage(bmpScreenshot), then g is given some parameters, but then we return bmpScreenshot which just doesn't make any sense, I would expect g to be returned?

推荐答案

这可以显示图形在Windows虚拟化设备。这个概念被称为在WINAPI一个设备上下文,基本表示是处理。该处理Graphics类包装,它本身并不存储像素。注意Graphics.GetHdc()方法,一种方式来获得该句柄。

Devices that can display graphics are virtualized in Windows. The concept is called a "device context" in the winapi, underlying representation is a "handle". The Graphics class wraps that handle, it does not itself store the pixels. Note the Graphics.GetHdc() method, a way to get to that handle.

类,否则只包含了由该所代表的设备上产生图形输出的绘图方法处理。实际设备可以在屏幕上,一台打印机,一元文件,位图。随着你自己的代码中的一大优势,它可以被用来生产在任何你想要去的输出。所以,印刷只是因为它画到屏幕或绘图您存储到文件的位图一样容易。

The class otherwise just contains the drawing methods that produce graphics output on the device represented by that handle. Actual devices can be the screen, a printer, a metafile, a bitmap. With the big advantage in your own code that it can be used to produce output where ever you want it to go. So printing is just as easy as painting it to the screen or drawing to a bitmap that you store to a file.

因此,通过调用Graphics.FromImage(),你准Graphics对象的位图。它的所有draw方法实际上是在设置位图像素。像CopyFromScreen(),从视频适配器的帧缓冲器的设备上下文它简单地拷贝像素,实际上设置位图中的像素。所以这段代码的预期收益值是实际的位图。这种情况发生之前,因为它已不再是有用的图形对象应该被设置。或者换句话说,底层手柄需要被释放,操作系统去分配自己的资源来代表设备上下文。

So by calling Graphics.FromImage(), you associate the Graphics object to the bitmap. All of its draw methods actually set pixels in the bitmap. Like CopyFromScreen(), it simply copies pixels from the video adapter's frame buffer to the device context, in effect setting the pixels in the bitmap. So the expected return value of this code is the actual bitmap. The Graphics object should be disposed before that happens since it is no longer useful. Or in other words, the underlying handle needs to be released so the operating system de-allocates its own resources to represent the device context.

这是在代码片段中的错误。此方法重复调用可以很容易崩溃的程序当Windows拒绝创造更多的设备上下文。而垃圾回收器不会否则追赶速度不够快。应该写成:

That's a bug in the code snippet. Repeated calls to this method can easily crash the program when Windows refuses to create more device contexts. And the garbage collector doesn't otherwise catch up fast enough. It should be written as:

  using (var g = Graphics.FromImage(bmpScreenshot)) {
      g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
      return bmpScreenshot;
  }

这篇关于如何图形CopyFromScreen方法复制到一个位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆