在 C# 中截取屏幕特定部分的屏幕截图?

发布于 2024-10-09 23:58:17 字数 209 浏览 1 评论 0原文

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

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? 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

树深时见影 2024-10-16 23:58:17

我修改了这里

编辑:这是代码

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

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;
        }
迷你仙 2024-10-16 23:58:17

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

Not an answer, but including the code from pastebin as it'll likely disappear sometime in the future and may be useful for others.

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文