【问题标题】:Make square image制作方形图像
【发布时间】:2010-05-12 22:25:12
【问题描述】:

如何在不使用任何 3rd 方库(仅限 .Net 框架)的情况下将图像重新采样为正方形,最好在 c# 中使用白色背景填充?

谢谢!

【问题讨论】:

  • 您想将新重新采样的图像放入带有白色画布的正方形中以使图像保持其纵横比,还是将其拉伸以适应正方形(使白色画布变得不必要)?
  • 我想保持纵横比 - 不拉伸
  • @MichaelD.这些不兼容吗?平方改变纵横比。还是可以接受剪裁图像?

标签: c# .net image


【解决方案1】:

这实际上可以很容易地完成。

public static Image PadImage(Image originalImage)
{
    int largestDimension = Math.Max(originalImage.Height, originalImage.Width);
    Size squareSize = new Size(largestDimension, largestDimension);
    Bitmap squareImage = new Bitmap(squareSize.Width, squareSize.Height);
    using (Graphics graphics = Graphics.FromImage(squareImage))
    {
        graphics.FillRectangle(Brushes.White, 0, 0, squareSize.Width, squareSize.Height);
        graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        graphics.DrawImage(originalImage, (squareSize.Width / 2) - (originalImage.Width / 2), (squareSize.Height / 2) - (originalImage.Height / 2), originalImage.Width, originalImage.Height);
    }
    return squareImage;
}

【讨论】:

  • 我知道这是一篇旧帖子,但它仍然有用。我实际上使用 Math.Min 来裁剪图像以获得缩略图。完美运行!
  • 如果您将所有 10 个高度和宽度都转换为整数,但尺寸和图像值是双倍的,则此方法有效。
【解决方案2】:

尝试使用此方法。最后一个参数是您是否要拉伸图像以适应的开关。如果为 false,则图像在新的白色画布内居中。您可以根据需要将正方形或非正方形大小传递给它。

    public static Bitmap ResizeBitmapOnWhiteCanvas(Bitmap bmpOriginal, Size szTarget, bool Stretch)
    {
        Bitmap result = new Bitmap(szTarget.Width, szTarget.Height);
        using (Graphics g = Graphics.FromImage((Image)result))
        {
            g.InterpolationMode = InterpolationMode.NearestNeighbor;
            g.FillRectangle(Brushes.White, new Rectangle(0, 0, szTarget.Width, szTarget.Height));
            if (Stretch)
            {
                g.DrawImage(bmpOriginal, 0, 0, szTarget.Width, szTarget.Height); // fills the square (stretch)
            }
            else
            {
                float OriginalAR = bmpOriginal.Width / bmpOriginal.Height;
                float TargetAR = szTarget.Width / szTarget.Height;
                if (OriginalAR >= TargetAR)
                {
                    // Original is wider than target
                    float X = 0F;
                    float Y = ((float)szTarget.Height / 2F) - ((float)szTarget.Width / (float)bmpOriginal.Width * (float)bmpOriginal.Height) / 2F;
                    float Width = szTarget.Width;
                    float Height = (float)szTarget.Width / (float)bmpOriginal.Width * (float)bmpOriginal.Height;
                    g.DrawImage(bmpOriginal, X, Y, Width, Height);
                }
                else
                {
                    // Original is narrower than target
                    float X = ((float)szTarget.Width / 2F) - ((float)szTarget.Height / (float)bmpOriginal.Height * (float)bmpOriginal.Width) / 2F;
                    float Y = 0F;
                    float Width = (float)szTarget.Height / (float)bmpOriginal.Height * (float)bmpOriginal.Width;
                    float Height = szTarget.Height;
                    g.DrawImage(bmpOriginal, X, Y, Width, Height);
                }
            }
        }
        return result;
    }

【讨论】:

    【解决方案3】:

    你没有说你希望它如何填充。假设您希望图像居中,图像文件名在 imageFileName 中,所需的输出文件名在 newFileName 中:

            Bitmap orig = new Bitmap(imageFileName);
            int dim = Math.Max(orig.Width, orig.Height);
            Bitmap dest;
            using (Graphics origG = Graphics.FromImage(orig))
            {
                dest = new Bitmap(dim, dim, origG);
            }
            using (Graphics g = Graphics.FromImage(dest))
            {
                Pen white = new Pen(Color.White, 22);
                g.FillRectangle(new SolidBrush(Color.White), 0, 0, dim, dim);
                g.DrawImage(orig, new Point((dim - orig.Width) / 2, (dim - orig.Height) / 2));
            }
            dest.Save(newFileName);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      相关资源
      最近更新 更多