您的位置:首页 > 移动开发 > 微信开发

用C#写屏幕截图小程序

2011-07-31 23:15 288 查看
一 方法

主要是利用Graphics.CopyFromScreen 方法实现屏幕截图。

二 实现

1 建一个WindowsForm程序。Form1

界面如图:



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; }
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: