using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Client
{
public partial class Catch : Form
{
public Catch() { InitializeComponent(); }
#region 用戶變量
private Point DownPoint = Point.Empty;
private bool CatchFinished = false;
private bool CatchStart = false;
private Bitmap originBmp;
private Rectangle CatchRect;
#endregion
private void Catch_Load(object sender, EventArgs e)
{
// 雙緩衝設定
this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
this.UpdateStyles();
originBmp = new Bitmap(this.BackgroundImage);
}
private void Catch_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
private void Catch_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && !CatchStart)
{
CatchStart = true;
DownPoint = new Point(e.X, e.Y);
}
}
private void Catch_MouseMove(object sender, MouseEventArgs e)
{
if (CatchStart)
{
Bitmap destBmp = (Bitmap)originBmp.Clone();
Point newPoint = new Point(DownPoint.X, DownPoint.Y);
Graphics g = Graphics.FromImage(destBmp);
Pen p = new Pen(Color.Blue, 1);
int width = Math.Abs(e.X - DownPoint.X);
int height = Math.Abs(e.Y - DownPoint.Y);
if (e.X < DownPoint.X) newPoint.X = e.X;
if (e.Y < DownPoint.Y) newPoint.Y = e.Y;
CatchRect = new Rectangle(newPoint, new Size(width, height));
g.DrawRectangle(p, CatchRect);
g.Dispose(); p.Dispose();
Graphics g1 = this.CreateGraphics();
g1.DrawImage(destBmp, new Point(0, 0));
g1.Dispose();
destBmp.Dispose();
}
}
private void Catch_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && CatchStart)
{
CatchStart = false;
CatchFinished = true;
}
}
private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && CatchFinished &&
CatchRect.Contains(new Point(e.X, e.Y)))
{
Bitmap caughtBmp = new Bitmap(CatchRect.Width, CatchRect.Height);
Graphics g = Graphics.FromImage(caughtBmp);
g.DrawImage(originBmp,
new Rectangle(0, 0, CatchRect.Width, CatchRect.Height),
CatchRect, GraphicsUnit.Pixel);
Clipboard.SetImage(caughtBmp);
g.Dispose();
CatchFinished = false;
this.BackgroundImage = originBmp;
caughtBmp.Dispose();
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
}
originBmp:永不修改的原始全螢幕截圖Clone() → 畫矩形 → DrawImage 整體呈現DownPoint:滑鼠按下起點e.X, e.Y:滑鼠當前位置Rectangle
g.DrawImage(originBmp,
new Rectangle(0, 0, CatchRect.Width, CatchRect.Height), // 目標矩形
CatchRect, // 來源矩形
GraphicsUnit.Pixel);
從 originBmp 的 CatchRect 區域,按像素單位繪製到新建立的等大 Bitmap。
Graphics、Pen、Bitmap 用完即 Dispose()destBmp 每幀建立每幀釋放,防止記憶體洩漏caughtBmp 存入剪貼簿後釋放(剪貼簿內部複製)CatchFinished 確保已完成選取CatchRect.Contains() 確保雙擊在選區內