Winform- 屏幕截屏
使用WINFORM来进行屏幕截图,试试这一串
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private int intStartX = 0;
private int intStartY = 0;
private bool isMouseDraw = false;
private Rectangle rectangle;
public Form1()
{
//this.BackColor = Color.Gray;
//this.Opacity = 0.5;
//this.FormBorderStyle = FormBorderStyle.None;
//this.WindowState = FormWindowState.Maximized;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap myImage = new Bitmap(rectangle.Width, rectangle.Height);
Graphics g = Graphics.FromImage(myImage);
//g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
//g.CopyFromScreen(rectangle.X,rectangle.Y,(rectangle.X + rectangle.Width),
//(rectangle.Y + rectangle.Height), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
g.CopyFromScreen(new Point(rectangle.X, rectangle.X), new Point(rectangle.X, rectangle.Y), new Size(rectangle.Width, rectangle.Height));
myImage.Save(@"D:\x.jpg");
g.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDraw = true;
intStartX = e.X;
intStartY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDraw)
{
try
{
Graphics g = this.CreateGraphics();
//清空上次画下的痕迹
g.Clear(this.BackColor);
Brush brush = new SolidBrush(Color.Red);
Pen pen = new Pen(brush, 1);
pen.DashStyle = DashStyle.Solid;
rectangle = new Rectangle(intStartX > e.X ? e.X : intStartX, intStartY > e.Y ? e.Y : intStartY, Math.Abs(e.X - intStartX), Math.Abs(e.Y - intStartY));
g.DrawRectangle(pen, rectangle);
g.Dispose();
}
catch (Exception ex)
{
ex.ToString();
}
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDraw = false;
intStartX = 0;
intStartY = 0;
}
}
}