0
我正在編寫一個可以截取截圖的應用程序,它需要將它們超快(多個秒)然後處理它們。這裏是我用來做它的代碼 - 它可以工作,但速度非常慢。截圖捕捉速度非常緩慢
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;
}
}
}
}
相關:http://stackoverflow.com/questions/1163761/c-sharp-capture-screenshot-of-active-window – Vulcan
你需要做什麼與圖像?爲什麼你將整個圖像重新複製到另一個「位圖」? '位圖im1 = CaptureScreen();'應該節省很多時間。和*五*秒?我需要更少的時間來在屏幕截圖上運行框模糊......這個屏幕有多大? – Ryan
在我的系統(1680 x 1050分辨率)上運行相同的代碼約120ms。 –