C# 重构两个函数失败

C# 重构两个函数失败,c#,C#,我有两种方法:一种是处理更新图片,另一种是调整图片大小并绘制矩形 public void updatePicture() { if (imageList.SelectedItem == null) return; /*String fileName = imageList.SelectedItem.ToString(); var currentFile = new System.IO.FileInfo(imageList.SelectedItem.ToString())

我有两种方法:一种是处理更新图片,另一种是调整图片大小并绘制矩形

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():

有人能看出我的错误吗?

也许这有帮助

    if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
    {

        pictureBox1.Image = bitmap;

        // add this line...
        pictureBox1.Refresh();

        return;
    }
也许你知道我在做什么。。。在所有情况下都调用刷新。

if(bitmap.Width        if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
    {

        pictureBox1.Image = bitmap;

        return;
    }
{ pictureBox1.Image=位图; 返回; } 如果语句为true且不绘制,则此代码位将返回

我将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);
        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(); }
    }
private void resizeImage(布尔绘制、布尔更新)
{
如果(!isValid())返回;
如果(更新)
{
String fileName=imageList.SelectedItem.ToString();
var currentFile=new System.IO.FileInfo(imageList.SelectedItem.ToString());
pictureBox1.Load(文件名);
}
位图位图=新位图(pictureBox1.Image);
bool needsCrop=true;
if(bitmap.Width

很抱歉浪费您的时间://

希望如果有人在谷歌上搜索“我很困惑”,他们会在这里结束。编辑:在霍根确定你的标题之前,这是一个更好的笑话。你应该编辑你的标题,让我们了解你的名字problem@Antiga-我很欣赏这个笑话。pictureBox1.Refresh()仅在drawRect()中调用。看起来?看起来您正在调整图像大小以适应pictureBox1的高度和宽度。为什么不能使用
PictureBox.SizeMode
属性并将其设置为
StretchImage
以获得相同的结果?这不是问题所在,但它帮助我解决了问题。当它检查宽度和高度是否小于pictureBox的宽度和高度时,它不会绘制。。谢谢:)@adfje-很好。。。很高兴听到这个消息。
    pictureBox1.Image = cropBitmap;
    // add this line...
    pictureBox1.Refresh();
        if (bitmap.Width < pictureBox1.Width && bitmap.Height < pictureBox1.Height)
    {

        pictureBox1.Image = bitmap;

        return;
    }
        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(); }
    }