Presentation is loading. Please wait.

Presentation is loading. Please wait.

MDI Picture Viewer Application

Similar presentations


Presentation on theme: "MDI Picture Viewer Application"— Presentation transcript:

1 MDI Picture Viewer Application

2 New Project - An MDI Picture Viewer

3 Main Form Settings and Controls
New Project - Windows Forms Application Menu Strip drop a MenuStrip onto the main form create tabs for File and Edit under File include New Open Save Save As Close we will work on Open and Save First create Click-Events for these two.... Property Setting Name FormMain IsMdiContainer True Text Whatever Name you Want WindowState Maximized

4 Adding a Child Form Click on the Project Tab and Select Add Windows Form frmChild Add a PictureBox control Property Setting Name frmChild Anchor Top,Bottom,Left,Right Dock Fill

5 The loadImage( ) Method
public void loadImage(string fname) { int minWidth = 100; int minHeight = 50; int maxWidth = 1024; int maxHeight = 800; Bitmap img = new Bitmap(fname); picBox.Image = img; if (img.Width + 15 > minWidth) this.Width = img.Width + 15; else this.Width = minWidth; if (img.Height > minHeight) this.Height = img.Height + 40; this.Height = minHeight; if (img.Width > maxWidth) this.Width = maxWidth; if (img.Height > maxHeight) this.Height = maxHeight; } This method is placed in the frmChild Code and resets the Child form size based on the picture. makes sure the Child form is not too small in case a very small image is loaded. The maximum size of the Child form is set to1024 x 800, which means that images larger than this will be clipped. We can manually resize the Child form but we may load pictures larger than our computer monitor can display.

6 Dealing with Very Large Images
We can add scrollbars to the image. remove the pictureBox and add a Panel to the Child Form...set the following properties: AutoSize = true Dock = Fill add a pictureBox (called picBox) to the Panel with the following property SizeMode = AutoSize leave all other properties in their default settings

7

8 Opening an Image in a Child Form
private void openImage() { string fileName = ""; OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "JPEG files (*.jpg)|*.jpg|GIF files (*.gif)|*.gif|Bitmap files (*.bmp)|*.bmp|PNG files (*.png)|*.png"; if (dlg.ShowDialog() == DialogResult.OK) fileName = dlg.FileName; Bitmap img = new Bitmap(fileName); makeNewMdiChild(); frmChild activeChild = this.ActiveMdiChild as frmChild; activeChild.Text = fileName; activeChild.loadImage(fileName); } private void openToolStripMenuItem_Click(object sender, EventArgs e) openImage(); private void makeNewMdiChild() PictureViewer.frmChild child = new PictureViewer.frmChild(this); child.Show(); this.ActiveMdiChild.Text = "New-File-" + Convert.ToString(newindex); newindex += 1; permits us to access .jpg, .gif, .bmp, and .png image files converts incoming image into a bitmap opens a Child form renames the Child form to the image path/filename note that in this case we have created a separate method named openImage( ) that is called inside the Click-Event for Open under the File tab...the code in openImage( ) could have been placed directly into the openToolStripMenuItem_Click( ) method.

9 Example Execution

10 Adding Scrollbars to the Picture Box
Sometimes an image will be larger than the Child form window or even the computer monitor. In these cases we will want to be able to scroll the image to see it all. In order to make the image scrollable you can place the pictureBox inside a panel with the following property settings… panel1 AutoScroll True AutoSize True Dock Fill picBox Dock None SizeMode AutoSize


Download ppt "MDI Picture Viewer Application"

Similar presentations


Ads by Google