I need to find an example of making a window with a menu bar that just displays an image. The Image must resize if the window is resized and also provide scrolling of the image at 100% if an option is turned on in the menu.
I've written one - see the end of this message. Is that what you required?
-- Ian Griffiths - http://www.interact-sw.co.uk/iangblog/ DevelopMentor - http://www.develop.com/
using System; using System.IO; using System.Drawing; using System.Windows.Forms;
namespace ScrollImage { public class ImageForm : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuFile; private System.Windows.Forms.MenuItem menuFileOpen; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuExit; private System.Windows.Forms.MenuItem menuView; private System.Windows.Forms.MenuItem menuNaturalSize; private System.Windows.Forms.OpenFileDialog openImageFileDialog; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null;
public ImageForm() { // // Required for Windows Form Designer support // InitializeComponent();
UpdateNaturalSize(); } private bool naturalSize;
private void UpdateNaturalSize() { menuNaturalSize.Checked = naturalSize; if (naturalSize) { pictureBox1.Dock = DockStyle.None; pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; AutoScroll = true; } else { AutoScroll = false; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Dock = DockStyle.Fill; } }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuFile = new System.Windows.Forms.MenuItem(); this.menuFileOpen = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.menuExit = new System.Windows.Forms.MenuItem(); this.menuView = new System.Windows.Forms.MenuItem(); this.menuNaturalSize = new System.Windows.Forms.MenuItem(); this.openImageFileDialog = new System.Windows.Forms.OpenFileDialog(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(292, 270); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFile,
this.menuView}); // // menuFile // this.menuFile.Index = 0; this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFileOpen,
this.menuItem3,
this.menuExit}); this.menuFile.Text = "&File"; // // menuFileOpen // this.menuFileOpen.Index = 0; this.menuFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO; this.menuFileOpen.Text = "&Open..."; this.menuFileOpen.Click += new System.EventHandler(this.menuFileOpen_Click); // // menuItem3 // this.menuItem3.Index = 1; this.menuItem3.Text = "-"; // // menuExit // this.menuExit.Index = 2; this.menuExit.Text = "E&xit"; // // menuView // this.menuView.Index = 1; this.menuView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuNaturalSize}); this.menuView.Text = "&View"; // // menuNaturalSize // this.menuNaturalSize.Index = 0; this.menuNaturalSize.Shortcut = System.Windows.Forms.Shortcut.CtrlL; this.menuNaturalSize.Text = "&Natural Size"; this.menuNaturalSize.Click += new System.EventHandler(this.menuNaturalSize_Click); // // ImageForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 270); this.Controls.Add(this.pictureBox1); this.Menu = this.mainMenu1; this.Name = "ImageForm"; this.Text = "ImageForm"; this.ResumeLayout(false);
} #endregion
/// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new ImageForm()); }
private void menuFileOpen_Click(object sender, System.EventArgs e) { if (openImageFileDialog.ShowDialog() == DialogResult.OK) { try { Image img; using (Stream imageStream = openImageFileDialog.OpenFile()) { img = Image.FromStream(imageStream); } pictureBox1.Image = img; } catch (Exception x) { MessageBox.Show("Error opening file: " + x.Message, "Error"); } } }
private void menuNaturalSize_Click(object sender, System.EventArgs e) { naturalSize = !naturalSize; UpdateNaturalSize(); } } }
=================================== This list is hosted by DevelopMentorŪ http://www.develop.com Some .NET courses you may be interested in:
Essential .NET: building applications and components with C# November 29 - December 3, in Los Angeles http://www.develop.com/courses/edotnet
View archives and manage your subscription(s) at http://discuss.develop.com