public partial class frmScreenRoi : Form
{
/// <summary> </summary>
int[] ScreenSize = new int[4] { 0, 0, 0, 0 };
/// <summary> </summary>
int[] ScreenSizePre = new int[4] { 0, 0, 0, 0 };
/// <summary> </summary>
Bitmap BackBit = null;
/// <summary> 刷新线程 </summary>
Thread thdUpdate;
/// <summary> 退出线程 </summary>
bool _isquit = false;
/// <summary> 鼠标按下 </summary>
bool _isMouseDown = false;
/// <summary> 第一次 按下+移动+释放 </summary>
bool _isFirst = true;
#region 构造
public frmScreenRoi()
{
InitializeComponent();
}
private void frmScreenRoi_Load(object sender, EventArgs e)
{
Rectangle r = Screen.PrimaryScreen.Bounds;
BackBit = new Bitmap(r.Width, r.Height);//截取的大小
Graphics g = Graphics.FromImage(BackBit);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), BackBit.Size);
//窗体最大化,及相关处理
this.Width = r.Width;
this.Height = r.Height;
this.Left = 0;
this.Top = 0;
this.TopMost = true;
ScreenSize[2] = r.Width;
ScreenSize[3] = r.Height;
pictureBox1.bitmap = BackBit;
thdUpdate = new Thread(Loop);
thdUpdate.Start();
}
private void frmScreenRoi_FormClosing(object sender, FormClosingEventArgs e)
{
_isquit = true;
Thread.Sleep(200);
thdUpdate?.Abort();
}
#endregion
private void Loop()
{
while (!_isquit)
{
Thread.Sleep(10);
if (_isMouseDown)
{
_isMouseDown = false;
ScreenSizePre[0] = ScreenSize[0];
ScreenSizePre[1] = ScreenSize[1];
this.Invoke((MethodInvoker)delegate {
pictureBox2.Location = new Point(ScreenSizePre[0], ScreenSizePre[1]);
pictureBox2.Visible = true;
});
}
if (ScreenSize[2] > 0 && ScreenSize[3] > 0)
{
if (ScreenSize[2] != ScreenSizePre[2] || ScreenSize[3] != ScreenSizePre[3])
{
ScreenSizePre[2] = ScreenSize[2];
ScreenSizePre[3] = ScreenSize[3];
this.Invoke((MethodInvoker)delegate {
pictureBox2.Size = new Size(ScreenSizePre[2], ScreenSizePre[3]);
Bitmap Bit = BackBit.Clone(new Rectangle(ScreenSizePre[0], ScreenSizePre[1], ScreenSize[2], ScreenSize[3]), BackBit.PixelFormat);
pictureBox2.BackgroundImage = Bit;
});
}
}
else
{
this.Invoke((MethodInvoker)delegate {
pictureBox2.Size = new Size(0, 0);
});
}
}
}
#region 画区域
//鼠标按下
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (_isFirst)
{
_isMouseDown = true;
ScreenSize[0] = e.X;
ScreenSize[1] = e.Y;
//pictureBox2.Location = new Point(e.X, e.Y);
//pictureBox2.Visible = true;
}
}
//鼠标移动
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_isFirst)
{
ScreenSize[2] = e.X - ScreenSize[0];
ScreenSize[3] = e.Y - ScreenSize[1];
//if (ScreenSize[2] > 0 && ScreenSize[3] > 0)
//{
// //SuspendLayout();
// pictureBox2.Size = new Size(ScreenSize[2], ScreenSize[3]);
// Bitmap Bit = BackBit.Clone(new Rectangle(ScreenSize[0], ScreenSize[1], ScreenSize[2], ScreenSize[3]), BackBit.PixelFormat);
// pictureBox2.BackgroundImage = Bit;
// //ResumeLayout(true);
//}
//else
//{
// pictureBox2.Size = new Size(0, 0);
//}
}
}
//鼠标释放
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (_isFirst)
{
ScreenSize[2] = e.X - ScreenSize[0];
ScreenSize[3] = e.Y - ScreenSize[1];
_isFirst = false;
if (ScreenSize[2] > 0 && ScreenSize[3] > 0)
{
try
{
Bitmap Bit = BackBit.Clone(new Rectangle(ScreenSize[0], ScreenSize[1], ScreenSize[2], ScreenSize[3]), BackBit.PixelFormat);
DateTime now = DateTime.Now;
string SavePath = PathTool.DataOutImagePath + now.ToString("yyyy-MM-dd") + "\";
string FileName = "Screen_" + now.ToString("hhmmss_fff") + ".bmp";
if (!Directory.Exists(SavePath))
Directory.CreateDirectory(SavePath);
Bit.Save(SavePath + FileName);
Log.Instance().Enqueue("自定义截图完成,保存路径:" + SavePath + FileName, SagensColor.Black);
}
catch (Exception ex)
{
Log.Instance().Enqueue(ex);
}
}
this.Close();
}
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right) this.Close();
}
private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
this.Close();
}
private void frmScreenRoi_KeyPress(object sender, KeyPressEventArgs e)
{
this.Close();
}
#endregion
}
|