winform截屏

引自 http://www.cnblogs.com/aland-liu/archive/2011/07/20/Winform.html

已经注册博客好久,一直由于工作原因没有打理。今天在网上看了一个截屏的方法思想,感觉不错。就按照这个思路和网友的代码进行整理编写了一个小工具。第一次发博客不足之处,还请高手们批评指正。

废话就不多说放了,代码如下:

截取全屏代码:

try
            {
                this.Hide();
                Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
                Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
                Graphics g = Graphics.FromImage(bitmap);
                g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);

                System.Threading.Thread.Sleep(50);

                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
                saveFileDialog.Title = "保存文件";
                saveFileDialog.ShowDialog();
                bmpPath = saveFileDialog.FileName;
                if ("" != bmpPath)
                {
                    bitmap.Save(bmpPath, ImageFormat.Bmp);
                }
                bitmap.Dispose();
                this.Show();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("抓图失败!");
                this.Show();
            }

 

frmChildScreen 窗体代码如下:

private void frmChildScreen_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Cross;

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.UpdateStyles();
            originBmp = new Bitmap(this.BackgroundImage);
        }

        private void Catch_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!catchStart)
                {
                    catchStart = true;
                    startPoint = new Point(e.X, e.Y);
                }
            }
        }

        private void Catch_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void Catch_MouseMove(object sender, MouseEventArgs e)
        {
            if (catchStart)
            {
                Bitmap destBmp = (Bitmap)originBmp.Clone();
                Point newPoint = new Point(startPoint.X, startPoint.Y);
                Graphics g = Graphics.FromImage(destBmp);
                Pen p = new Pen(Color.Blue, 1);
                int width = Math.Abs(e.X - startPoint.X), height = Math.Abs(e.Y - startPoint.Y);
                if (e.X < startPoint.X)
                {
                    newPoint.X = e.X;
                }
                if (e.Y < startPoint.Y)
                {
                    newPoint.Y = e.Y;
                }
                catchRect = new Rectangle(newPoint, new Size(width, height));
                g.DrawRectangle(p, catchRect);
                g.Dispose();
                p.Dispose();
                Graphics g1 = this.CreateGraphics();
                g1 = this.CreateGraphics();
                g1.DrawImage(destBmp, new Point(0, 0));
                g1.Dispose();
                destBmp.Dispose();
            }
        }


        private void Catch_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (catchStart)
                {
                    catchStart = false;
                    catchFinish = true;
                }
            }
        }

        private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && catchFinish)
            {
                if (catchRect.Contains(new Point(e.X, e.Y)))
                {
                    Bitmap bitmap = new Bitmap(catchRect.Width, catchRect.Height);
                    Graphics g = Graphics.FromImage(bitmap);
                    g.DrawImage(originBmp, new Rectangle(0, 0, bitmap.Width, bitmap.Height), catchRect, GraphicsUnit.Pixel);

                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
                    saveFileDialog.Title = "保存文件";
                    saveFileDialog.ShowDialog();
                    bmpPath = saveFileDialog.FileName;
                    if ("" != bmpPath)
                    {
                        bitmap.Save(bmpPath, ImageFormat.Bmp);
                    }
                    bitmap.Dispose();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }

 

截取局部屏幕代码如下:

try
            {
                this.Hide();
                Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));

                Thread.Sleep(50);
                frmChildScreen CatchForm = new frmChildScreen();
                Bitmap catchBmp = new Bitmap(bounds.Width, bounds.Height);
                Graphics g = Graphics.FromImage(catchBmp);
                g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));
                CatchForm.BackgroundImage = catchBmp;
                if (CatchForm.ShowDialog() == DialogResult.OK)
                {
                    this.Show();
                }

            }
            catch (System.Exception e)
            {
                MessageBox.Show("抓图失败!");
                this.Show();
            }

 

winform截屏

上一篇:mongodb查询语句


下一篇:[转]在windows环境中使用varnish