Home

Operations on Bitmaps: Scaling a Picture

     

Introduction

Scaling a picture consists of changing its size, either to widen, to enlarge, to narrow, to heighten, or to shrink it. Although there are various complex algorithms you can use to perform this operation, the Bitmap class provides a shortcut. Of course, in order to scale a picture, you must first have one.

To support picture scaling, the Bitmap class provides the following constructor:

public Bitmap(Image original, int width, int height);

The original argument is the Image, such as a Bitmap object, that you want to scale. The width and the height arguments represent the new size you want to apply to the picture. After this constructor has been used, you get a bitmap with the new size. Here is an example of using it:

Picture Normal Size

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExoPicture1
{
    public partial class Form1 : Form
    {
        Bitmap bmpPicture;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bmpPicture = new Bitmap(10, 10);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(bmpPicture, 120, 12);
        }

        private void btnLoadPicture_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlgOpen = new OpenFileDialog();

            if (dlgOpen.ShowDialog() == DialogResult.OK)
            {
                String strFilename = dlgOpen.FileName;
                bmpPicture = new Bitmap(strFilename);
                int width = bmpPicture.Width;
                int height = bmpPicture.Height;

                textBox1.Text = width.ToString();
                textBox2.Text = height.ToString();

                Invalidate();
            }
        }

        private void btnResize_Click(object sender, EventArgs e)
        {
            int width  = 0,
                height = 0;

            try
            {
                width = int.Parse(textBox1.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("The value you entered for the width is not valid");
            }

            try
            {
                height = int.Parse(textBox2.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("The value you entered for the height is not valid");
            }

            Bitmap bmpNew = new Bitmap(bmpPicture, width, height);
            bmpPicture = bmpNew;
            Invalidate();
        }
    }
}

Picture Resized

You can also specify both the width and the height as the size. To do this, you can use the following constructor:

public Bitmap(Image original, Size newSize);

As you can see, the scaling operation could produce a kind of distorted picture. An alternative would be to keep the ration of both dimensions while changing the value of one.

 
 

Home Copyright © 2010-2016, FunctionX