关闭

C# 抓屏程序

标签: c#objecttextboxwindowsstringuser
2114人阅读 评论(0) 收藏 举报
分类:

由于工作关系,常常需要将一些软件的内容抓屏保存下来,原来抓的屏不是很多,一直采用Ctrl+C,Ctrl+V,另存的方法。最近几次,由于工作量比较大,差点累得手抽筋。于是找了个抓屏软件,但是其功能太强大了,哈哈。还是自己弄一个比较好。

1[静心编程]blog中提到:

           // 模拟按键,将当前窗口图像截取到剪贴板:
            SendKeys.SendWait("%{PRTSC}"); //
模拟 Alt+PrtSc,如果要截取整个屏幕,把 Alt (%) 去掉

//获取剪贴板里的图像:
            Bitmap myCapture = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
            //
要显示图像可以这么做:
            //  this.pictureBox1.Image = myCapture;
            //
要保存图像可以这么做:
            myCapture.Save(mysettingForm.savepath + savename + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
   2
。那么怎么让软件在后台捕获键盘呢。

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(IntPtr hWnd,// handle to window
        int id, // hot key identifier
        KeyModifiers fsModifiers, // key-modifier options
        Keys vk // virtual-key code
        );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(IntPtr hWnd,// handle to window
        int id // hot key identifier
        );

        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8
        }

        protected override void WndProc(ref Message m)//循环监视Windows消息

{}

是我的选择。

3。最后加上,保存路径和保存名字等一些无关紧要的东西。

源码如下:

//////////////////Form1.cs/////////////////////////////////

      [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(IntPtr hWnd,// handle to window
        int id, // hot key identifier
        KeyModifiers fsModifiers, // key-modifier options
        Keys vk // virtual-key code
        );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(IntPtr hWnd,// handle to window
        int id // hot key identifier
        );

        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8
        }

        private Setting mysettingForm = new Setting ();

        private int number = 0;//保存一系列同名图像时的编号
        private  string oldname = "Helios";

        private void printprocess()
        {
            //
模拟按键,将当前窗口图像截取到剪贴板:
            SendKeys.SendWait("%{PRTSC}"); //
模拟 Alt+PrtSc,如果要截取整个屏幕,把 Alt (%) 去掉
            // SendKeys.SendWait("{PRTSC}"); //
模拟 Alt+PrtSc,如果要截取整个屏幕,把 Alt (%) 去掉

            // 获取剪贴板里的图像:
            Bitmap myCapture = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
            //
要显示图像可以这么做:
            //  this.pictureBox1.Image = myCapture;
            //
要保存图像可以这么做:
            if (oldname != mysettingForm.savename)//
判断存档名字是否变化
                number = 0;

            string savename = mysettingForm.savename + (++number).ToString().PadLeft(5, '0');
            myCapture.Save(mysettingForm.savepath + savename + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
           
            oldname = mysettingForm.savename;   //
保留当前存档名字
      
            Clipboard.Clear();

        }

       
        private void Form1_Load(object sender, EventArgs e)
        {
            RegisterHotKey(Handle, 100, 0,Keys.F2 );//mysettingForm.shortcutKey
            this.Visible = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnregisterHotKey(Handle, 100);//
卸载快捷键
        }

        protected override void WndProc(ref Message m)//循环监视Windows消息
        {
            const int WM_HOTKEY = 0x0312;//
按快捷键
            switch (m.Msg)
            {
                case WM_HOTKEY:

                    printprocess();//调用主处理程序
                    break;
            }
            base.WndProc(ref m);
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("
抓屏软件V0.2  By Helios ", "抓屏软件V0.2", MessageBoxButtons.OK);
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.mysettingForm.Visible = true;
            mysettingForm.Show(); 
        }

/////////////////////////////////////settingform.cs/////////////////////////////////////////////////////////////////////

       public string savepath = @"C:/";
        public string savename = "Helios";


        private void OK_Click(object sender, EventArgs e)
        {
            savepath = textBox1.Text;
            savename = textBoxsaveName.Text;
            this.Visible  = false;
        }

        private void view_Click(object sender, EventArgs e)
        {
            DialogResult Result = folderBrowserDialog1.ShowDialog();
            if (Result == DialogResult.OK)
                textBox1.Text = folderBrowserDialog1.SelectedPath;
        }


        private void Default_Click(object sender, EventArgs e)
        {
            savepath = @"C:/";
            savename = "Helios";
            textBoxsaveName.Text = savename;
            textBox1.Text = savepath;
            //this.Visible = false;
        }

        private void Apply_Click(object sender, EventArgs e)
        {
            savepath = textBox1.Text;
            savename = textBoxsaveName.Text;
        }

        private void textBoxsaveName_TextChanged(object sender, EventArgs e)
        {
            this.Apply.Enabled = true;
        }

        private void Setting_Load(object sender, EventArgs e)
        {
            this.Apply.Enabled = false;
        }

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:306794次
    • 积分:4274
    • 等级:
    • 排名:第7078名
    • 原创:79篇
    • 转载:33篇
    • 译文:55篇
    • 评论:103条
    文章分类
    最新评论