83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ScanToTextProgram
|
|
{
|
|
public class ScreenCapture
|
|
{
|
|
|
|
public Bitmap CaptureScreen(Rectangle area)
|
|
{
|
|
// 화면 캡처를 위한 Bitmap 생성
|
|
Bitmap screenshot = new Bitmap(area.Width, area.Height);
|
|
|
|
using (Graphics g = Graphics.FromImage(screenshot))
|
|
{
|
|
// 스크린샷을 찍을 영역을 지정
|
|
g.CopyFromScreen(area.Location, Point.Empty, area.Size);
|
|
}
|
|
|
|
return screenshot;
|
|
}
|
|
|
|
// 사용자로부터 영역을 선택받는 기능
|
|
public Rectangle SelectArea()
|
|
{
|
|
// 마우스로 영역을 선택하도록 하는 Form을 표시
|
|
using (var form = new Form())
|
|
{
|
|
form.FormBorderStyle = FormBorderStyle.None;
|
|
form.BackColor = Color.Black;
|
|
form.Opacity = 0.3;
|
|
form.StartPosition = FormStartPosition.Manual;
|
|
form.Location = new Point(0, 0);
|
|
form.Size = Screen.PrimaryScreen.Bounds.Size;
|
|
|
|
var selectionRectangle = new Rectangle();
|
|
var mouseDown = false;
|
|
var startPoint = Point.Empty;
|
|
|
|
form.MouseDown += (sender, e) =>
|
|
{
|
|
mouseDown = true;
|
|
startPoint = e.Location;
|
|
};
|
|
|
|
form.MouseMove += (sender, e) =>
|
|
{
|
|
if (mouseDown)
|
|
{
|
|
selectionRectangle = new Rectangle(
|
|
Math.Min(startPoint.X, e.X),
|
|
Math.Min(startPoint.Y, e.Y),
|
|
Math.Abs(startPoint.X - e.X),
|
|
Math.Abs(startPoint.Y - e.Y)
|
|
);
|
|
form.Invalidate();
|
|
}
|
|
};
|
|
|
|
form.MouseUp += (sender, e) =>
|
|
{
|
|
mouseDown = false;
|
|
form.Close();
|
|
};
|
|
|
|
form.Paint += (sender, e) =>
|
|
{
|
|
e.Graphics.DrawRectangle(Pens.Red, selectionRectangle);
|
|
};
|
|
|
|
form.ShowDialog();
|
|
|
|
return selectionRectangle;
|
|
}
|
|
}
|
|
}
|
|
}
|