c# 截屏

直接可运行,运行时输入命令 格式如:  cap [地址]

地址为需要保存的截屏文件地址,比如 D://save.jpg


class Program
    {
        public static void Capture(String filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                // Create a random file name by GUID and set the saved 
                // directory at current directory.
                filePath = Guid.NewGuid().ToString();
            }

            // Create a bitmap for save
            Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                    Screen.PrimaryScreen.Bounds.Height,
                                    PixelFormat.Format48bppRgb);

            // Create a graphic for drawing from bmp
            Graphics screenG = Graphics.FromImage(bmp);

            screenG.RotateTransform(90.0f);

            screenG.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                   Screen.PrimaryScreen.Bounds.Y,
                                   0, 0,
                                   Screen.PrimaryScreen.Bounds.Size,
                                   CopyPixelOperation.SourceCopy
                                   );

           // screenG.ScaleTransform(0.5f,0.5f);
           

            bmp.Save(filePath, ImageFormat.Jpeg);
        }

        static void Main(string[] args)
        {
            String cmd = "";

            while (true)
            {
                cmd = Console.ReadLine();

                char[] spliter ={ ' ' };
                String[] cmds = cmd.Split(spliter);

                if (cmds[0].Equals("exit"))
                {
                    break;
                }

                else if (cmds[0].Equals("cap"))
                {
                    if (cmds.Length < 2)
                    {
                        Console.WriteLine("format error, need a filepath");
                    }
                    else
                    {
                        Capture(cmds[1]);
                        Console.WriteLine("image saved!");
                    }
                }
                else
                {
                    Console.WriteLine("unkown command!");
                }
            }

        }


你可能感兴趣的:(c# 截屏)