简体   繁体   English

在C#中截取屏幕的某些部分?

[英]Taking a screenshot of certain section of screen in C#?

I want to take a screenshot of a section of my screen and then store that information in an image array. 我想对屏幕的一部分进行截图,然后将该信息存储在图像数组中。 Is there a way to modify this class: http://pastebin.com/PDPPxmPT so that it will allow me to take a screenshot of a specific area? 有没有办法修改这个类: http//pastebin.com/PDPPxmPT这样它可以让我截取特定区域的截图? For example, if I wanted the x, y pixel origins of the bitmap to be 200, 200 and the x, y destinations to be 600, 700, how would I go about doing that? 例如,如果我想要位图的x,y像素来源为200,200且x,y目的地为600,700,我将如何进行此操作?

I mod it here 在这里修改

EDIT : Here's the code 编辑:这是代码

public static Color[,] takeScreenshot(int x=0, int y=0, int width=0, int height = 0)
        {
    if (width==0)
        width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
    if (height==0)
        height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            Bitmap screenShotBMP = new Bitmap(width,
                height, PixelFormat.Format32bppArgb);

            Graphics screenShotGraphics = Graphics.FromImage(screenShotBMP);

            screenShotGraphics.CopyFromScreen(x,
                y, 0, 0, new Size(width,height),
                CopyPixelOperation.SourceCopy);

            screenShotGraphics.Dispose();

            return bitmap2imagearray(screenShotBMP);
        } 

public static Color[,] bitmap2imagearray(Bitmap b)
        {
            Color[,] imgArray = new Color[b.Width, b.Height];
            for (int y = 0; y < b.Height; y++)
            {
                for (int x = 0; x < b.Width; x++)
                {
                    imgArray[x, y] = b.GetPixel(x, y);
                }
            }
            return imgArray;
        }

Not an answer, but including the code from pastebin as it'll likely disappear sometime in the future and may be useful for others. 不是答案,而是包括来自pastebin的代码,因为它将来可能会在某个时候消失,并且可能对其他人有用。

    public static Color[,] takeScreenshot()
    {
        Bitmap screenShotBMP = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
            System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

        Graphics screenShotGraphics = Graphics.FromImage(screenShotBMP);

        screenShotGraphics.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X,
            System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y, 0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size,
            CopyPixelOperation.SourceCopy);

        screenShotGraphics.Dispose();

        return bitmap2imagearray(screenShotBMP);
    } 

    public static Color[,] bitmap2imagearray(Bitmap b)
    {
        Color[,] imgArray = new Color[b.Width, b.Height];
        for (int y = 0; y < b.Height; y++)
        {
            for (int x = 0; x < b.Width; x++)
            {
                imgArray[x, y] = b.GetPixel(x, y);
            }
        }
        return imgArray;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM