I paint a drawing on a drawing area.
In order to optimize the performance of that drawing I decided to redraw only inside the really necessary region(clipping region). See the picture.
The problem is that I don't arrive to build a method(GetBitmapFromRectangle
) that returns me the result of intersection of my paths with the clipping rectangle.
This method could be very useful when the user moves(with the mouse) paths. In that case there is need to repaint only the former and current moved path area - and not the whole picture, that in case of complex pictures can visibly slow down the application performance.
My tests shows that the calculation time is much less important that the drawing one, so I'd better perform more calculations that more drawing.
I want to draw not the entire paths that intersects the rectangle, but really only the paths inside the clipping region.
In other words, I need a method that will make the BLUE lines inside the red area PINK.
Here is the code:
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplication38
{
public partial class Form1 : Form
{
Point center;
int radius, penWidth;
public Form1()
{
InitializeComponent();
center = new Point(50, 50);
radius = 100;
penWidth = 5;
this.ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.Clear(this.BackColor);
e.Graphics.DrawImage(
GetBitmapFromRectangle(e.ClipRectangle),
e.ClipRectangle.Location);
//
// INITIAL DRAWING METHOD
//
Pen p = new Pen(Color.Blue, penWidth);
// draw O
e.Graphics.DrawEllipse(p, center.X - radius, center.Y - radius, radius * 2, radius * 2);
// draw X
Point[] line1 = new Point[] { new Point(0, 0), new Point(this.Width, this.Height) };
Point[] line2 = new Point[] { new Point(this.Width, 0), new Point(0, this.Height) };
e.Graphics.DrawLines(p, line1);
e.Graphics.DrawLines(p, line2);
p.Dispose();
}
private Bitmap GetBitmapFromRectangle(Rectangle rect)
{
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage(bmp);
if (rect != this.DisplayRectangle)
g.Clear(Color.Red);
else
g.Clear(this.BackColor);
// Draw ONLY! the intersetion between drawing and rectangle...
// How to ???
return bmp;
}
}
}
Nota bene
The example is a sample for demo purpose only. In the real project I have very complex graphic, and the drawing time is much more expensive that the calculation one. This why I want to not redraw all the painting, but only the lines inside the clipping region!