⌘BABAKOV.NET Let's code!
100% [light] [ru]

Changing Image Size in C#

From the archive - might come in handy...
Often when adding images to a project, you need to change the image size, and often you want to do it properly, with the image proportions preserved. Personally, I often need to insert images into a ListView and shrink them correctly (so they don't spread out in a square of the specified size). This is exactly where the procedure below will help.

Size change procedure:

static Image ScaleImage(Image source, int width, int height)
        {

            Image dest = new Bitmap(width, height);
            using (Graphics gr = Graphics.FromImage(dest))
            {
                gr.FillRectangle(Brushes.White, 0, 0, width, height);  // Очищаем экран
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                float srcwidth = source.Width;
                float srcheight = source.Height;
                float dstwidth = width;
                float dstheight = height;

                if (srcwidth <= dstwidth && srcheight <= dstheight)  // Исходное изображение меньше целевого
                {
                    int left = (width - source.Width) / 2;
                    int top = (height - source.Height) / 2;
                    gr.DrawImage(source, left, top, source.Width, source.Height);
                }
                else if (srcwidth / srcheight > dstwidth / dstheight)  // Пропорции исходного изображения более широкие
                {
                    float cy = srcheight / srcwidth * dstwidth;
                    float top = ((float)dstheight - cy) / 2.0f;
                    if (top < 1.0f) top = 0;
                    gr.DrawImage(source, 0, top, dstwidth, cy);
                }
                else  // Пропорции исходного изображения более узкие
                {
                    float cx = srcwidth / srcheight * dstheight;
                    float left = ((float)dstwidth - cx) / 2.0f;
                    if (left < 1.0f) left = 0;
                    gr.DrawImage(source, left, 0, cx, dstheight);
                }

                return dest;
            }
        }



Now, here's an example of how you can use this procedure further. The example inserts an image into a 200x200 square, proportionally compressing it first using the example above, and outlining everything with a black border.

static Image ScaleImageMain(Image img)
        {
            int x1 = 200;
            int y1 = 200;
            int x2 = 3;
            int y2 = 3;
            if (img.Width > img.Height)
            {
                x1 = 200;
                y1 = (int)Math.Round((double)img.Height / (img.Width / 200));
                y2 = (int)Math.Round((double)((200 - y1) / 2));

            }
            else
            {
                if (img.Width < img.Height)
                {
                    y1 = 200;
                    x1 = (int)Math.Round((double)img.Width / (img.Height / 200));
                    x2 = (int)Math.Round((double)((200 - x1) / 2));
                }
            }
            img = ScaleImage(img, x1, y1);
            Image dest = new Bitmap(208, 208);
            Graphics gr = Graphics.FromImage(dest);
            // Здесь рисуем рамку.
            Pen blackPen = new Pen(Color.Black, 3);
            float x = 0.0F;
            float y = 0.0F;
            float width = 208.0F;
            float height = 208.0F;
            gr.DrawRectangle(blackPen, x, y, width, height);

            gr.DrawImage(img, x2, y2, img.Width, img.Height);

            return dest;
        }

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.