The idea is that im drawing a rectangle over the pictureBox1 and then when i move the mouse wheel up/down it will resize like zoom in/out the area of the image in the pictureBox1 that i drawed the rectangle.
So far i did that i can draw a rectangle. And i did that i can zoom in/out the image in the pictureBox1 but i couldn't find how to make that it will zoom in/out only the part of the rectangle and not the whole image in the pictureBox1.
This is the code.
At the top of the form i did:
double increment = 1.25; Image img; Rectangle mRect; private double factor = 1; private double currentfactor;
Then in the constructor i did:
pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel); pictureBox1.MouseHover += new EventHandler(pictureBox1_MouseHover); this.DoubleBuffered = true; bitmapwithclouds = new Bitmap(Form1PictureBox1Image); currentfactor = factor; pictureBox1.Image = ResizeImage(bitmapwithclouds, new Size((int)(bitmapwithclouds.Width * factor), (int)(bitmapwithclouds.Height * factor))); img = new Bitmap(pictureBox1.Image);
Then the ResizeImage method:
private Image ResizeImage(Image img, Size size) { return new Bitmap(img, size); }
pictureBox1 paint event:
private void pictureBox1_Paint(object sender, PaintEventArgs e) { DrawRectangle(e.Graphics); }
DrawRectangle method:
private void DrawRectangle(Graphics e) { using (Pen pen = new Pen(Color.Red, 2)) { e.DrawRectangle(pen, mRect); } }
pictureBox1 mouse down event:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { mRect = new Rectangle(e.X, e.Y, 0, 0); pictureBox1.Invalidate(); }
pictureBox1 mouse move event:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top); pictureBox1.Invalidate(); } }
picutreBox1 mousewheel event:
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { Image im = new Bitmap(mRect.Width, mRect.Height); CalculateNewSizeFactor(e.Delta); if (pictureBox1.Image != null) pictureBox1.Image.Dispose(); pictureBox1.Image = null; pictureBox1.Image = ResizeImage(im, new Size((int)(im.Width * currentfactor), (int)(im.Height * currentfactor))); }
pictureBox1 mouse hover event:
void pictureBox1_MouseHover(object sender, EventArgs e) { pictureBox1.Focus(); }
And the method calculate new size factor:
private void CalculateNewSizeFactor(int delta) { if (delta > 0 && factor < 2.5) { factor *= increment; currentfactor = factor; } else if (delta < 0 && factor > 0.25) { factor /= increment; currentfactor = factor; } }
Now what i changed few mintues ago was in the mouse wheel event:
I added this line:
Image im = new Bitmap(mRect.Width, mRect.Height);
And replaced it with the variable img in this line:
pictureBox1.Image = ResizeImage(im, new Size((int)(im.Width * currentfactor), (int)(im.Height * currentfactor)));
And its not working.
But if i will put instead the variable (im) the variable img i will be able to zoom in out the whole image in the pictureBox.
But since i want to zoom in out only the part of the drawed rectangle i tried to make this changes but it dosent work at all. Show me whie and dosent zoom in out at all.