使用C#直接截屏的话有两个问题
为了解决这个问题这里就采用了利用nircmd.exe截屏,但是这个工具截屏不能设置截屏后的大小,所以我们在做一下压缩并删除之前文件就行了。
使用nircmd.exe
截屏
官网:http://www.nirsoft.net/utils/nircmd.html
链接:https://pan.baidu.com/s/1AyGNHN5XM5v08gjGx3y6Dw 提取码:sytu
nircmd.exe savescreenshot "d:\temp.png"
这种方法其实各种语言都可以使用。
首先在项目下放入下载的exe路径:Tools/nircmd.exe
项目右键=>属性=>生成事件=>生成前事件命令行 添加如下
xcopy /Y /i /e $(ProjectDir)\Tools $(TargetDir)\Tools
目的是为了打包运行时能够找到 nircmd.exe
工具类
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace SchoolClient.Utils
{
internal class ZScreenUtil2
{
public static bool CaptureScreenSave(string filePath)
{
int lastindex = filePath.LastIndexOf(Path.DirectorySeparatorChar);
if (lastindex < filePath.Length)
{
string fileFolder = filePath.Substring(0, lastindex);
string filename = filePath.Substring(lastindex + 1);
string tempFilePath = fileFolder + Path.DirectorySeparatorChar + "_temp_" + filename;
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string toolsPath = System.IO.Path.Combine(basePath, "Tools");
Process mps = new Process();
ProcessStartInfo mpsi = new ProcessStartInfo("nircmd.exe", "savescreenshot \"" + tempFilePath+"\"" )
{
WorkingDirectory = toolsPath,
UseShellExecute = true,
RedirectStandardInput = false,
RedirectStandardOutput = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
mps.StartInfo = mpsi;
mps.Start();
mps.WaitForExit();
mps.Close();
bool result = GetPicThumbnail(tempFilePath, filePath, 1920, 1080, 80, true);
return result;
}
else
{
return false;
}
}
/// 无损压缩图片
/// <param name="sFile">原图片</param>
/// <param name="dFile">压缩后保存位置</param>
/// <param name="dWidth"></param>
/// <param name="dHeight">高度</param>
/// <param name="ratio">压缩质量(数字越小压缩率越高) 1-100</param>
/// <returns></returns>
public static bool GetPicThumbnail(string sFile, string dFile, int dWidth, int dHeight, int ratio, bool deleteSFile)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
//按比例缩放
Size tem_size = new Size(iSource.Width, iSource.Height);
int sW;
int sH;
if (tem_size.Width > dHeight || tem_size.Width > dWidth)
{
if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
{
sW = dWidth;
sH = (dWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = dHeight;
sW = (tem_size.Width * dHeight) / tem_size.Height;
}
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
}
Bitmap ob = new Bitmap(dWidth, dHeight);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(
iSource,
new Rectangle((dWidth - sW) / 2,(dHeight - sH) / 2, sW, sH),
0,
0,
iSource.Width,
iSource.Height,
GraphicsUnit.Pixel
);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = ratio;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
if (jpegICIinfo != null)
{
ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
}
else
{
ob.Save(dFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
if (deleteSFile)
{
FileInfo di = new FileInfo(sFile);
if (di.Exists)
{
try
{
di.Delete();
}
catch (Exception)
{
}
}
}
}
}
}
}
注意在压缩图片前一定要调用
mps.WaitForExit();
mps.Close();
这样才会在生成图片后进行压缩。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有