Operations on Bitmaps
Operations on Bitmaps
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 Learning: Introducing .NET Framework Collections
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:
Control | Name | Other Properties | |
Label | New &Width: | ||
TextBox | txtWidth | ||
Label | New &Height: | ||
TextBox | txtHeight | ||
Button | btnResizePicture | Resize the Picture | |
Button | btnOpenPicture | Open a Picture... | |
PictureBox | pbxCanvas | BorderStyle: FixedSingle | |
OpenFileDialog | 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 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:
Control | Name | Other Properties | |
PictureBox | pbxCanvas | BorderStyle: Fixed3D | |
VScrollBar | vsbPicture | Maximum: 345 Value: 345 Anchor: Top, Bottom, Right |
|
HScrollBar | 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(); } } }
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:
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:
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; }
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:
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:
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; } } }
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:
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:
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; } } }
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:
Original | After Calling the Method | |
=> | ||
Notice that, after the method has been called, the person is still holding the remote control with his right hand. Here is an example:
private void btnRotate_Click(object sender, EventArgs e)
{
Bitmap bmpPicture = new Bitmap("person1.gif");
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
CreateGraphics().DrawImage(bmpPicture, 10, 10);
} |
Original | |
After Calling the Method | |
Notice that, after the method has been called, the person is still holding the remote control with his right hand. As a result, the remote control is on opposite sites of the pictures, indicating that it is the same hand. You can get the same result by calling the method twice with the Rotate90FlipNone member each time: private void btnRotate_Click(object sender, EventArgs e)
{
Bitmap bmpPicture = new Bitmap("person1.gif");
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
CreateGraphics().DrawImage(bmpPicture, 10, 10);
} |
Original | After Calling the Method | |
=> | ||
Notice that, after the method has been called, the person is still holding the remote control with his right hand. You can get the same result by calling the method three times with the Rotate90FlipNone member each time: private void btnRotate_Click(object sender, EventArgs e)
{
Bitmap bmpPicture = new Bitmap("person1.gif");
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
CreateGraphics().DrawImage(bmpPicture, 10, 10);
} |
Original | |
After Calling the Method |
Original | After Calling the Method | |
=> | ||
Notice that, after the method has been called, the person is now holding the remote control with his left hand |
Original | |
After Calling the Method | |
Notice that, after the method has been called, the person is now holding the remote control with his left hand. As a consequence, the remote control is on the same side of the screen on both pictures (compare with Rotate180FlipNone) |
Original | After Calling the Method | |
=> | ||
Notice that, after the method has been called, the person is now holding the remote control with his left hand |
Practical Learning: Ending the Lesson
|
|||
Previous | Copyright © 2010-2024, FunctionX | Friday 01 Mars 2024 | Next |
|