Hi,
Have drawn rectangle and zoom the image. As per zoom, the rectangle are redrawn (http://www.daniweb.com/forums/thread296495.html). Redrawn rectangle place slightly down. so i adjusted to fit the point. but the rectangle value varies with different resolutions.
how to adjust the rectangle value with different resolution.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (mybitmap == null)
{
mybitmap = new Bitmap(pictureBox1.Image.Width, pictureBox1.Image.Height);
mybitmap.SetResolution(300, 300);
}
rect = new Rectangle(e.X, e.Y, 0, 0);
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (mybitmap == null)
{
return;
}
using (Pen pen = new Pen(Color.Green, 2))
{
foreach (Rectangle r in rectangles)
{
e.Graphics.DrawRectangle(pen, r);
e.Graphics.DrawString(lab[c].ToString(), new Font(lab[c].ToString(), 8F), new SolidBrush(label1.ForeColor), r);
}
}
}
//sz1 = pic width, sz = form width
public void ZoomIn()
{
double ratio = 0d;
if (sz1.Width >= Convert.ToInt16(sz.Width *2.2))
MessageBox.Show("Max ZoomIn");
else
{
sz1.Width +=50;
sz1.Height +=50;
ratio = Convert.ToDouble(sz1.Width) / Convert.ToDouble(pictureBox1.Width);
pictureBox1.Size = sz1;
//Image rectangle value in Non-Public members increases when pic width increases, so i used this value to adjust the picture location to stay top (this works with different resolution).
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
rect1 = (Rectangle)pInfo.GetValue(pictureBox1, null);
n2 = rect1.Y - n3;
//n3 to hold previous value of image rectangle
n3 = rect1.Y;
Point pf = pictureBox1.Location;
pf.Offset(-25, -n2);
if ((pf.Y < 0))
{
pf.Offset(0, 0);
}
pictureBox1.Location = pf;
//the below code works fine if the resolution is 1152*864
for (c=0; c <= (rectangles.Count-1); c++)
{
rect1.X = Convert.ToInt16((rectangles[c].X) * ratio)
// used n2 to adjust the position
if (sz1.Width < (sz.Width + (50 * 8)))
rect1.Y = Convert.ToInt16((rectangles[c].Y * ratio))+(n2-1);
else if (sz1.Width < (sz.Width+(50*10)))
rect1.Y = Convert.ToInt16((rectangles[c].Y * ratio)) + (n2-2);
else if (sz1.Width < (sz.Width+(50*15)))
rect1.Y = Convert.ToInt16((rectangles[c].Y * ratio)) + (n2-3);
else if (sz1.Width < (sz.Width + (50 * 25)))
rect1.Y = Convert.ToInt16((rectangles[c].Y * ratio)) + (n2-4);
else
rect1.Y = Convert.ToInt16((rectangles[c].Y * ratio)) + (n2 -5);
ratio)+5);
rect1.Width = Convert.ToInt16(rectangles[c].Width * ratio);
rect1.Height = Convert.ToInt16(rectangles[c].Height * ratio);
rectangles.RemoveAt(c);
rectangles.Insert(c, rect1);
}
rect = new Rectangle(0, 0, 0, 0);
pictureBox1.Invalidate();
}
}
How to do this with different resolution.
note: don't know that what resolution will user use.