Several methods of taking a screenshot

Method One: capture a screenshot

private Bitmap GetScreenCapture()
{
     Rectangle tScreenRect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
     Bitmap tSrcBmp = new Bitmap(tScreenRect.Width, tScreenRect.Height); // 用于屏幕原始图片保存
     Graphics gp = Graphics.FromImage(tSrcBmp);
     gp.CopyFromScreen(0, 0, 0, 0, tScreenRect.Size);
     gp.DrawImage(tSrcBmp, 0, 0, tScreenRect, GraphicsUnit.Pixel);
     return tSrcBmp;
}

Method Two:

Bitmap ImgRgbToGray static public (Bitmap Bitmap)
{
     Bitmap Bitmap new new B = (Bitmap);
     the BitmapData bmData = b.LockBits (new new the Rectangle (0, 0, b.Width, b.Height),
     ImageLockMode.ReadWrite,
     PixelFormat.Format24bppRgb);
     int stride = bmData.Stride; // scan width of
     the unsafe
     {
         byte * P = (byte *) bmData.Scan0.ToPointer (); // address of the first image acquiring
         int nOffset = stride - b.Width * 3 ; // actual the width of the system from the width
         byte Red, Green, Blue;
         for (int Y = 0; Y <b.Height; Y ++)
         {
             for (int X = 0; X <b.Width; X ++)
             {
                 Blue P = [0];
                 Green = P [. 1];
                 red = p[2];

                p [0] = p [1] = p [2] = (byte) (299 * red + .587 * green + .114 * blue.); // conversion formula

                p + = 3; // skip the next three bytes processed pixel
             }
             P + = nOffset; // plus the spacing
         }
     }
     b.UnlockBits (bmData); // unlock the
     return B;
}

Specific theme implementation of the method
is assumed to be at the local coordinates (55,88), taken as a rectangular aspect (520,233), the entire background ashing theme, the display picture area screenshot

Using the method of replacing the pixels
of the code is the easiest method, core function is GetPixel, SetPixel, but the speed flattered! ! ! Particularly slow !!!!!!!!


Method three:

Method on the original drawing of
the method is faster, but each time it needs to be redrawn, changing the base map

private Bitmap DrawOnPicture()
{
     int tAbsWidth = 1;
     Bitmap tSrcBtm = GetScreenCapture();
     Bitmap tGrayBtm = ImgRgbToGray(tSrcBtm);
     Rectangle tRectArea = new Rectangle(55, 88, 520, 233);
     Rectangle tDrawArea = new Rectangle(55 + tAbsWidth, 88 + tAbsWidth, 520 - tAbsWidth, 233 - tAbsWidth);
     Graphics gp = Graphics.FromImage(tGrayBtm);
     gp.DrawRectangle(Pens.Red, tRectArea);
     gp.DrawImage(tSrcBtm, tDrawArea, tRectArea, GraphicsUnit.Pixel);

    return tGrayBtm;
}

Use the mask method
is actually on the drawing pictures, but painted directly on the background of a gray rect covered screen, rather than take the grayscale

public void CaptureScreen()
{
     // 获取屏幕
     Bitmap btp = GetScreenCapture();
     Image BackScreen = new Bitmap(btp);

    // 在 BackScreen 上绘制蒙板
     Graphics g = Graphics.FromImage(BackScreen);
     g.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 0)), 0, 0, BackScreen.Width, BackScreen.Height);
     this.BackgroundImage = BackScreen;

    // maximize
     this.WindowState = FormWindowState.Maximized;

    // do not display the Taskbar
     this.ShowInTaskbar = false;
     this.TopMost = to true;

    // borderless
     this.FormBorderStyle = FormBorderStyle.None;

    // where coordinates (55,88) for the length and width taken (520,233) is rectangular
     the Rectangle RECT = new new the Rectangle (55, 88, 520., 233);
     g.drawImage (BTP, RECT, RECT, GraphicsUnit.Pixel) ;

    this.Show();
}

Guess you like

Origin www.cnblogs.com/snsnetw/p/12374597.html