关于Form.Paint事件绘制缓慢问题
--------------------编程问答-------------------- 你在什么时候触发的OnPaint事件,或许跟你的触发条件有关系,如果矩形是逐步形成的,那肯定会多次调用OnPaint,你看看ClipRectangle这个东西是怎样的 --------------------编程问答-------------------- java代码书写方式弄到c#里看着特别。args.ClipRectangle.Width, args.ClipRectangle.Height 应该这个地方有问题。
--------------------编程问答-------------------- 问题已得到解决,谢谢大家.
是没有双缓冲导致的,Form.DoubleBuffered = true 就没问题了 --------------------编程问答--------------------
--------------------编程问答--------------------
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;
}
留个痕迹 --------------------编程问答--------------------
同样有问题
补充:.NET技术 , C#