0

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);
        }
user3200169
  • 275
  • 4
  • 17
  • It is up to you to define where the location should be. What do you want to do? Do you want to resize a part of the image and center it in the original image? Please explain! – Olivier Jacot-Descombes Jan 19 '14 at 16:14
  • Olivier yes i draw a rectangle over the pictureBox1 thats the mRect variable and when im using the mouse wheel i want that only the part of the retangle of the image to be resize like zoom in/out – user3200169 Jan 19 '14 at 16:16
  • Olivier i just updated my question added the needed methods and events. – user3200169 Jan 19 '14 at 16:20
  • Oliver yes center it in the original image i drawed the rectangle in the same rectangle place. So it will resize zoom in out only the area of the drawed rectangle. – user3200169 Jan 19 '14 at 16:40
  • I did this: e.Graphics.DrawImage(mImage, mRect); in the paint event. But when its resizing in/out it look blurry. – user3200169 Jan 19 '14 at 16:47
  • See [High Quality Image Scaling C#](http://stackoverflow.com/q/249587/880990). The important part is: "set the resize quality modes to high quality" – Olivier Jacot-Descombes Jan 19 '14 at 17:03

1 Answers1

0

The problem when zooming is that the aspect ratio of the rectangle to be zoomed will probably be different from the aspect ratio of the whole image. Therefore you must consider two different cases.

// Calculate the size and position of the zoomed rectangle.
double zoomFactorX = picturBox1.Width / mRect.Width;
double zoomFactorY = picturBox1.Height / mRect.Height;
Size newSize;
Point newLocation;
if (zoomFactorX < zoomFactorY) { // Fit image portion horizontally.
    newSize = new Size(picturBox1.Width, (int)Math.Round(zoomFactorX * mRect.Height));

    // We have a top and a bottom padding.
    newLocation = new Point(0, (picturBox1.Height - newSize.Height) / 2);
} else {  // Fit image portion vertically.
    newSize = new Size((int)Math.Round(zoomFactorY * mRect.Width), picturBox1.Height);

    // We have a left and a right padding.
    newLocation = new Point((picturBox1.Width - newSize.Width) / 2, 0);
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Olivier i forgot to add a small method so i added it now to my question. It called: GetPartOfImageInRect maybe its important. – user3200169 Jan 19 '14 at 16:49
  • Olivier in your answer where do i put this code ? In the mouse wheel event ? – user3200169 Jan 19 '14 at 16:51
  • I would use the mouse events only to determine the size and position of the zoom rectangle. Creating the new image and calculating its size and position should go to the Paint method. I am only showing you the principle here; you might have to adapt my code to fit your existing code. – Olivier Jacot-Descombes Jan 19 '14 at 16:58