Hi all. I'm having some trouble resizing an image just by dragging the mouse. I found an average resize method and now am trying to modify it to use the mouse instead of given values.
The way I'm doing it makes sense to me but maybe you guys can give me some better ideas. I'm basically using the distance between the current location of the mouse and the previous location of the mouse as the scaling factor. If the distance between the current mouse location and the center of of the image is less than the distance between previous mouse location and the center of the image then the image gets smaller, and vice-versa.
With the code below I'm getting an Argument Exception (invalid parameter) when creating the new bitmap with the new height and width and I really don't understand why... any ideas?
private static Image resizeImage(Image imgToResize, System.Drawing.Point prevMouseLoc, System.Drawing.Point currentMouseLoc)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float dCurrCent = 0; //Distance between current mouse location and the center
of the image
float dPrevCent = 0; //Distance between previous mouse location and the center
of the image
float dCurrPrev = 0; //Distance between current mouse location and the
previous mouse location
int sign = 1;
System.Drawing.Point imgCenter = new System.Drawing.Point();
float nPercent = 0;
imgCenter.X = imgToResize.Width / 2;
imgCenter.Y = imgToResize.Height / 2;
// Calculating the distance between the current mouse location and the center
of the image
dCurrCent = (float)Math.Sqrt(Math.Pow(currentMouseLoc.X - imgCenter.X, 2) +
Math.Pow(currentMouseLoc.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.XimgCenter.X,2) +
Math.Pow(prevMouseLoc.Y - imgCenter.Y, 2));
// Calculating the sign value
if (dCurrCent >= dPrevCent)
{
sign = 1;
}
else
{
sign = -1;
}
nPercent = sign * (float)Math.Sqrt(Math.Pow(currentMouseLoc.X -
prevMouseLoc.X, 2) + Math.Pow(currentMouseLoc.Y - prevMouseLoc.Y,
2));
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight); // exception thrown here
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}