簡體   English   中英

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

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

我想對屏幕的一部分進行截圖,然后將該信息存儲在圖像數組中。 有沒有辦法修改這個類: http//pastebin.com/PDPPxmPT這樣它可以讓我截取特定區域的截圖? 例如,如果我想要位圖的x,y像素來源為200,200且x,y目的地為600,700,我將如何進行此操作?

在這里修改

編輯:這是代碼

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的代碼,因為它將來可能會在某個時候消失,並且可能對其他人有用。

    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