Scaling a Picture

Introduction

Scaling a picture consists of widenning, enlarging, narrowing, heightenning, or shrinking 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.

Practical LearningPractical Learning: Introducing .NET Framework Collections

  1. Start Microsoft Visual Studio
  2. Create a Windows Forms App named BitmapsOperations

Resizing a Picture by Width and Height

To support picture scaling, one of the constructors that the Bitmap class provides uses the following syntax:

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:

Scaling a Picture

Control Name Other Properties
Label Label   New &Width:
TextBox TextBox txtWidth  
Label Label   New &Height:
TextBox TextBox txtHeight  
Button Button btnResizePicture Resize the Picture
Button Button btnOpenPicture Open a Picture...
PictureBox Picture Box pbxCanvas BorderStyle: FixedSingle
OpenFileDialog Open File Dialog dlgPicture Filter:
namespace BitmapOperations
{
    public partial class PictureScaling : Form
    {
        Bitmap? bmpPicture;

        public PictureScaling()
        {
            InitializeComponent();
        }

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

        private void pbxCanvas_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(bmpPicture!, 0, 0);
        }

        private void btnOpenPicture_Click(object sender, EventArgs e)
        {
            if (dlgOpenFile.ShowDialog() == DialogResult.OK)
            {
                bmpPicture = new Bitmap(dlgOpenFile.FileName);

                txtWidth.Text = bmpPicture.Width.ToString();
                txtHeight.Text = bmpPicture.Height.ToString();

                pbxCanvas.Width = bmpPicture.Width;
                pbxCanvas.Height = bmpPicture.Height;

                Width = pbxCanvas.Width + 60;
                Height = pbxCanvas.Height + 110;

                pbxCanvas.Invalidate();
            }
        }

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

            try
            {
                width = int.Parse(txtWidth.Text);
            }
            catch(Exception exc) when (exc is FormatException fex)
            {
                MessageBox.Show("The value you entered for the width is not valid."
                                + Environment.NewLine +
                                "The error produced is: " + fex.Message,
                                "Bitmaps Operations",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            try
            {
                height = int.Parse(txtHeight.Text);
            }
            catch(Exception exc) when (exc is FormatException fex)
            {
                MessageBox.Show("The value you entered for the height is not valid."
                                + Environment.NewLine +
                                "The error produced is: " + fex.Message,
                                "Bitmaps Operations",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            Bitmap bmpNew = new Bitmap(bmpPicture!, width, height);

            bmpPicture = bmpNew;
            pbxCanvas.Invalidate();
        }
    }
}

Scaling a Picture

Scaling a Picture

Scaling a Picture

Scaling a Picture

Scaling a Picture by Size

As another solution to scale a picture, you can specify the size to apply to it. To support this, the Bitmap provides another constructor whose syntax is:

public Bitmap(Image original, Size newSize);

Here is an example of calling this method:

Scaling a Picture

Scaling a Picture

Control Name Other Properties
PictureBox Picture Box pbxCanvas BorderStyle: Fixed3D
VScrollBar Vertical Scroll Bar vsbPicture Maximum: 345
Value: 345
Anchor: Top, Bottom, Right
HScrollBar Horizontal Scroll Bar hsbPicture Maximum: 659
Value: 659
Anchor: Bottom, Left, Right
namespace BitmapOperations
{
    public partial class Exercise : Form
    {
        Bitmap? bmpPicture;

        public Exercise()
        {
            InitializeComponent();
        }

        private void pbxCanvas_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(bmpPicture!, 0, 0);
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            bmpPicture = new Bitmap("C:\\Exercise1\\Leopard.png");
            pbxCanvas.Invalidate();
        }

        private void vsbPicture_Scroll(object sender, ScrollEventArgs e)
        {
            bmpPicture = new Bitmap("C:\\Exercise1\\Leopard.png");
            bmpPicture = new Bitmap(bmpPicture!, new Size(hsbPicture.Value, vsbPicture.Value));
            pbxCanvas.Invalidate();
        }

        private void hsbPicture_Scroll(object sender, ScrollEventArgs e)
        {
            bmpPicture = new Bitmap("C:\\Exercise1\\Leopard.png");
            bmpPicture = new Bitmap(bmpPicture!, new Size(hsbPicture.Value, vsbPicture.Value));
            pbxCanvas.Invalidate();
        }
    }
}

Scaling a Picture

Scaling a Picture

Scaling a Picture

Other Operations on Bitmaps

Copying a Picture

Copying a picture is the process of getting each pixel of a picture from one document, the source, and reproducing it on another document, the target:

Copying a Picture

This operator is easy. from the source, get the (x, y) coordinate of a pixel and assign it to the corresponding (x, y) coordinate on the target document. This can be done as follows:

Copying a Picture

private void btnCopy_Click(object sender, EventArgs e)
{
    Graphics graph = pbxSource.CreateGraphics();
    Bitmap bmpSource = (Bitmap)pbxSource.Image;
    Bitmap bmpDestination =
                new Bitmap(pbxDestination.ClientSize.Width,
                           pbxDestination.ClientSize.Height);

    int width = bmpSource.Width;
    int height = bmpSource.Height;

    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            Color clr = bmpSource.GetPixel(x, y);
            bmpDestination.SetPixel(x, y, clr);
        }
    }

    pbxDestination.Image = bmpDestination;
}

Copying a Picture

Mirroring a Picture

Mirroring a picture consists of changing the horizontal direction of a picture. This is done by transferring each pixel from the source of the picture to a target picture on the opposite side:

Mirror

To mirror a picture, get each of its pixels from one side of its horizontal position and transfer it to the opposite side of the horizon position. The vertical position of each pixel stays the same. This can be done as follows:

Mirroring a Picture

namespace Mirror
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnMirror_Click(object sender, EventArgs e)
        {
            Graphics graph = pbxSource.CreateGraphics();
            Bitmap bmpSource = (Bitmap)pbxSource.Image;
            Bitmap bmpDestination =
                new Bitmap(pbxDestination.ClientSize.Width,
                           pbxDestination.ClientSize.Height);

            int width = bmpSource.Width;
            int height = bmpSource.Height;

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Color clr = bmpSource.GetPixel(x, y);
                    bmpDestination.SetPixel(width - x - 1, y, clr);
                }
            }

            pbxDestination.Image = bmpDestination;
        }
    }
}

Mirroring a Picture

Instead of writing you own code, to assist you with picture mirroring, the Bitmap class inherits a method named RotateFlip from its parent Image class. Its syntax is:

public void RotateFlip(RotateFlipType rotateFlipType);

This function takes one argument that specifies the mirroring option through the RotateFlipType enumeration. The members of the RotateFlipType enumeration that can be used to mirror a picture are RotateNoneFlipX and Rotate180FlipY. Here is an example of mirroring a picture:

private void btnManipulate_Click(object sender, EventArgs e)
{
    Bitmap bmpPicture = new Bitmap("person.jpg");
    bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipX);
    CreateGraphics().DrawImage(bmpPicture, 10, 10);
}

When this method is called, the bitmap horizontal direction would be changed.

Flipping a Picture

Flipping a picture consists of changing its vertical direction. For example you may have a picture that seems to be oriented down. By flipping, you can make the picture point up:

Flipping a Picture

To flip a picture, get each of its pixels from one side of its vertical position and transfer it to the opposite vertical position. The horizontal position of each pixel stays the same. This can be done as follows:

Flipping a Picture

namespace Flip
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnFlip_Click(object sender, EventArgs e)
        {
            Graphics graph = pbxSource.CreateGraphics();
            Bitmap bmpSource = (Bitmap)pbxSource.Image;
            Bitmap bmpDestination =
                new Bitmap(pbxDestination.ClientSize.Width,
                           pbxDestination.ClientSize.Height);

            int width = bmpSource.Width;
            int height = bmpSource.Height;
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Color clr = bmpSource.GetPixel(x, y);
                    bmpDestination.SetPixel(x, height - y - 1, clr);
                }
            }

            pbxDestination.Image = bmpDestination;
        }
    }
}

Flip

To support picture flipping, you can call the same RotateFlip() method of the Image class. This time, you would use a different value for the argument. The member of the RotateFlipType enumeration used to flip a picture is  RotateNoneFlipY. Here is an example of flipping a picture:

private void btnManipulate_Click(object sender, EventArgs e)
{
    Bitmap bmpPicture = new Bitmap("Pushpin.jpg");
    bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipY);
    CreateGraphics().DrawImage(bmpPicture, 10, 10);
}

Rotating a Picture

Rotating a picture consists of changing its angular orientation. The Image class supports this operation (with somewhat limited options) through its RotateFlip() method. As mentioned for mirroring and flipping, the value you pass as argument would determine how the operation is carried out.

The members of the RotateFlipType enumeration that are used to rotate a picture are:

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2010-2024, FunctionX Friday 01 Mars 2024 Next