This is the code in the paint event of pictureBox1. I need to find the location of the variable mImage.
if (null != mImage)
{
e.Graphics.DrawImage(mImage, theLocationOfImage);
}
mImage is Image type. Instead theLocationOfImage I need to put the mImage location. This is how I got the mImage:
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.Image);
Bitmap bmp1 = GetPartOfImageInRect(bmp, mRect);
CalculateNewSizeFactor(e.Delta);
Image img1 = ResizeImage(bmp1,
new Size((int)(bmp1.Width * currentfactor),
(int)(bmp1.Height * currentfactor)));
mImage = img1;
pictureBox1.Invalidate();
}
mRect is a rectangle I draw over the pictureBox1.
EDIT
This is how i draw the rectangle:
private void DrawRectangle(Graphics e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.DrawRectangle(pen, mRect);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mRect = new Rectangle(e.X, e.Y, 0, 0);
pictureBox1.Invalidate();
}
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();
}
}
This is the resize image method:
private Image ResizeImage(Image img, Size size)
{
return new Bitmap(img, size);
}
And this is the pictureBox1 paint event:
if (null != mImage)
{
e.Graphics.DrawImage(mImage, theLocationOfImage);
}
DrawRectangle(e.Graphics);
And last the calculate new size factor method:
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;
}
}
I could resize zoom in out the whole image but i want to zoom in out only the area the rectangle is drawn over.
EDIT
Forgot to add this method:
private Bitmap GetPartOfImageInRect(Bitmap source, Rectangle rect)
{
return source.Clone(rect, source.PixelFormat);
}