tzippy
tzippy

Reputation: 6638

Pad or Crop Image to achieve square size in C#

I have Bitmap Images that are usually smaller than 500x500 pixels. But sometimes one or both dimensions can exceed 500 pixels. If the height is >500 I want to crop the image at the bottom and if the width is >500 I want to crop it on both sides equally. If the Image is <500 in any dimension I want to pad it with white pixels on each side equally to make it 500x500. I'm not familliar with .NET but I understand there's a lot that'S already been done for you (I'm a C++ developer). I appreciate any help! Thanks!

This is what I have so far, which puts the image in the center of a white 500x500 rectangular image. It's just that I cant wrap my head around the cases where one dimension of the original image exceeds 500 pixels. (See the two lines with ??)

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;
        }

Upvotes: 0

Views: 1727

Answers (2)

Alexander Petrov
Alexander Petrov

Reputation: 14231

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;
}

Upvotes: 2

Mark Redman
Mark Redman

Reputation: 24515

Here is an old method I have used many times

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

        }

Upvotes: 1

Related Questions