黑马程序员技术交流社区
标题:
如何实时截取桌面信息
[打印本页]
作者:
徐艳勇
时间:
2012-10-16 10:04
标题:
如何实时截取桌面信息
如何实时截取桌面信息,并生成数据流
作者:
李辰东
时间:
2012-10-16 13:45
截取桌面图像,调用Graphics的CopyFromScreen方法可以实现
操作代码:
Graphics gSrc = this.CreateGraphics(); //创建窗体的Graphics对象
HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc());
int width = this.Width-SystemInformation.FrameBorderSize.Width; //获取宽度
int height = this.Height-SystemInformation.FrameBorderSize.Height; //获取高度
const int SRCCOPY = 0xcc0020; //复制图块的光栅操作码
Bitmap bmSave = new Bitmap(width, height); //用于保存图片的位图对象
Graphics gSave = Graphics.FromImage(bmSave); //创建该位图的Graphics对象
HandleRef hDcSave = new HandleRef(null, gSave.GetHdc()); //得到句柄
BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY);
如此操作,当前窗体的图像即被保存之bmSave中。
详细的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ScreenShot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnScreen_Click(object sender, EventArgs e)
{
int screenWidth = System.Windows.Forms.SystemInformation.VirtualScreen.Width; //屏幕宽度
int screenHeight = System.Windows.Forms.SystemInformation.VirtualScreen.Height; //屏幕高度
Bitmap bmSave = new Bitmap(screenWidth, screenHeight);
Graphics g = Graphics.FromImage(bmSave);
g.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy);
g.Dispose();
SaveFile(bmSave);
}
private void btnWindow_Click(object sender, EventArgs e)
{
Graphics gSrc = this.CreateGraphics(); //创建窗体的Graphics对象
HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc());
int width = this.Width-SystemInformation.FrameBorderSize.Width; //获取宽度
int height = this.Height-SystemInformation.FrameBorderSize.Height; //获取高度
const int SRCCOPY = 0xcc0020; //复制图块的光栅操作码
Bitmap bmSave = new Bitmap(width, height); //用于保存图片的位图对象
Graphics gSave = Graphics.FromImage(bmSave); //创建该位图的Graphics对象
HandleRef hDcSave = new HandleRef(null, gSave.GetHdc()); //得到句柄
BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY);
gSrc.ReleaseHdc();
gSave.ReleaseHdc();
gSrc.Dispose();
gSave.Dispose();
SaveFile(bmSave);
}
private void SaveFile(Bitmap bmSave)
{
if (DialogResult.OK == saveFileDialog1.ShowDialog())
{
string fileName = saveFileDialog1.FileName;
if (1 == saveFileDialog1.FilterIndex)
{
if (!fileName.EndsWith(".bmp"))
{
fileName += ".bmp";
}
}
else if (2 == saveFileDialog1.FilterIndex)
{
if (!fileName.EndsWith(".jpg"))
{
fileName += ".jpg";
}
}
else if (3 == saveFileDialog1.FilterIndex)
{
if (!fileName.EndsWith(".png"))
{
fileName += ".png";
}
}
Save(bmSave, fileName);
}
}
private void Save(Bitmap bm,string fileName)
{
if (fileName.EndsWith(".bmp"))
{
bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
}
else if (fileName.EndsWith(".jpg"))
{
bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else if (fileName.EndsWith(".png"))
{
bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
bm.Dispose();
}
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2