111,126
社区成员
发帖
与我相关
我的任务
分享
private void Screenshot() {
Image myImg = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(myImg);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds.Size);
//SendKeys.Send("{PRTSC}");
//Form form = new MyForm();
Form form = new Form();
form.FormBorderStyle = FormBorderStyle.None;
form.BackgroundImage = myImg; // 截取的图片作为Form的背景图片
form.Size = new System.Drawing.Size(myImg.Size.Width, myImg.Size.Height);
form.Paint += new PaintEventHandler(delegate(object sender,PaintEventArgs args){
Graphics gg = args.Graphics;
Image offsetScr = new Bitmap(args.ClipRectangle.Width, args.ClipRectangle.Height);
Graphics srcG = Graphics.FromImage(offsetScr);
Color c = Color.FromArgb(150, 0, 0, 0);
Brush brush = new SolidBrush(c);
srcG.FillRectangle(brush, args.ClipRectangle);
brush.Dispose();
srcG.Dispose();
gg.DrawImage(offsetScr, args.ClipRectangle);
offsetScr.Dispose();
Console.WriteLine("OnPaint");
});
form.KeyDown += new KeyEventHandler(delegate(object sender, KeyEventArgs args)
{
if (args.KeyCode == Keys.Escape)
{
form.Hide();
form.Dispose();
}
else if (args.KeyCode == Keys.S && args.Control) {
SaveFileDialog sd = new SaveFileDialog();
sd.InitialDirectory = ".";
sd.Filter = "PNG图片|*.png";
DialogResult r = sd.ShowDialog();
if (r == DialogResult.OK)
{
myImg.Save(sd.FileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
});
form.Show();
/*
Panel p = new Panel();
p.Dock = DockStyle.Fill;
p.BackColor = Color.FromArgb(150, 0, 0, 0);
p.Paint += new PaintEventHandler(delegate(object sender,PaintEventArgs args){
Console.WriteLine("on paint");
});
form.Controls.Add(p);
*/
}
form.Paint += new PaintEventHandler(delegate(object sender,PaintEventArgs args){
Graphics gg = args.Graphics;
// 这里有一个双缓冲,不用又缓冲效果也是一样的
Image offsetScr = new Bitmap(args.ClipRectangle.Width, args.ClipRectangle.Height);
Graphics srcG = Graphics.FromImage(offsetScr);
Color c = Color.FromArgb(150, 0, 0, 0);
Brush brush = new SolidBrush(c);
srcG.FillRectangle(brush, args.ClipRectangle);
brush.Dispose();
srcG.Dispose();
gg.DrawImage(offsetScr, args.ClipRectangle);
offsetScr.Dispose();
Console.WriteLine("OnPaint");
});


private void Screenshot()
{
new MyScreenCapturer().ShowDialog();
}
class MyScreenCapturer : Form
{
Bitmap myImg = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
public MyScreenCapturer()
{
using (Graphics g = Graphics.FromImage(myImg))
{
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);
}
this.FormBorderStyle = FormBorderStyle.None;
this.Size = Screen.PrimaryScreen.Bounds.Size;
}
protected override void OnPaintBackground(PaintEventArgs e){/* do nothing */}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImageUnscaled(myImg, Point.Empty);
using (Brush brush = new SolidBrush(Color.FromArgb(150, 0, 0, 0)))
{
Region mask = new Region(this.Bounds); mask.Exclude(this.GetSelection());
e.Graphics.Clip = mask;
e.Graphics.FillRectangle(brush, 0, 0, myImg.Width, myImg.Height);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
this.mouseDown = this.mouseMove = e.Location;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (this.Capture)
{
Rectangle last = GetSelection();
mouseMove = e.Location;
this.Invalidate(Rectangle.Union(last, GetSelection()));
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
Rectangle rect = this.GetSelection();
if (rect.Size != Size.Empty)
{
SaveFileDialog sd = new SaveFileDialog() {Filter = "PNG图片|*.png"};
if (sd.ShowDialog() == DialogResult.OK)
{
myImg.Clone(rect, myImg.PixelFormat).Save(sd.FileName, System.Drawing.Imaging.ImageFormat.Png);
}
this.Close();
}
}
private Rectangle GetSelection()
{
Point location = new Point(Math.Min(mouseDown.X, mouseMove.X), Math.Min(mouseDown.Y, mouseMove.Y));
Size size = new Size(Math.Abs(mouseDown.X - mouseMove.X), Math.Abs(mouseDown.Y - mouseMove.Y));
return new Rectangle(location, size);
}
Point mouseDown, mouseMove;
}