I have an application in which I am loading an image and drawing a small rectangle on the image. Further rectangle is moved to select speific area and zoom that area in other picture box.
My problem is when I move the rectangle the movement is very slow. It may be due to unnecessary drawing or rendering the image. Below is the sample code to draw image and rectangle:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.InterpolationMode = interMode;
if (image != null)
{
Rectangle srcRect;
Point pt = new Point((int)(hScrollBar1.Value / zoom),
(int)(vScrollBar1.Value / zoom));
if (canvasSize.Width * zoom < viewRectWidth &&
canvasSize.Height * zoom < viewRectHeight)
{
srcRect = new Rectangle(0, 0, canvasSize.Width, canvasSize.Height);
}
else
{
srcRect = new Rectangle(pt, new Size((int)(viewRectWidth / zoom),
(int)(viewRectHeight / zoom)));
}
g.DrawImage(image, srcRect, srcRect, GraphicsUnit.Pixel);
}
}
public Selection Selection
{
get { return selection; }
set { selection = value; Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (selection != null)
selection.Draw(e.Graphics);
}
public void Draw(Graphics g)
{
Pen p = new Pen(lineColor, lineWidth);
g.DrawRectangle(p, new Rectangle(location, size));
p.Dispose();
}
private void YLScsPanel1_MouseDown(object sender, MouseEventArgs e)
{
Point pt = new Point(e.X, e.Y);
pt = YLScsPanel1.ConvertControlPointToCanvas(pt);
if (selection.isHitting(pt) && e.Button == MouseButtons.Left && srcBmp != null)
{
isHit = true;
YLScsPanel1.Cursor = Cursors.Hand;
}
}
private void YLScsPanel1_MouseMove(object sender, MouseEventArgs e)
{
if (isHit)
{
YLScsPanel1.Cursor = Cursors.Hand;
Point pt = new Point(e.X, e.Y);
pt = YLScsPanel1.ConvertControlPointToCanvas(pt);
newLocation.X = Math.Max(0, Math.Min(pt.X, srcBmp.Width - newSize.Width));
newLocation.Y = Math.Max(0, Math.Min(pt.Y, srcBmp.Height - newSize.Height));
selection.Location = newLocation;
YLScsPanel1.Selection = selection;
}
}
private void YLScsPanel1_MouseUp(object sender, MouseEventArgs e)
{
if (isHit)
{
isHit = false;
YLScsPanel1.Cursor = Cursors.Default;
showPanel2();
}
}
Any suggestion is welcome.
Thanks in advance.