重构两个函数失败
本文关键字:两个 函数 失败 重构 | 更新日期: 2023-09-27 18:35:33
我有两种方法:一种处理更新图片,另一种调整图片大小并绘制矩形。
public void updatePicture()
{
if (imageList.SelectedItem == null) return;
/*String fileName = imageList.SelectedItem.ToString();
var currentFile = new System.IO.FileInfo(imageList.SelectedItem.ToString());
pictureBox1.Load(fileName); This code is moved to resizeImage*/
resizeImage(false, true);
//drawRect(); It works if I enable this code
}
但是,我正在尝试将所有内容移动到 resizeImage():
private void resizeImage(Boolean draw, Boolean update)
{
if (!isValid()) return;
if (update)
{
String fileName = imageList.SelectedItem.ToString();
var currentFile = new System.IO.FileInfo(imageList.SelectedItem.ToString());
pictureBox1.Load(fileName);
}
Bitmap bitmap = new Bitmap(pictureBox1.Image);
if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
{
pictureBox1.Image = bitmap;
return;
}
int width, height;
float percentWidth = (float)pictureBox1.Width / (float)bitmap.Width;
float percentHeight = (float)pictureBox1.Height / (float)bitmap.Height;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
width = Convert.ToInt32(bitmap.Width * percent);
height = Convert.ToInt32(bitmap.Height * percent);
Bitmap cropBitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(cropBitmap);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(bitmap, 0, 0, cropBitmap.Width, cropBitmap.Height);
pictureBox1.Image = cropBitmap;
if (draw)
{
drawRect();
}
}
在这种状态下,它不起作用,但要使其工作,我只需在 updatePicture() 方法中调用 drawRect() 方法,它基本上只是移动代码。我也不会在不同的时间打电话。
这是我的drawRect()方法:
private void drawRect()
{
pictureBox1.Refresh();
pictureBox1.CreateGraphics().DrawRectangle(pen, cropX, cropY, cropW, cropH);
}
有人能看到我的错误吗?
也许这有帮助?
if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
{
pictureBox1.Image = bitmap;
// add this line...
pictureBox1.Refresh();
return;
}
也许也在下面...
pictureBox1.Image = cropBitmap;
// add this line...
pictureBox1.Refresh();
也许你看到我在做什么...在所有情况下调用刷新。
if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
{
pictureBox1.Image = bitmap;
return;
}
如果语句为 true,则返回这段代码,并且不绘制。
我将调整大小图像方法更改为:
private void resizeImage(Boolean draw, Boolean update)
{
if (!isValid()) return;
if (update)
{
String fileName = imageList.SelectedItem.ToString();
var currentFile = new System.IO.FileInfo(imageList.SelectedItem.ToString());
pictureBox1.Load(fileName);
}
Bitmap bitmap = new Bitmap(pictureBox1.Image);
bool needsCrop = true;
if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
{
pictureBox1.Image = bitmap;
needsCrop = false;
}
if (needsCrop)
{
int width, height;
float percentWidth = (float)pictureBox1.Width / (float)bitmap.Width;
float percentHeight = (float)pictureBox1.Height / (float)bitmap.Height;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
width = Convert.ToInt32(bitmap.Width * percent);
height = Convert.ToInt32(bitmap.Height * percent);
Bitmap cropBitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(cropBitmap);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawImage(bitmap, 0, 0, cropBitmap.Width, cropBitmap.Height);
pictureBox1.Image = cropBitmap;
}
if (draw) { drawRect(); }
}
很抱歉浪费您的时间:/