当前位置:我的异常网» C# » ,c#制作屏幕截图
,c#制作屏幕截图
www.myexceptions.net 网友分享于:2014-12-22 浏览:0次
求助,c#制作屏幕截图
当抓取屏幕的时候,同时抓取鼠标,并将鼠标显示在屏幕截图里,该如何实现呢?
------解决思路----------------------
http://www.cnblogs.com/lujin49/p/3630509.html
------解决思路----------------------
C#如何屏幕截图
//抓取带鼠标的桌面
private Bitmap CaptureDesktop()
{
try
{
int _CX = 0, _CY = 0;
Bitmap _Source = new Bitmap(GetSystemMetrics(0), GetSystemMetrics(1));
using (Graphics g = Graphics.FromImage(_Source))
{
g.CopyFromScreen(0, 0, 0, 0, _Source.Size);
g.DrawImage(CaptureCursor(ref _CX, ref _CY), _CX, _CY);
g.Dispose();
}
_X = (800 - _Source.Width) / 2;
_Y = (600 - _Source.Height) / 2;
return _Source;
}
catch
{
return null;
}
}//codego.net/tags/1/1/
//抓取没有鼠标的桌面
private Bitmap CaptureNoCursor()
{
Bitmap _Source = new Bitmap(GetSystemMetrics(0), GetSystemMetrics(1));
using (Graphics g = Graphics.FromImage(_Source))
{
g.CopyFromScreen(0, 0, 0, 0, _Source.Size);
g.Dispose();
}
return _Source;
}
//获取鼠标样式和坐标
private Bitmap CaptureCursor(ref int _CX, ref int _CY)
{
IntPtr _Icon;
CURSORINFO _CursorInfo = new CURSORINFO();
ICONINFO _IconInfo;
_CursorInfo.cbSize = Marshal.SizeOf(_CursorInfo);
if (GetCursorInfo(ref _CursorInfo))
{
if (_CursorInfo.flags == 0x00000001)
{
_Icon = CopyIcon(_CursorInfo.hCursor);
if (GetIconInfo(_Icon, out _IconInfo))
{
_CX = _CursorInfo.ptScreenPos.X - _IconInfo.xHotspot;
_CY = _CursorInfo.ptScreenPos.Y - _IconInfo.yHotspot;
return Icon.FromHandle(_Icon).ToBitmap();
}
}
}
return null;
}
//保存截图样式
string Cursor;
string PicPath;
private void button1_Click(object sender, EventArgs e)
{
try
{
path = Application.StartupPath.ToString();
path = path.Substring(0, path.LastIndexOf("\\"));
path = path.Substring(0, path.LastIndexOf("\\"));
path += @"\Setup.ini";
if (checkBox1.Checked == true)
{
Cursor = "1";
}
else
{
Cursor = "0";
}
if (txtSavaPath.Text == "")
{
PicPath = @"D:\";
}
else
{
PicPath = txtSavaPath.Text.Trim();
}
IniWriteValue("Setup", "CapMouse", Cursor);
IniWriteValue("Setup", "Dir", PicPath);
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
------解决思路----------------------
private Bitmap CaptureScreen()
{
Bitmap _Source = new Bitmap(GetSystemMetrics(0), GetSystemMetrics(1));
using (Graphics g = Graphics.FromImage(_Source))
{
g.CopyFromScreen(0, 0, 0, 0, _Source.Size);
}
return _Source;
}
//引入命名空间、和相关的API函数
using System.Runtime.InteropServices;
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
private static extern int GetSystemMetrics(int mVal);
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retval,int size,string filePath);
#region 定义快捷键
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
//抓取屏幕处理
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312; //定义一个操作系统的消息号
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 81: //若按下的是组合键
getImg();//则调用“屏幕抓图”方法
break;
}
break;
}
base.WndProc(ref m);//执行基类的WndProc方法
}//codego.net/tags/1/1/
//用于抓取屏幕图像方法
private void getImg()
{
DirectoryInfo di = new DirectoryInfo(MyPicPath);
if (!di.Exists)
{
Directory.CreateDirectory(MyPicPath);
}
if (MyPicPath.Length == 3)
MyPicPath = MyPicPath.Remove(MyPicPath.LastIndexOf(":") + 1);//移除冒号后面的"\",只保留“盘符:”
//图片的保存路径
string PicPath = MyPicPath + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".bmp";
Bitmap bt = CaptureScreen();//获取屏幕图像,是一个位图实例
bt.Save(PicPath);//把图片保存到指定位置
}
//存放截图
private Bitmap CaptureScreen()//抓取桌面
{
Bitmap _Source = new Bitmap(GetSystemMetrics(0), GetSystemMetrics(1));
using (Graphics g = Graphics.FromImage(_Source))
{
g.CopyFromScreen(0, 0, 0, 0, _Source.Size);
}
return _Source;
}
------解决思路----------------------
截屏(带鼠标)
这份代码完整可运行的。
里面的代码应该都不用注释吧,从名字就能看出是什么功能。至于不懂得还是得多百度好。
文章评论