使用鼠标拖动调整图像大小 (C#)
我在仅通过拖动鼠标来调整图像大小时遇到一些问题。我找到了一种平均调整大小方法,现在尝试修改它以使用鼠标而不是给定值。
我这样做的方式对我来说很有意义,但也许你们可以给我一些更好的想法。我基本上使用鼠标当前位置和鼠标先前位置之间的距离作为缩放因子。如果当前鼠标位置与图像中心之间的距离小于先前鼠标位置与图像中心之间的距离,则图像会变小,反之亦然。
使用下面的代码,在创建具有新高度和宽度的新位图时,我遇到了参数异常(无效参数),我真的不明白为什么......有什么想法吗?
- - - - - - - - - - - - - - - - -编辑 - - - - - - - - --------------------------------------
好的,感谢 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;
}
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?
---------------------------------EDIT------------------------------------------------------
Ok, thanks to Aaronaught the exception problem has been fixed and I updated the code below. Now I'm having a problem making the resize look smooth and finding a way to keep it from distorting to the point that you can't recognize the picture after resizing it multiple times.
My idea for keeping it from distorting is to change it back to the original image when it is within a certain range of sizes; but I'm not too sure how I would make that work without it looking wierd. Here's the updated code:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了最大限度地减少失真,您需要找出图像的实际尺寸和图像的新尺寸之间的差异,并始终根据原始图像创建调整大小的图像。每当图像大小发生变化时,请在您正在绘制图像的控件上调用
Refresh
。顺便说一句,将
SizeMode
设置为PictureBoxSizeMode.Zoom
的PictureBox
控件可以为您处理图像大小调整。调整大小的代码只需要调整 PictureBox 的大小。 (当然,在您的情况下使用控件可能没有意义,但我想我会让您知道以防万一)。To minimize distortion, you need to figure out the difference between the actual size of the image and the new size of the image and always create the re-sized image from the original image. Whenever the image size changes, call
Refresh
on the control that you are drawing the image on.By the way, a
PictureBox
control with itsSizeMode
set toPictureBoxSizeMode.Zoom
can handle the image resizing for you. Your code to re-size would only need to re-size the PictureBox. (Of course, it may not make sense to use a control in your situation, but I thought I would let you know just in case).如果鼠标根本没有移动会发生什么?您没有处理
nPercent
计算结果为0
的情况。如果您尝试创建高度和宽度为零的位图,则会出现异常。
What happens if the mouse hasn't moved at all? You're not handling the case where
nPercent
evaluates to0
.If you try to create a
Bitmap
with zero height and width, that is the exception you'll get.