写程序中,有时需要利用截屏操作时,而调用QQ或者系统的PrintScreen不方便传值时,需要在程序中做截屏功能,本程序实现这一功能。
效果图:
具体源码:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (!isDoubleClick)
{
begin = true;
firstPoint = new Point(e.X, e.Y);
changePoint(e.X, e.Y);
msg.Visible = true;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (begin)
{
//获取新的右下角坐标
secondPoint = new Point(e.X, e.Y);
int minX = Math.Min(firstPoint.X, secondPoint.X);
int minY = Math.Min(firstPoint.Y, secondPoint.Y);
int maxX = Math.Max(firstPoint.X, secondPoint.X);
int maxY = Math.Max(firstPoint.Y, secondPoint.Y);
//重新画背景图
Image tempimage = new Bitmap(cachImage);
Graphics g = Graphics.FromImage(tempimage);
//画裁剪框
g.DrawRectangle(new Pen(Color.Red), minX, minY, maxX - minX, maxY - minY);
pictureBox1.Image = tempimage;
}
}
/*动态调整显示信息的位置,输入参数为当前截屏鼠标位置*/
public void changePoint(int x, int y)
{
if (x < halfWidth)
{
if (y < halfHeight)
{ msg.Top = halfHeight; msg.Left = halfWidth; }
else
{ msg.Top = 0; msg.Left = halfWidth; }
}
else
{
if (y < halfHeight)
{ msg.Top = halfHeight; msg.Left = 0; }
else
{ msg.Top = 0; msg.Left = 0; }
}
msg.Visible = false;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
begin = false;
isDoubleClick = true; //之后再点击就是双击事件了
}
private void copytoFather(Rectangle r)
{
Image img = new Bitmap(r.Width, r.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(r.Left,r.Top), new Point(0,0), r.Size);
Clipboard.SetImage(img);
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
if (firstPoint != secondPoint)
{
int minX = Math.Min(firstPoint.X, secondPoint.X);
int minY = Math.Min(firstPoint.Y, secondPoint.Y);
int maxX = Math.Max(firstPoint.X, secondPoint.X);
int maxY = Math.Max(firstPoint.Y, secondPoint.Y);
Rectangle r = new Rectangle(minX, minY, maxX - minX, maxY - minY);
copytoFather(r);
}
this.Close();
}
在此基础上,需要再进行过程的调用,则可以灵活实现截屏的功能。
具体资源:源码下载