用C#写屏幕截图小程序
2011-07-31 23:15
288 查看
一 方法
主要是利用Graphics.CopyFromScreen 方法实现屏幕截图。
二 实现
1 建一个WindowsForm程序。Form1
界面如图:
![](http://hi.csdn.net/attachment/201107/31/0_13121250182W88.gif)
Form代码:
二 再建一个WindoesFrom窗体GForm,用于实现截图。对窗体的属性作如下设置:
GForm代码:
主要是利用Graphics.CopyFromScreen 方法实现屏幕截图。
二 实现
1 建一个WindowsForm程序。Form1
界面如图:
![](http://hi.csdn.net/attachment/201107/31/0_13121250182W88.gif)
Form代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.IO; namespace PictureCutForm { public partial class Form1 : Form { private string fullFileName; private GForm gForm; public Form1() { InitializeComponent(); } private void btnOpen_Click(object sender, EventArgs e) { try { if (gForm != null) { fullFileName = gForm.FileFullName; Process p = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(fullFileName); p.StartInfo = startInfo; p.Start(); } else { MessageBox.Show("您还没有截图"); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnDelete_Click(object sender, EventArgs e) { try { if (gForm != null) { fullFileName = gForm.FileFullName; FileInfo file = new FileInfo(fullFileName); file.Delete(); MessageBox.Show("删除成功"); } else { MessageBox.Show("您还没有截图!"); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void btnCutPicture_Click(object sender, EventArgs e) { gForm = new GForm(); gForm.Show(); } } }
二 再建一个WindoesFrom窗体GForm,用于实现截图。对窗体的属性作如下设置:
this.Opacity = 0; this.ShowIcon = false; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.ControlBox = false; this.BackColor = System.Drawing.Color.White; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
GForm代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using System.Diagnostics; namespace PictureCutForm { public partial class GForm : Form { private int start_x, start_y; //起始坐标 private int end_x, end_y; //终止坐标 private int width_x, height_y; //长和宽 private Rectangle rect; //矩形框 private Image image; private Graphics g; private string fileName; private string fullFileName; private bool isMouseDown = false; public GForm( ) { InitializeComponent(); fileName=fullFileName; } //鼠标按下事件 private void GForm_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = true; // g = this.CreateGraphics(); start_x = e.X; //按下鼠标时的坐标 start_y = e.Y; } } private void GForm_MouseUp(object sender, MouseEventArgs e) { end_x = e.X; end_y = e.Y; width_x = Math.Abs(end_x - start_x); height_y = Math.Abs(end_y - start_y); rect = new Rectangle(start_x, start_y, width_x, height_y); image=new Bitmap(width_x,height_y); g = Graphics.FromImage(image); // g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(width_x, height_y)); g.CopyFromScreen(new Point(start_x,start_y),new Point(0,0),new Size(width_x,height_y)); fileName = GetFileName(); string path=@"C:\Documents and Settings\Administrator\桌面\"; fullFileName = path + fileName+".jpg"; image.Save(fullFileName,ImageFormat.Jpeg); this.Close(); if (MessageBox.Show("截图成功,是否打开图片", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { Process p = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(fullFileName); p.StartInfo = startInfo; p.Start(); } } /// <summary> /// 获取文件名 /// </summary> /// <returns></returns> public string GetFileName() { string fileName = DateTime.Now.ToString(); fileName= fileName.Replace("/", ""); fileName= fileName.Replace(" ",""); fileName = fileName.Replace(":", ""); fileName=fileName.Replace("\\", ""); Random rand = new Random(); int ran = rand.Next(0, 1000); fileName += ran.ToString(); return fileName; } private void GForm_MouseMove(object sender, MouseEventArgs e) { //如果是鼠标左键并且先曾按下 if (e.Button == MouseButtons.Left && isMouseDown == true) { g = this.CreateGraphics(); Pen p=new Pen (Brushes.Black,2); g.DrawRectangle(p, start_x, start_y, Math.Abs(e.X - start_x), Math.Abs(e.Y - start_y)); //g.Flush(); g.Clear(Color.White); } } private void GForm_Load(object sender, EventArgs e) { this.Opacity = 0.5; this.BackColor = Color.SteelBlue; } public string FileFullName { get { return this.fullFileName; } } } }
相关文章推荐
- C#实现类似qq的屏幕截图程序 [转载]
- C#实现类似qq的屏幕截图程序
- C#实现类似qq的屏幕截图程序
- C#实现类似qq的屏幕截图程序
- C#实现类似qq的屏幕截图程序
- 整理的C#屏幕截图,控件截图程序
- C#实现类似qq的屏幕截图程序收藏
- C#实现类似qq的屏幕截图程序
- C#实现类似qq的屏幕截图程序
- C#实现类似qq的屏幕截图程序
- C#实现类似qq的屏幕截图程序
- C#实现类似qq的屏幕截图程序 转
- C#实现类似QQ的屏幕截图程序 转
- C#实现类似qq的屏幕截图程序 [转载]
- 非API程序模仿QQ截图。初学探讨。C#
- C#屏幕绘制程序
- 浅析如何用C#.NET做屏幕截图软件以及注册全局快捷键(上)
- C# 窗口实现Win7简单屏幕泡泡保护程序 和实现聊天窗口震动
- 运用C#制作屏幕捕捉程序(2)
- C#软件开发实例.私人订制自己的屏幕截图工具(六)添加配置管理功能