- 创建一个窗体,如
Form4.cs
,用于放置全屏下的截图,默认最大化,具体代码如下
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WindowsFormsApp2.lib;
namespace WindowsFormsApp2
{
public partial class Form4 : Form
{
public Image returnImg = null;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
Text = "屏幕区域";
Hide();
var bmp = GetScreenFullImage();
BackgroundImage = bmp;
Show();
var f5 = new Form5(bmp);
var res = f5.ShowDialog();
if (res == DialogResult.OK)
{
DialogResult = DialogResult.OK;
returnImg = f5.returnImg;
}
}
public static 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();
}
public static Image Open(string fileName)
{
Image originalImg = Image.FromFile(fileName);
return originalImg;
}
private Bitmap GetScreenFullImage(int left = 0, int top = 0, int width = 0, int height = 0)
{
int screenWidth = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;
if (width <= 0)
{
width = screenWidth;
}
if (height <= 0)
{
height = screenHeight;
}
Bitmap bmSave = new Bitmap(screenWidth, screenHeight);
Graphics g = Graphics.FromImage(bmSave);
g.CopyFromScreen(left, top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
g.Dispose();
return bmSave;
}
private Bitmap GetCurrentWindowImage()
{
Graphics gSrc = this.CreateGraphics();
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);
HandleRef hDcSave = new HandleRef(null, gSave.GetHdc());
DllCommon.BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY);
gSrc.ReleaseHdc();
gSave.ReleaseHdc();
gSrc.Dispose();
gSave.Dispose();
return bmSave;
}
}
}
- 还有,再新建一个
DllCommon.cs
引用,这是一个调用Windows系统的dll库,代码如下,
using System.Runtime.InteropServices;
namespace WindowsFormsApp2.lib
{
public class DllCommon
{
[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);
}
}
- 在创建一个窗体如
Form5.cs
,用作对话框,截图部分区域,换成图片返回
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form5 : Form
{
public Image returnImg = null;
private Bitmap bmp = null;
public Form5(Bitmap bmp)
{
this.bmp = bmp;
InitializeComponent();
}
private void Form5_Load(object sender, EventArgs e)
{
button1.Text = "确认截取";
button1.BackColor = Color.White;
getScreenRectImage();
}
private void Form4_Move(object sender, EventArgs e)
{
getScreenRectImage();
}
private void getScreenRectImage()
{
Text = String.Format(@"截取区域:Width:{2}, Height:{3}, 坐标:X:{0}, Y:{1}", Left, Top, Width, Height);
Bitmap partImg = GetPart(bmp, 0, 0, Width, Height, Left, Top);
pictureBox1.Image = partImg;
}
private void Form4_Resize(object sender, EventArgs e)
{
getScreenRectImage();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
returnImg = pictureBox1.Image;
Close();
}
private static Bitmap GetPart(Bitmap originalImg, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
{
Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
Graphics graphics = Graphics.FromImage(partImg);
Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));
Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));
graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
return partImg;
}
}
}
- 记录到此,一个截屏工具就这样完成了,调用它的代码如下所示
private void OpenScreenshotsDialog()
{
var f4 = new Form4();
var res = f4.ShowDialog();
if (res == DialogResult.OK && f4.returnImg!= null)
{
rectImg = f4.returnImg;
var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "screen.png");
Form4.Save(new Bitmap(rectImg), fileName);
}
}
- 最后,附上运行效果图
