标题: C#实现全屏截图 [打印本页]
作者: hongniu 时间: 2015-6-25 16:50
标题: C#实现全屏截图
C#实现全屏截图,虽然没有什么大用吧、但还是记录下吧!
直接代码、没什么好解释的:
using System; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
namespace Screenshot
{
class Program
{
public static int width = 0; // 截图的宽
public static int height = 0; // 截图的高
static void Main(string[] args)
{
Console.WriteLine("3秒后开始全屏截图!");
Thread.Sleep(3000); // 当前线程休眠3S
width = Screen.PrimaryScreen.Bounds.Width; // 获取系统宽
height = Screen.PrimaryScreen.Bounds.Height; // 获取系统高
string strFile = "C:\\" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".jpg"; // 文件保存的路径
ScreenShow(strFile, width, height);
Console.WriteLine("提示:截图保存为" + strFile);
Console.ReadKey(); // 等待用户输入退出
}
/// <summary>
/// 全屏截图
/// </summary>
/// <param name="strFileName">保存路径</param>
/// <param name="nWidth">图片宽</param>
/// <param name="nHeight">图片高</param>
/// <returns></returns>
public static Bitmap ScreenShow(string strFileName, int nWidth, int nHeight)
{
Bitmap btm = new Bitmap(nWidth, nHeight); // 新建一个Bitmap对象
using (Graphics g = Graphics.FromImage(btm))
{
g.CopyFromScreen(0, 0, 0, 0, Screen.AllScreens[0].Bounds.Size); // 获取第0个显示器的大小
g.Dispose();
btm.Save(strFileName, ImageFormat.Jpeg); // 保存
}
return btm;
}
}
}
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |