|
Practical
Learning: Creating the Application
|
|
- Start a new Windows Forms Application named PictureViewer2
- Change the properties of the form as follows:
Text: Picture Viewer
StartPosition: CenterScreen
- On the main menu, click Project -> Add New Item...
- In the Add New Item dialog box, click Icon File
- Set the Name to openpicture and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:
- On the main menu, click File -> Save openpicture.ico As...
- Select the PictureViewer2\PictureViewer2\bin\Debug folder
- Click Save
- In the Solution Explorer, expand the bin\Debug folder. Right-click the
Debug folder -> Add -> New Item...
- Select Icon File. Set the Name to f the project -> Add ->
Resource...
- In the Add Resource dialog box, double-click Icon flip and
click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to mirror.ico and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to rotate90right and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to rotate90left and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to rotate180 and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to fliprotate90right and click
Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to fliprotate90left and click
Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to fliprotate180 and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- From the Menus & Toolbars section of the Toolbox, click MenuStrip and
click the form
- While the menu strip is still selected under the form, in the
Properties window, click (Name) and type mnuMain
- Use the menu designer and/or the Items field of the Properties
window to create the menu items as follows:
| Menu Top |
Sub Items/DropDownItems |
| Text |
Name |
Text |
Name |
Image |
| &File |
mnuFile |
&Open Picture... |
mnuFileOpenPicture |
openpicture.ico |
| |
|
Separator |
|
|
| |
|
E&xit |
mnuFileExit |
|
 |
| Menu Top |
Sub Items/DropDownItems |
| Text |
Name |
Text |
Name |
Image |
| &Tools |
mnuTools |
&Flip |
mnuToolsFlip |
flip.ico |
| |
|
&Mirror |
mnuToolsMirror |
mirror.ico |
| |
|
Separator |
|
|
| |
|
&Rotate |
mnuToolsRotate |
|
| |
|
Flip && R&otate |
mnuToolsFlipRotate |
|
|

|
| Sub Item |
Sub Items/DropDownItems |
| Text |
Text |
Name |
Image |
| Rotate |
90º |
mnuToolsRotate90 |
|
| |
180º |
mnuToolsRotate180 |
rotate180.ico |
| Flip & Rotate |
90º |
mnuToolsFlipRotate90 |
|
| |
180º |
mnuToolsFlipRotate180 |
fliprotate180.ico |
|

 |
| Sub Item |
Sub Item |
Sub Items/DropDownItems |
| Text |
Text |
Text |
Name |
Image |
| Rotate |
90º |
&Left |
mnuToolsRotate90Left |
rotate90left.ico |
| |
|
&Right |
mnuToolsRotate90Right |
rotate90right.ico |
| Flip & Rotate |
90º |
&Left |
mnuToolsFlipRotate90Left |
fliprotate90left.ico |
| |
|
&Right |
mnuToolsFlipRotate90Right |
fliprotate90right.ico |
|

 |
| Menu Top |
Sub Item |
| Text |
Text |
Name |
| &Tools |
Flip & Rotate |
|
| |
Separator |
|
| |
&Original Size |
mnuOriginalSize |
| |
Ce&nter Image |
mnuCenterImage |
| |
R&esize Form to Fit Picture |
mnuResizeForm |
| |
O&ccupy Client Area |
mnuOccupyClientArea |
| |
&Zoom |
mnuZoom |
|
 |
- From the Common Controls section of the Toolbox, click PictureBox
and click the form
- In the Properties window, change its characteristics as follows:
BorderStyle: Fixed3D
(Name): pbxViewer
Dock: Fill
- You can copy the following pictures and add them to a folder on your
computer (for example the PictureViewer2\PictureViewer2\bin\Debug
folder of the current project)
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type PictureViewer.cs and press Enter twice
- Right-click the form and click View Code
- Declare a private Bitmap variable named bmpPicture
namespace PictureViewer2
{
public partial class Form1 : Form
{
Bitmap bmpPicture;
public Form1()
{
InitializeComponent();
}
}
}
|
- Return to the form
- On top of the Properties window, click the arrow of the combo box
and select PictureViewer
- Click the Events button
and double-click Paint
- Implement the event as follows:
private void PictureViewer_Paint(object sender, PaintEventArgs e)
{
if( bmpPicture != null )
{
pbxViewer.Image = bmpPicture;
}
}
|
- Return to the form
- Click Tools and double-click Flip
- Implement the event as follows:
private void mnuToolsFlip_Click(object sender, EventArgs e)
{
if( bmpPicture != null )
{
bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipY);
Invalidate();
}
}
|
- Return to the form, click Tools and double-click Mirror
- Implement the event as follows:
private void mnuToolsMirror_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipX);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Rotate, then
click 90º and double-click Left
- Implement the event as follows:
private void mnuToolsRotate90Left_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate270FlipNone);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Rotate, then
click 90º and double-click Right
- Implement the event as follows:
private void mnuToolsRotate90Right_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Rotate, and
double-click 180º
- Implement the event as follows:
private void mnuToolsRotate180_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate180FlipNone);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Flip &
Rotate, then click 90º and double-click Left
- Implement the event as follows:
private void mnuToolsFlipRotate90Left_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate270FlipX);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Flip &
Rotate, then click 90º and double-click Right
- Implement the event as follows:
private void mnuToolsFlipRotate90Right_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipX);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Flip &
Rotate, and double-click 180º
- Implement the event as follows:
private void mnuToolsFlipRotate180_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate180FlipX);
Invalidate();
}
}
|
- Return to the form, click Tools and double-click Original Size
- Implement its event as follows:
private void mnuToolsOriginalSize_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
pbxViewer.SizeMode = PictureBoxSizeMode.Normal;
mnuOriginalSize.Checked = true;
mnuCenterImage.Checked = false;
mnuOccupyClientArea.Checked = false;
mnuZoom.Checked = false;
}
}
|
- Return to the form, click Tools and double-click Center Image
- Implement its event as follows:
private void mnuToolsCenterImage_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
pbxViewer.SizeMode = PictureBoxSizeMode.CenterImage;
mnuOriginalSize.Checked = false;
mnuCenterImage.Checked = true;
mnuOccupyClientArea.Checked = false;
mnuZoom.Checked = false;
}
}
|
- Return to the form, click Tools and double-click Occupy Client Area
- Implement its event as follows:
private void mnuOccupyClientArea_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
pbxViewer.SizeMode = PictureBoxSizeMode.StretchImage;
mnuOriginalSize.Checked = false;
mnuCenterImage.Checked = false;
mnuOccupyClientArea.Checked = true;
mnuZoom.Checked = false;
}
}
|
- Return to the form, click Tools and double-click Zoom
- Implement its event as follows:
private void mnuZoom_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
pbxViewer.SizeMode = PictureBoxSizeMode.Zoom;
mnuOriginalSize.Checked = false;
mnuCenterImage.Checked = false;
mnuOccupyClientArea.Checked = false;
mnuZoom.Checked = true;
}
}
|
- Return to the form
- From the Dialogs section of the Toolbox, click OpenFileDialog
and click the form
- While the open file control is still selected under the form, in the
Properties window, click (Name) and type ofdPicture
- Still in the Properties window for the open file control, click
FileName and delete its string
- Still in the Properties window for the open file control, click
Filter and type:
Bitmap Files (*.bmp)|*.bmp|JPEG Files (*.jpg,*.jpeg)|*.jpg|GIF Files
(*.gif)|*.gif|PNG Files (*.png)|*.png
- Save the form
- Still in the Properties window for the open file control, click
DefaultExt and type bmp
- Save the form
- On the form, click File and double-click Open Picture
- Implement the event as follows:
private void mnuFileOpenPicture_Click(object sender, EventArgs e)
{
if (ofdPicture.ShowDialog() == DialogResult.OK)
{
bmpPicture = new Bitmap(ofdPicture.FileName);
Invalidate();
mnuToolsOriginalSize_Click(sender, e);
}
}
|
- Return to the form
- Click Tools and double-click Exit
- Implement the event as follows:
private void mnuFileExit_Click(object sender, EventArgs e)
{
Close();
}
|
- Execute the application
- Open a picture and use the menu items
- Close the form and return to your programming environment
|
|