【问题标题】:Resizing an image using mouse dragging (C#)使用鼠标拖动调整图像大小 (C#)
【发布时间】:2010-03-21 02:25:36
【问题描述】:

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

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

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

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

好的,感谢 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;
    }

【问题讨论】:

    标签: c# image resize mouse


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果鼠标根本没有移动会发生什么?您没有处理 nPercent 评估为 0 的情况。

      如果您尝试创建一个高度和宽度为零的Bitmap,您会遇到例外情况。

      【讨论】:

      • 谢谢,没想到。我解决了这个问题,并对图片的大小进行了限制,这样类似的事情就不会再发生了。但是现在我无法使调整大小看起来平滑并找到一种方法来修复多次调整大小后的失真......
      • @Gaax:每次调整大小都应该从原始图像完成。一旦您制作了“副本的副本”,您就会开始逐渐退化。这里不需要复杂的启发式,只要始终使用原始作为源即可。
      • 我改变了我的代码,所以我现在正在这样做,它仍然在做同样的事情,但它需要更长的时间......这可能与我计算新高度的方式有关和宽度?
      • @Gaxx:我强烈怀疑您仍然看到的任何渐进式失真是因为您从该方法获取结果图像并替换原始图像。我需要查看其余代码才能确定。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多