簡體   English   中英

屏幕截圖捕獲非常慢

[英]Screenshot capture is extremely slow

我正在編寫一個可以截取屏幕截圖的應用程序,它需要以極快的速度(每秒數秒)來處理它們。 這是我用來執行此操作的代碼-可以工作,但是速度非常慢。

using System.Drawing;
using System.Drawing.Imaging;

public static Bitmap CaptureScreen()
{
    Bitmap BMP = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
                            System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,
                            System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    System.Drawing.Graphics GFX = System.Drawing.Graphics.FromImage(BMP);
    GFX.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X,
                        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y,
                        0, 0,
                        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size,
                        System.Drawing.CopyPixelOperation.SourceCopy);

    return BMP;
}

用法-:

Bitmap im1 = new Bitmap(CaptureScreen());

上面的代碼可以正常工作,但至少需要5秒鍾來處理。 所以有人可以提供一種與上述方法類似的方法,但是速度更快,我也想使用前景窗口而不是整個屏幕進行捕獲。

編輯這是比較代碼!

private void timer2_Tick(object sender, EventArgs e)
{

    pictureBox1.Image = CaptureScreen();
    pictureBox2.Image = CaptureScreenOld();
    Bitmap im1 = (Bitmap)pictureBox1.Image;
    Bitmap im2 = (Bitmap)pictureBox2.Image;
    for (int y = 0; y < pictureBox1.Height; y++)
    {
        for (int x = 0; x < pictureBox1.Width; x++)
        {
            // Get the color of the current pixel in each bitmap
            Color color1 = im1.GetPixel(x, y);
            Color color2 = im2.GetPixel(x, y);

            // Check if they're the same
            if (color1 != color2)
            {
                // If not, generate a color...
                Color myRed = Color.FromArgb(90, 0, 0);
                // .. and set the pixel in one of the bitmaps
                im2.SetPixel(x, y, myRed);
                pictureBox2.Image = im2;
            }
        }
    }
}

好的,多次使用GetPixel比較像素確實很慢。 而是將您的位鎖定到一個數組中:

private static void CompareBitmaps(Bitmap a, Bitmap b) {
    Stopwatch sw = new Stopwatch();

    sw.Start();

    var lockRect = new Rectangle(0, 0, a.Width, a.Height);
    BitmapData ad = a.LockBits(lockRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    BitmapData bd = b.LockBits(lockRect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

    Int32[] apixels = new Int32[ad.Width * ad.Height];
    Int32[] bpixels = new Int32[bd.Width * bd.Height];

    Marshal.Copy(ad.Scan0, apixels, 0, apixels.Length);
    Marshal.Copy(bd.Scan0, bpixels, 0, bpixels.Length);

    for(int i = 0; i < apixels.Length; i++) {
        if(apixels[i] != bpixels[i]) {
            unchecked {
                bpixels[i] = (int)0xff5a0000;
            }
        }
    }

    Marshal.Copy(bpixels, 0, bd.Scan0, bpixels.Length);

    a.UnlockBits(ad);
    b.UnlockBits(bd);

    sw.Stop();

    Console.WriteLine("CompareBitmaps took {0}.", sw.Elapsed);
}

我得到的時間約為240毫秒; 夠了嗎

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM