问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

请问c#截屏的最快方法?请大神指点,Copyfromscreen有点慢,希望有更快的方法

发布网友 发布时间:2023-07-14 01:32

我来回答

1个回答

热心网友 时间:2023-10-02 07:43

下面的代码可以设置放置路径 根据鼠标的点击来截图 快捷键shift+f
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace GetScreen
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
private int _X, _Y;
[StructLayout(LayoutKind.Sequential)]
private struct ICONINFO
{
public bool fIcon;
public Int32 xHotspot;
public Int32 yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[StructLayout(LayoutKind.Sequential)]
private struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public Point ptScreenPos;
}
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
private static extern int GetSystemMetrics(int mVal);
[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
private static extern bool GetCursorInfo(ref CURSORINFO cInfo);
[DllImport("user32.dll", EntryPoint = "CopyIcon")]
private static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll", EntryPoint = "GetIconInfo")]
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO iInfo);
[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
);
//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
#endregion

public string path;
public void IniWriteValue(string section, string key, string value)
{
WritePrivateProfileString(section,key,value,path);
}
public string IniReadValue(string section, string key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section,key,"",temp,255,path);
return temp.ToString();
}

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 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;
}
}

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 void button2_Click(object sender, EventArgs e)
{
this.Hide();
}

private void button3_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtSavaPath.Text = folderBrowserDialog1.SelectedPath;
}
}

private void Form1_StyleChanged(object sender, EventArgs e)
{
RegisterHotKey(Handle, 81, KeyModifiers.Shift, Keys.F);
}
public bool flag=true;
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
//注销Id号为81的热键设定
UnregisterHotKey(Handle, 81);
timer1.Stop();
flag = false;
Application.Exit();
}

string MyCursor;
string MyPicPath;
private void Form1_Activated(object sender, EventArgs e)
{
RegisterHotKey(Handle, 81, KeyModifiers.Shift, Keys.F);
path = Application.StartupPath.ToString();
path = path.Substring(0, path.LastIndexOf("\\"));
path = path.Substring(0, path.LastIndexOf("\\"));
path += @"\Setup.ini";
MyCursor = IniReadValue("Setup", "CapMouse");
MyPicPath = IniReadValue("Setup", "Dir");
if (MyCursor == "" || MyPicPath == "")
{
checkBox1.Checked = true;
txtSavaPath.Text = @"D:\";
}
else
{
if (MyCursor == "1")
{
checkBox1.Checked = true;
}
else
{
checkBox1.Checked = false;
}
txtSavaPath.Text = MyPicPath;
}
}

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+"\\IMG_" +
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;
if (MyCursor=="0")
{
bt = CaptureNoCursor();
bt.Save(PicPath);
}
else
{
bt = CaptureDesktop();
bt.Save(PicPath);
}
}

protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 81: //按下的是Shift+Q
getImg();
break;
}
break;
}
base.WndProc(ref m);
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)//双击显示设置窗体
{
this.Show();
}

private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)//单击设置打开设置窗体
{
this.Show();
}

private void timer1_Tick(object sender, EventArgs e)
{
RegisterHotKey(Handle, 81, KeyModifiers.Shift, Keys.F);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
if (flag == true)
{
e.Cancel = true;
}
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}追问你好,我发现你这方法有问题,就是获取鼠标指针的会引起截屏报错:参数无效,重复截屏就会有这问题。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
手机壁纸花卉红色手机壁纸花卉 辽宁智能五芯防水连接器 军工产品浅谈——航空插头 xp电脑系统修复XP系统怎么恢复出厂设置 ...表编辑器后打不开任何文件,无需重装系统,电脑盲都能用 粼基本信息 粼五笔怎样打? 【微科普】想变成光?先搞清楚光是什么吧! 手机被拉黑后打电话是什么提示? 物流管理专业对数学的要求高么 上帝要让一个人灭亡,一定会让他先疯狂,为什么呢? 华夏银行股份有限公司是哪个网贷 LOL这几个英雄在大乱斗中就是BUG,没有卡特你信 英雄联盟虚空之眼很难玩吗? 来安塔山中路519号是什么地方 滁州周边温泉团购价,安徽来安白鹭岛温泉自驾游? 来安景华生态园如何? 女性是因为什么原因身体内的雄性激素会变多,? 雄性激素过多会有什么症状.? wps有填充柄却不能快速填充,下拉只能复制原值,没有自动填充选项,怎么... 跪求all 4 one 的no doubt 的歌词翻译! 翻译篇英文 theable韩国女装贵吗 劳力士水鬼防震程度 第1句it's easy to get around on a bike.第2句it saves money on gas too.小哥这2个句子都用了On, 全自动手表如何转动 我们可以把冰制成一个凸透镜病能点燃纸屑对吗 护士执业证什么时候能拿到? 北京银行是北京光大银行吗 台湾最大的湖泊 降糖药能减肥吗,会不会有副作用? 英国AA网手工皂为什么是透明的 透明皂为什么有粉色绿色蓝色 为什么所有肥皂用指甲刮下来一层都是白色,透明皂也是 为什么有的手工皂是透明的,透明的手工皂好用吗? 剑来李槐一步十三境是在第几章 为什么手机显示SD卡已安全拔出 从宾阳卢圩到黎塘火车站要坐多久的车 从卢圩车站到宾阳汽车站有多远 南宁到宾阳卢圩有多少公里 宾阳办临时身份证在哪里办 谁知道大马帅哥 陈沛光 的近况? 21÷9怎么验算式计算 八十一除九乘六怎样算 349除9乘6怎么算? 请问一部电视剧的名字啊? 312乘以9除6脱式计算? 21×9递等式怎么计算 21÷6用简便方法计算是怎样的? 中石化门店提货券怎么用 氧化镁的应用