使用鼠标拖动调整图像大小(C#)

使用鼠标拖动调整图像大小(C#),c#,image,resize,mouse,C#,Image,Resize,Mouse,仅通过拖动鼠标调整图像大小时遇到问题。我发现了一个平均调整大小的方法,现在正试图修改它,使用鼠标,而不是给定的值 我这样做对我来说很有意义,但也许你们能给我一些更好的主意。我基本上使用鼠标当前位置和鼠标前一个位置之间的距离作为缩放因子。如果当前鼠标位置与图像中心之间的距离小于先前鼠标位置与图像中心之间的距离,则图像将变小,反之亦然 使用下面的代码,我在创建具有新高度和宽度的新位图时遇到参数异常(无效参数),我真的不明白为什么。。。有什么想法吗 --------------------------

仅通过拖动鼠标调整图像大小时遇到问题。我发现了一个平均调整大小的方法,现在正试图修改它,使用鼠标,而不是给定的值

我这样做对我来说很有意义,但也许你们能给我一些更好的主意。我基本上使用鼠标当前位置和鼠标前一个位置之间的距离作为缩放因子。如果当前鼠标位置与图像中心之间的距离小于先前鼠标位置与图像中心之间的距离,则图像将变小,反之亦然

使用下面的代码,我在创建具有新高度和宽度的新位图时遇到参数异常(无效参数),我真的不明白为什么。。。有什么想法吗

---------------------------------编辑------------------------------------------------------

好的,多亏了Aaronaught,异常问题已经解决,我更新了下面的代码。现在,我遇到了一个问题,就是如何使调整大小的图像看起来平滑,并找到一种方法使其不失真到在多次调整大小后无法识别图片的程度

我的想法是,当它在一定的尺寸范围内时,把它改回原始图像;但我不太确定,如果它看起来不太结实,我怎么能让它工作。以下是更新的代码:

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;
        float dPrevCent = 0;
        float dCurrPrev = 0;
        bool increase = true;
        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.X - imgCenter.X, 2) + Math.Pow(prevMouseLoc.Y - imgCenter.Y, 2));

        // Setting flag to increase or decrease size
        if (dCurrCent >= dPrevCent)
        {
            increase = true;
        }
        else
        {
            increase = false;
        }

        // Calculating the scaling factor
        dCurrPrev = nPercent = (float)Math.Sqrt(Math.Pow(currentMouseLoc.X - prevMouseLoc.X, 2) + Math.Pow(currentMouseLoc.Y - prevMouseLoc.Y, 2));

        if (increase)
        {
            nPercent = (float)dCurrPrev;
        }
        else
        {
            nPercent = (float)(1 / dCurrPrev);
        }

        // Calculating the new height and width of the image
        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        // Create new bitmap, resize image (within limites) and return it
        if (nPercent != 0 && destWidth > 100 && destWidth < 600)
        {
            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }
        else
            return imgToResize;
    }
private static Image resizeImage(Image imgToResize,System.Drawing.Point prevMouseLoc,System.Drawing.Point currentMouseLoc)
{
int sourceWidth=imgToResize.Width;
int sourceHeight=imgToResize.Height;
浮点数电流=0;
浮动dPrevCent=0;
浮动dCurrPrev=0;
布尔增加=真;
System.Drawing.Point imgCenter=新的System.Drawing.Point();
浮动百分比=0;
imgCenter.X=imgToResize.Width/2;
imgCenter.Y=imgToResize.Height/2;
//计算当前鼠标位置与图像中心之间的距离
dCurrCent=(float)Math.Sqrt(Math.Pow(currentMouseLoc.X-imgCenter.X,2)+Math.Pow(currentMouseLoc.Y-imgCenter.Y,2));
//计算上一个鼠标位置与图像中心之间的距离
dPrevCent=(float)Math.Sqrt(Math.Pow(prevMouseLoc.X-imgCenter.X,2)+Math.Pow(prevMouseLoc.Y-imgCenter.Y,2));
//设置标志以增大或减小大小
如果(Dcurrent>=Dprevent)
{
增加=真;
}
其他的
{
增加=错误;
}
//计算比例因子
dcurrprov=nPercent=(float)Math.Sqrt(Math.Pow(currentMouseLoc.X-prevMouseLoc.X,2)+Math.Pow(currentMouseLoc.Y-prevMouseLoc.Y,2));
如果(增加)
{
nPercent=(浮动)dcurrprov;
}
其他的
{
n%=(浮动)(1/dcurrprov);
}
//计算图像的新高度和宽度
int destWidth=(int)(sourceWidth*n百分比);
int destHeight=(int)(源高度*n百分比);
//创建新位图,调整图像大小(在限制范围内)并返回它
如果(nPercent!=0&&destWidth>100&&destWidth<600)
{
位图b=新位图(destWidth、destHeight);
Graphics g=Graphics.FromImage((图像)b);
g、 插值模式=插值模式。高质量双三次;
g、 DrawImage(imgToResize、0、0、destWidth、destHeight);
g、 处置();
返回(图像)b;
}
其他的
返回imgToResize;
}

如果鼠标没有移动,会发生什么?您没有处理
nPercent
计算结果为
0
的情况


如果您尝试创建高度和宽度为零的
位图
,则会出现例外情况。

如果鼠标完全没有移动,会发生什么情况?您没有处理
nPercent
计算结果为
0
的情况


如果您尝试创建高度和宽度为零的
位图,则会出现例外情况。

为了最大限度地减少失真,您需要计算图像的实际大小与图像的新大小之间的差异,并始终从原始图像创建重新调整大小的图像。每当图像大小发生变化时,在绘制图像的控件上调用
刷新


顺便说一下,将
PictureBox
控件的
SizeMode
设置为
PictureBoxSizeMode.Zoom
可以为您调整图像大小。要重新调整大小的代码只需重新调整PictureBox的大小。(当然,在您的情况下使用控件可能没有意义,但我想我会让您知道以防万一)。

为了最大限度地减少失真,您需要找出图像的实际大小和新大小之间的差异,并始终从原始图像创建重新调整大小的图像。每当图像大小发生变化时,在绘制图像的控件上调用
刷新


顺便说一下,将
PictureBox
控件的
SizeMode
设置为
PictureBoxSizeMode.Zoom
可以为您调整图像大小。要重新调整大小的代码只需重新调整PictureBox的大小。(当然,在您的情况下使用控件可能没有意义,但我想我会让您知道,以防万一)。

谢谢,我没有想到这一点。我修正了这个问题,并限制了图片的大小,这样类似的事情就不会再发生了。但是现在我很难使调整大小看起来平滑,也很难找到一种方法来修复多次调整大小后的失真…@Gaax:每次调整大小都应该从原始图像开始。一旦你做了一个“拷贝的拷贝”,你就开始逐渐退化。你不需要复杂的启发法,j