【问题标题】:Resizing image programmatically in C# [duplicate]在 C# 中以编程方式调整图像大小 [重复]
【发布时间】:2011-01-12 13:33:37
【问题描述】:

可能重复:
Resize an Image C#

如何在 C# 中以编程方式调整图像的大小以使其适合 winforms 控件?

【问题讨论】:

    标签: c# .net winforms image-manipulation


    【解决方案1】:

    将图像缩放为 PictureBox:

    class ImageHandling
    {
        public static Rectangle GetScaledRectangle(Image img, Rectangle thumbRect)
        {
            if (img.Width < thumbRect.Width && img.Height < thumbRect.Height)
                return new Rectangle(thumbRect.X + ((thumbRect.Width - img.Width) / 2), thumbRect.Y + ((thumbRect.Height - img.Height) / 2), img.Width, img.Height);
    
            int sourceWidth = img.Width;
            int sourceHeight = img.Height;
    
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
    
            nPercentW = ((float)thumbRect.Width / (float)sourceWidth);
            nPercentH = ((float)thumbRect.Height / (float)sourceHeight);
    
            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;
    
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);
    
            if (destWidth.Equals(0))
                destWidth = 1;
            if (destHeight.Equals(0))
                destHeight = 1;
    
            Rectangle retRect = new Rectangle(thumbRect.X, thumbRect.Y, destWidth, destHeight);
    
            if (retRect.Height < thumbRect.Height)
                retRect.Y = retRect.Y + Convert.ToInt32(((float)thumbRect.Height - (float)retRect.Height) / (float)2);
    
            if (retRect.Width < thumbRect.Width)
                retRect.X = retRect.X + Convert.ToInt32(((float)thumbRect.Width - (float)retRect.Width) / (float)2);
    
            return retRect;
        }
    
        public static Image GetResizedImage(Image img, Rectangle rect)
        {
            Bitmap b = new Bitmap(rect.Width, rect.Height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(img, 0, 0, rect.Width, rect.Height);
            g.Dispose();
    
            try
            {
                return (Image)b.Clone();
            }
            finally
            {
                b.Dispose();
                b = null;
                g = null;
            }
        }
    }
    

    以及如何使用它:

    Image img = Image.FromFile(imageFilePath);
    Rectangle newRect = GetScaledRectangle(img, pictureBox1.ClientRectangle);
    pictureBox1.Image = ImageHandling.GetResizedImage(img, newRect);
    

    【讨论】:

    • 对不起,这个方法能解决什么问题?除了与 Image 类的紧密耦合之外,这不会调整图片的大小,只是提供一种计算在调整到定义的宽度和高度(tumbRect)时如何保持纵横比的方法。
    • 我正在使用它来正确缩放在固定大小的图片框中显示的图像,而无需触及原始文件。
    • 好吧,实际的调整大小还是发生在ImageHandler.GetResizeImage,我猜这是你自己的实用程序类?
    • 哦,对了,忘记粘贴了...谢谢!
    【解决方案2】:

    Sacle 和 ImageBox 居中:

    private void LoadAndResizeImage(string sImagepath, int iMaxHeight, int iMaxWidth, int iMinX, int iMinY)
    {
        int wdt = iMaxWidth;
        int hgt = iMaxHeight;
        int partialX = iMinX;
        int partialY = iMinY;
    
        pictureBox1.ImageLocation = sImagepath;
        if ((pictureBox1.Image.Height > iMaxHeight) || (pictureBox1.Image.Width > iMaxWidth))
        {
            if ((pictureBox1.Image.Width / iMaxWidth) > (pictureBox1.Image.Height / iMaxHeight))
            {
                wdt = iMaxWidth;
                double ratio = (double)pictureBox1.Image.Height / (double)pictureBox1.Image.Width;
                hgt = (int)(iMaxWidth * ratio);
            }
            else
            {
                hgt = iMaxHeight;
                double ratio = (double)pictureBox1.Image.Width / (double)pictureBox1.Image.Height;
                wdt = (int)(iMaxHeight * ratio);
            }
        }
        else
        {
            hgt = pictureBox1.Image.Height;
            wdt = pictureBox1.Image.Width;
        }
        if (wdt < iMaxWidth)
        {
            partialX = (iMaxWidth - wdt) / 2;
            partialX += iMinX;
        }
        else
        {
            partialX = iMinX;
        }
        if (hgt < iMaxHeight)
        {
            partialY = (iMaxHeight - hgt) / 2;
            partialY += iMinY;
        }
        else
        {
            partialY = iMinY;
        }
    
    
        //Set size
        pictureBox1.Height = hgt;
        pictureBox1.Width = wdt;
        pictureBox1.Left = partialX;
        pictureBox1.Top = partialY;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-30
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2012-05-07
      • 2018-04-22
      相关资源
      最近更新 更多