【问题标题】:Pad or Crop Image to achieve square size in C#在 C# 中填充或裁剪图像以实现正方形大小
【发布时间】:2017-05-10 19:36:17
【问题描述】:

我有通常小于 500x500 像素的位图图像。 但有时一个或两个维度可能超过 500 像素。 如果高度> 500,我想在底部裁剪图像,如果宽度> 500,我想在两侧均等地裁剪它。 如果图像在任何维度上小于 500,我想在每一侧用白色像素填充它,使其成为 500x500。 我不熟悉 .NET,但我知道已经为您完成了很多工作(我是 C++ 开发人员)。 我很感激任何帮助!谢谢!

这是我目前所拥有的,它将图像置于白色 500x500 矩形图像的中心。只是对于原始图像的一维超过 500 像素的情况,我无法理解。 (见??这两行)

public static System.Drawing.Bitmap PadImage(System.Drawing.Bitmap originalImage)
        {
            if (originalImage.Height > 500)
             ??    
            if (originalImage.Width > 500)   
             ??

            Size squareSize = new Size(500, 500);
            System.Drawing.Bitmap squareImage = new System.Drawing.Bitmap(squareSize.Width, squareSize.Height);
            using (Graphics graphics = Graphics.FromImage(squareImage))
            {
                graphics.FillRectangle(System.Drawing.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;
        }

【问题讨论】:

  • 这表明没有任何研究工作
  • 你问了 5 个问题......
  • @EpicKip 抱歉,我发的太快了,没有加代码。
  • 假设它是 1000*1000,你会剪切它还是调整它的大小?因为我刚刚意识到我面前的东西几乎就是答案

标签: c# .net


【解决方案1】:
Bitmap PadCropImage(Bitmap original)
{
    if (original.Width == 500 && original.Height == 500)
        return original;

    if (original.Width > 500 && original.Height > 500)
    {
        int x = (original.Width - 500) / 2;
        int y = (original.Height - 500) / 2;
        return original.Clone(new Rectangle(x, y, 500, 500), original.PixelFormat);
    }

    Bitmap square = new Bitmap(500, 500);
    var g = Graphics.FromImage(square);

    if (original.Width > 500)
    {
        int x = (original.Width - 500) / 2;
        int y = (500 - original.Height) / 2;
        g.DrawImageUnscaled(original, -x, y);
    }
    else if (original.Height > 500)
    {
        int x = (500 - original.Width) / 2;
        int y = (original.Height - 500) / 2;
        g.DrawImageUnscaled(original, x, -y);
    }
    else
    {
        int x = (500 - original.Width) / 2;
        int y = (500 - original.Height) / 2;
        g.DrawImageUnscaled(original, x, y);
    }

    return square;
}

【讨论】:

    【解决方案2】:

    这是我用过很多次的老方法

    public static Image ThumbnailImage(Image sourceImage, int imageSize, bool maintainAspectRatio, bool maintainImageSize, Color backgroundColor)
            {
                try
                {
    
                    int thumbnailWidth = imageSize;
                    int thumbnailHeight = imageSize;
    
                    if (maintainAspectRatio)
                    {
                        float aspectRatio = (float) sourceImage.Width/sourceImage.Height;
                        float targetAspectRatio = (float) thumbnailWidth/thumbnailHeight;
    
                        if (aspectRatio < targetAspectRatio)
                        {
                            thumbnailWidth = (int) (thumbnailHeight*aspectRatio);
                        }
                        else if (aspectRatio > targetAspectRatio)
                        {
                            thumbnailHeight = (int) (thumbnailWidth/aspectRatio);
                        }
                    }
    
                    Image thumbnail = sourceImage.GetThumbnailImage(thumbnailWidth, thumbnailHeight, null, new IntPtr());
    
                    if (maintainImageSize)
                    {
                        var offset = new Point(0, 0);
                        if (thumbnailWidth != imageSize)
                        {
                            offset.X = ((imageSize - thumbnailWidth)/2);
                        }
                        if (thumbnailHeight != imageSize)
                        {
                            offset.Y = ((imageSize - thumbnailHeight)/2);
                        }
    
                        var bmpImage = new Bitmap(imageSize, imageSize, PixelFormat.Format32bppArgb);
    
                        using (Graphics graphics = Graphics.FromImage(bmpImage))
                        {
                            graphics.Clear(backgroundColor);
                            graphics.DrawImage(thumbnail, new Rectangle(offset.X, offset.Y, thumbnailWidth, thumbnailHeight), new Rectangle(0, 0, thumbnailWidth, thumbnailHeight), GraphicsUnit.Pixel);
                        }
                        thumbnail.Dispose();
                        return Image.FromHbitmap(bmpImage.GetHbitmap());
                    }
                    return thumbnail;
                }
                catch (Exception exception)
                {
                    const string strExMsg = "Error Creating Thumbnail";
                    throw new Exception(Assembly.GetExecutingAssembly().GetName().Name + " - " + strExMsg + " Msg : " + exception.Message);
                }
    
            }
    

    【讨论】:

    • 实际上刚刚意识到这不会填充图像,但会调整它的大小
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2016-11-29
    • 2013-02-27
    • 1970-01-01
    • 2014-01-27
    • 2012-04-23
    相关资源
    最近更新 更多