Scaling an image using the mouse in C#

Posted by Gaax on Stack Overflow See other posts from Stack Overflow or by Gaax
Published on 2010-04-02T04:01:05Z Indexed on 2010/04/02 4:03 UTC
Read the original article Hit count: 347

Filed under:
|
|
|
|

Hey guys... I'm trying to use the position of the mouse to calculate the scaling factor for scaling an image. Basically, the further you get away from the center of the image, the bigger it gets; and the closer to the center you get, the smaller it gets. I have some code so far but it's acting really strange and I have absolutely no more ideas. First I'll let you know, one thing I was trying to do is average out 5 distances to get a more smooth resize animation. Here's my code:

private void pictureBoxScale_MouseMove(object sender, MouseEventArgs e)
    {
        if (rotateScaleMode && isDraggingToScale)
        {
            // For Scaling              
            int sourceWidth = pictureBox1.Image.Width;
            int sourceHeight = pictureBox1.Image.Height;
            float dCurrCent = 0; // distance between the current mouse pos and the center of the image
            float dPrevCent = 0; // distance between the previous mouse pos and the center of the image

            System.Drawing.Point imgCenter = new System.Drawing.Point();

            imgCenter.X = pictureBox1.Location.X + (sourceWidth / 2);
            imgCenter.Y = pictureBox1.Location.Y + (sourceHeight / 2);

            // Calculating the distance between the current mouse location and the center of the image
            dCurrCent = (float)Math.Sqrt(Math.Pow(e.X - imgCenter.X, 2) + Math.Pow(e.Y - imgCenter.Y, 2));

            // Calculating the distance between the previous mouse location and the center of the image
            dPrevCent = (float)Math.Sqrt(Math.Pow(prevMouseLoc.X - imgCenter.X, 2) + Math.Pow(prevMouseLoc.Y - imgCenter.Y, 2));

            if (smoothScaleCount < 5)
            {
                dCurrCentSmooth[smoothScaleCount] = dCurrCent;
                dPrevCentSmooth[smoothScaleCount] = dPrevCent;
            }


            if (smoothScaleCount == 4)
            {
                float currCentSum = 0;
                float prevCentSum = 0;
                for (int i = 0; i < 4; i++)
                {
                    currCentSum += dCurrCentSmooth[i];
                }
                for (int i = 0; i < 4; i++)
                {
                    prevCentSum += dPrevCentSmooth[i];
                }

                float scaleAvg = (currCentSum / 5) / (prevCentSum / 5);


                int destWidth = (int)(sourceWidth * scaleAvg);
                int destHeight = (int)(sourceHeight * scaleAvg);

                // If statement is for limiting the size of the image
                if (destWidth > (currentRotatedImage.Width / 2) && destWidth < (currentRotatedImage.Width * 3) && destHeight > (currentRotatedImage.Height / 2) && destWidth < (currentRotatedImage.Width * 3))
                {
                    AForge.Imaging.Filters.ResizeBilinear resizeFilter = new AForge.Imaging.Filters.ResizeBilinear(destWidth, destHeight);
                    pictureBox1.Image = resizeFilter.Apply((Bitmap)currentRotatedImage);
                    pictureBox1.Size = pictureBox1.Image.Size;
                    pictureBox1.Refresh();
                }

                smoothScaleCount = -1;
            }
            prevMouseLoc = e.Location;
            currentScaledImage = pictureBox1.Image;
            smoothScaleCount++;

        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about image