winfrom 截屏、抓屏 分类: WinForm 2014-08-01 13:02 198人阅读 评论(0) 收藏
截取全屏代码:
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();
}
***** Jackerson *****
I love you not for who you are, but because of who I am when I'm by your side.