在 C# 中以编程方式调整图像大小

发布于 2024-08-20 06:12:17 字数 198 浏览 4 评论 0原文

可能的重复:
调整图像大小 C#

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

Possible Duplicate:
Resize an Image C#

How can I programmatically resize an image in C# so that it'll fit in a winforms control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小情绪 2024-08-27 06:12:18

放置并居中 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;
}

Sacle and center an 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;
}
怀念你的温柔 2024-08-27 06:12:17

将图像缩放到 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);

Scaling an image into a 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;
        }
    }
}

And how to use it:

Image img = Image.FromFile(imageFilePath);
Rectangle newRect = GetScaledRectangle(img, pictureBox1.ClientRectangle);
pictureBox1.Image = ImageHandling.GetResizedImage(img, newRect);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文