以利用Graphics类的CopyFromScreen方法来实现屏幕截取,舍弃了比较麻烦的API函数,只要几句代码就能实现了,而且这个这个方法能实现只抓取部分区域的屏幕,可以实现类似qq抓屏的功能。
复制C#代码保存代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Screen scr = Screen.PrimaryScreen;
Rectangle rc = scr.Bounds;
int iWidth = rc.Width;
int iHeight = rc.Height;
Image myImage = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(myImage);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
myImage.Save(@"c:/1.jpeg");
}
}
}
以上内容转自lijgame的blog,在此对作者表示感谢!
原文地址:
http://blog.csdn.net/lijgame/archive/2006/12/18/1447921.aspx 现在就文中的“实现QQ截屏”功能继续研究如下。
1、首先要实现在屏幕上画图,即:可以在屏幕上画出一个矩形框,双击该矩形框将截取矩形框内的屏幕。 目前在屏幕上画图的C#实现代码如下:
复制C#代码保存代码
[DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);
private void button19_Click(object sender, EventArgs e)
{
System.IntPtr DesktopHandle = GetDC(System.IntPtr.Zero);
Graphics g = Graphics.FromHdc(DesktopHandle);
g.DrawRectangle(new Pen(Color.Red), new Rectangle(10, 10, 100, 100));
}
需要用到可恶的API,实在不想用这种方法来实现!
2、研究出QQ屏幕上画图的真相 继续研究QQ截图的功能,发现当点击“截图”按钮之后,屏幕变成静止的了,原先网页上跳动的东西都不动了,哈哈,原因很简单,QQ截取了整个屏幕,然后用这张静止的屏幕覆盖全屏,真是精妙的解决办法啊!
于是用C#实现是:
建一个子窗体ScreenForm,在窗体上放一个pictureBox和一个label控件。ScreenForm代码如下:
复制C#代码保存代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace copyScreen
{
public partial class ScreenForm : Form
{
public ScreenForm()
{
InitializeComponent();
}
public event copyToFatherTextBox copytoFather; public bool begin = false; public bool isDoubleClick = false;
public Point firstPoint = new Point(0, 0); public Point secondPoint = new Point(0, 0); public Image cachImage = null; public int halfWidth = 0; public int halfHeight = 0;
public void copyScreen()
{
Rectangle r = Screen.PrimaryScreen.Bounds;
Image img = new Bitmap(r.Width, r.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), r.Size);
this.Width = r.Width;
this.Height = r.Height;
this.Left = 0;
this.Top = 0;
pictureBox1.Width = r.Width;
pictureBox1.Height = r.Height;
pictureBox1.BackgroundImage = img;
cachImage = img;
halfWidth = r.Width / 2;
halfHeight = r.Height / 2;
this.Cursor = new Cursor(GetType(), "MyCursor.cur");
}
private void ScreenForm_Load(object sender, EventArgs e)
{
copyScreen();
}
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;
msg.Text = "左上角坐标:(" + minX.ToString() + "," + minY.ToString() + ")/r/n";
msg.Text += "右下角坐标:(" + maxX.ToString() + "," + maxY.ToString() + ")/r/n";
msg.Text += "截图大小:" + (maxX - minX) + "×" + (maxY - minY) + "/r/n";
msg.Text += "双击任意地方结束截屏!";
changePoint((minX + maxX) / 2, (minY + maxY) / 2);
}
}
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; }
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
begin = false;
isDoubleClick = true; }
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();
}
}
}
界面如下:
本程序下载:CODE
http://files.cnblogs.com/tuyile006/copyscreensource.rar 演示版:Demo
http://files.cnblogs.com/tuyile006/copyscreendemo.rar
分享到:
相关推荐
C#模拟QQ截屏功能
京华志 源码多【www.jinghuazhi.com】C#模拟QQ截屏功能
用C#写的模拟qq截屏功能案例 案例软件运行中点击开始截屏后鼠标选中区域 然后再该区域中双击 即可获得截图 另附 屏幕句柄绘图 在整个屏幕上绘图的功能
c#写的模拟QQ截图功能,欢迎使用并给出宝贵意见!
本程序是仿照QQ的截图程序进行仿照的,截图选取选定后可以进行图形绘制,比如添加文字说明,绘制矩形,绘制椭圆,画笔,按完成截图按钮则将主窗体的背景设置为截图的图片。本程序截图在512MB内存,P4 2.0以上的机器...
C#模拟QQ截图功能,附带有图片灰色处理。。代码有些凌乱。。仅为初学者提供思路。
一款模拟qq2006截屏的小工具,第一次做,用的是c#,个人感觉还不错,起码截取的时候能把框框弄出来,哈哈
一个C#程序的全项目与源代码,图形开发的一个实例本地截图工具
实现的功能如下: 1、 彩色光标,在.Net中实现光标嵌入资源文件需要变换一下思路。 2、可以抓到悬浮窗等LayeredWindow。 3、矩形选择,并可调整矩形的大小和位置,限定选择域不超出屏幕。已有选择的情况下,仍...
完全模拟MSNShell,QQ截图方式,C#完成,代码简洁易懂,是学习的好材料
模拟鼠标划选的粗代码,有模拟QQ截图功能
模拟QQ截图功能。c#语言,vs2008。(对网友的代码改善了下,对原作者表示感谢)
用C#开发截屏实例,类似QQ,优点占用内存、CPU小到极点。 开发环境:WinXP+Visual Studio 2005
模拟按键精灵,可以自由的编辑命令 控制鼠标键盘,可以通过自动截图来校验页面
该项目的开发环境为VS2010,由C#开发的winform程序。实现了截图功能,点击截图按钮后就可以截图了。按下左键开始,拖动确定范围,弹起后双击左键会弹出保存对话框,选定路径保存就好了。
这就不解系了,是人都知道QQ有这个功能,当然小编怎么可以放过。 10.最主要的当然是QQ好友列表啦,网上找了很多仿QQ列表组件,看效果都特别特别差,于是小编自己写了好友列表,高仿度足以以假乱真,采用双向加载更新...
c#实现快速截屏,模拟qq截屏,实现任意截屏,具有快捷键设置功能和开机启动功能
c#模拟QQ截图的工具,很实用
启动QQ自带的截图功能,从屏幕左上角开始截图,将光标拖动到群成员搜索框内,记下此时截图工具显示的截图像素尺寸X*Y(也即点击点屏幕坐标),将其填入程序设置中的“QQ群成员搜索框坐标”中。 然后打卡与任一群...