【问题标题】:Taking a screenshot of certain section of screen in C#?在 C# 中截取屏幕的某些部分?
【发布时间】:2010-12-31 13:01:32
【问题描述】:

我想截取屏幕的一部分,然后将该信息存储在图像数组中。有没有办法修改这个类: http://pastebin.com/PDPPxmPT 这样它就可以让我截取特定区域的屏幕截图?例如,如果我希望位图的 x、y 像素原点为 200、200,x、y 目标为 600、700,我该怎么做呢?

【问题讨论】:

  • 请在您的问题中发布您的代码,而不是在 Pastebin 上。

标签: c# bitmap screenshot


【解决方案1】:

我修改它here

编辑:这是代码

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;
        }

【讨论】:

  • 请在您的答案中发布您的代码,而不是在 Pastebin 上。
【解决方案2】:

不是答案,但包括来自 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;
    }

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    相关资源
    最近更新 更多