Csharp/C Sharp/GUI Windows Form/PictureBox — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:32, 26 мая 2010

Dragging Images

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MainForm : Form {
    private PictureBox happyBox = new PictureBox();
    private int oldX, oldY;
    private bool isDragging;
    private Rectangle dropRect = new Rectangle(100, 100, 140, 170);
    public MainForm() {
        happyBox.SizeMode = PictureBoxSizeMode.StretchImage;
        happyBox.Location = new System.Drawing.Point(64, 32);
        happyBox.Size = new System.Drawing.Size(50, 50);
        happyBox.Cursor = Cursors.Hand;
        happyBox.Image = new Bitmap("happyDude.bmp");
        happyBox.MouseDown += new MouseEventHandler(happyBox_MouseDown);
        happyBox.MouseUp += new MouseEventHandler(happyBox_MouseUp);
        happyBox.MouseMove += new MouseEventHandler(happyBox_MouseMove);
        Controls.Add(happyBox);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 361);
        this.Text = "Dragging Images";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
    }
    void happyBox_MouseMove(object sender, MouseEventArgs e) {
        if (isDragging) {
            happyBox.Top = happyBox.Top + (e.Y - oldY);
            happyBox.Left = happyBox.Left + (e.X - oldX);
        }
    }
    void happyBox_MouseUp(object sender, MouseEventArgs e) {
        isDragging = false;
        if (dropRect.Contains(happyBox.Bounds))
            MessageBox.Show("You win!", "What an amazing test of skill...");
    }
    void happyBox_MouseDown(object sender, MouseEventArgs e) {
        isDragging = true;
        oldX = e.X;
        oldY = e.Y;
    }
    private void MainForm_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.BlueViolet, dropRect);
        g.DrawString("Drag the happy guy in here...",new Font("Times New Roman", 25), Brushes.WhiteSmoke, dropRect);
    }
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new MainForm());
    }
}


Load image to ImageBox

  using System;
  using System.Drawing;
  using System.Windows.Forms;
  public class MyForm : System.Windows.Forms.Form
  {
    private Button btnLoad;  
    private PictureBox imgPhoto;
    public MyForm()
    {
      this.Text = "www.nfex.ru";
      btnLoad = new Button();
      btnLoad.Text = "&Load";
      btnLoad.Left = 10;
      btnLoad.Top = 10;
      btnLoad.Click += new System.EventHandler(this.OnLoadClick);
      imgPhoto = new PictureBox();
      imgPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      imgPhoto.Width = this.Width / 2;      
      imgPhoto.Height = this.Height / 2;      
      imgPhoto.Left = (this.Width - imgPhoto.Width) / 2;
      imgPhoto.Top = (this.Height - imgPhoto.Height) / 2;
      imgPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
      this.Controls.Add(btnLoad);
      this.Controls.Add(imgPhoto);  
    }
    protected void OnLoadClick(object sender, System.EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog();
      dlg.Title = "Open Image";
      dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;
      if (dlg.ShowDialog() == DialogResult.OK)
      {
        imgPhoto.Image = new Bitmap(dlg.OpenFile());
      }
      dlg.Dispose();
    }

    public static void Main()
    {
      Application.Run(new MyForm());
    }
  }


PictureBox Click event

   using System;
   using System.Drawing;
   using System.Collections;
   using System.ruponentModel;
   using System.Windows.Forms;
   using System.Data;
   using System.IO;
   public class PictureBoxTest : System.Windows.Forms.Form
   {
      private System.Windows.Forms.PictureBox imagePictureBox;
      private System.Windows.Forms.Label promptLabel;
      public PictureBoxTest()
      {
         InitializeComponent();
      }
      private void InitializeComponent()
      {
         this.promptLabel = new System.Windows.Forms.Label();
         this.imagePictureBox = new System.Windows.Forms.PictureBox();
         this.SuspendLayout();
         this.promptLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.promptLabel.Location = new System.Drawing.Point(22, 8);
         this.promptLabel.Name = "promptLabel";
         this.promptLabel.Size = new System.Drawing.Size(124, 56);
         this.promptLabel.TabIndex = 0;
         this.promptLabel.Text = "Click On PictureBox to View Images";
         this.promptLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
         this.imagePictureBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
         this.imagePictureBox.Location = new System.Drawing.Point(34, 72);
         this.imagePictureBox.Name = "imagePictureBox";
         this.imagePictureBox.Size = new System.Drawing.Size(100, 100);
         this.imagePictureBox.TabIndex = 1;
         this.imagePictureBox.TabStop = false;
         this.imagePictureBox.Click += new System.EventHandler(this.imagePictureBox_Click );
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(168, 189);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {this.imagePictureBox,
                                                                      this.promptLabel});
         this.Text = "PictureBoxTest";
         this.ResumeLayout(false);
      }
      [STAThread]
      static void Main() 
      {
         Application.Run( new PictureBoxTest() );
      }
      private void imagePictureBox_Click(object sender, System.EventArgs e )
      {
         imagePictureBox.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\winter.jpg" );
      }
   }


PictureBox Demo

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace BictureBox
{
    /// <summary>
    /// Summary description for PictureBoxDemo.
    /// </summary>
    public class PictureBoxDemo : System.Windows.Forms.Form
    {
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.PictureBox pictureBox3;
        private System.Windows.Forms.PictureBox pictureBox4;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public PictureBoxDemo()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.Text = "SizeMode in PictureBox";
            this.button1.Text = "Display";
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <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.button1 = new System.Windows.Forms.Button();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.pictureBox3 = new System.Windows.Forms.PictureBox();
            this.pictureBox4 = new System.Windows.Forms.PictureBox();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox1.Location = new System.Drawing.Point(8, 8);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(100, 70);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(232, 24);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(56, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // pictureBox2
            // 
            this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox2.Location = new System.Drawing.Point(120, 8);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(100, 70);
            this.pictureBox2.TabIndex = 0;
            this.pictureBox2.TabStop = false;
            // 
            // pictureBox3
            // 
            this.pictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox3.Location = new System.Drawing.Point(8, 88);
            this.pictureBox3.Name = "pictureBox3";
            this.pictureBox3.Size = new System.Drawing.Size(100, 70);
            this.pictureBox3.TabIndex = 0;
            this.pictureBox3.TabStop = false;
            // 
            // pictureBox4
            // 
            this.pictureBox4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox4.Location = new System.Drawing.Point(120, 88);
            this.pictureBox4.Name = "pictureBox4";
            this.pictureBox4.Size = new System.Drawing.Size(100, 70);
            this.pictureBox4.TabIndex = 0;
            this.pictureBox4.TabStop = false;
            // 
            // PictureBoxDemo
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(376, 254);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.button1,
                                                                          this.pictureBox2,
                                                                          this.pictureBox3,
                                                                          this.pictureBox4,
                                                                          this.pictureBox1});
            this.Name = "PictureBoxDemo";
            this.Text = "PictureBoxDemo";
            this.ResumeLayout(false);
        }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new PictureBoxDemo());
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            SetPictureBoxSizeMode();
        }
        private void SetPictureBoxSizeMode()
        {
            string path = @"Dobos3.BMP";  // Change the path if needed.
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            pictureBox1.Image = Image.FromFile(path);
         
            pictureBox2.SizeMode = PictureBoxSizeMode.Normal;
            pictureBox2.Image = Image.FromFile(path);
         
            pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox3.Image = Image.FromFile(path);
            pictureBox4.SizeMode = PictureBoxSizeMode.AutoSize;
            pictureBox4.Image = Image.FromFile(path);
        }
    }
}

<A href="http://www.nfex.ru/Code/CSharpDownload/P17_PictureBox.zip">P17_PictureBox.zip( 103 k)</a>


PictureBox Scroll Text

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    Font textFont = new Font("Times New Roman", 24);
    public Form1() {
        this.panel1 = new System.Windows.Forms.Panel();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.panel1.SuspendLayout();
        ((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        this.panel1.AutoScroll = true;
        this.panel1.AutoScrollMinSize = new System.Drawing.Size(600, 400);
        this.panel1.Controls.Add(this.pictureBox1);
        this.panel1.Location = new System.Drawing.Point(13, 13);
        this.panel1.Size = new System.Drawing.Size(267, 243);
        this.pictureBox1.BackColor = System.Drawing.SystemColors.Window;
        this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Size = new System.Drawing.Size(600, 400);
        this.pictureBox1.TabStop = false;
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 268);
        this.Controls.Add(this.panel1);
        this.Load += new System.EventHandler(this.Form1_Load);
        this.panel1.ResumeLayout(false);
        ((System.ruponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
    }
    private void Form1_Load(object sender, EventArgs e) {
        Bitmap b = new Bitmap(600, 600);
        Graphics g = Graphics.FromImage(b);
        g.FillRectangle(Brushes.White, new Rectangle(0, 0, b.Width, b.Height));
        g.DrawString("Hello, World", textFont, Brushes.Black, 40, 40);
        g.DrawString("Hello, World", textFont, Brushes.Red, 40, 240);
        g.DrawString("Hello, World", textFont, Brushes.Blue, 350, 40);
        g.DrawString("Hello, World", textFont, Brushes.Green, 350, 240);
        pictureBox1.BackgroundImage = b;
        pictureBox1.Size = b.Size;
    }
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.PictureBox pictureBox1;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}


PictureBox visible and invisible

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class ListText : Form {
 
  private Button choose = new Button();
  private ListBox animalList = new ListBox( );
  private ListBox things = new ListBox( );
  private TextBox text = new TextBox( );
  private PictureBox picture = new PictureBox();
  private PictureBox photo = new PictureBox();
  private CheckBox show = new CheckBox();
  private CheckBox author = new CheckBox();
 
  private Bitmap moon = new Bitmap("winter.jpg");
 
  public ListText( ) {
    Text = "List Text";
    choose.Text = "Choose";
    show.Text = "Show a bitmap";
    author.Text = "Show another bitmap";
 
    Size = new Size(400, 300);
    choose.Size = new Size(100,20);
    text.Size = new Size(150,50);
    photo.Size = new Size(100,100);
 
    choose.Location = new Point(20,30);
    animalList.Location = new Point(30 + choose.Width, 30);
    things.Location = new Point(40 + choose.Width + animalList.Width, 30);
    text.Location = new Point(20, 150);
    photo.Location = new Point(40 + text.Width, 150);
    picture.Location = new Point(60 + text.Width + photo.Width, 150);
    show.Location = new Point(20,70);
    author.Location = new Point(20,110);
 
    animalList.SelectionMode = SelectionMode.MultiSimple; 
    things.SelectionMode = SelectionMode.One;
    text.Multiline = true;                             
    picture.Image = (Image)moon;                       
    picture.Visible = false;     
    photo.Image = Image.FromFile("winter.jpg");
    photo.Visible = false;
    BackColor = Color.White;
    choose.BackColor = Color.Pink;
 
    animalList.Items.Add("A");
    animalList.Items.Add("B");
    animalList.Items.Add("C");
    animalList.Items.Add("D");
    animalList.Items.Add("E");
    things.Items.Add("1");
    things.Items.Add("2");
    things.Items.Add("3");
    things.Items.Add("4");
 
    Controls.Add(animalList);
    Controls.Add(things);
    Controls.Add(choose);
    Controls.Add(text);
    Controls.Add(picture);
    Controls.Add(show);
    Controls.Add(author);
    Controls.Add(photo);
 
    choose.Click += new EventHandler(Choose_Click);
    things.SelectedIndexChanged += new EventHandler(Things_Changed);
    show.CheckedChanged += new EventHandler(Picture_Changed);
    author.CheckedChanged += new EventHandler(Photo_Changed);
  }
 
  protected void Choose_Click(object sender, EventArgs e) {
    for(int i = 0; i < animalList.SelectedItems.Count; i++){
      Console.WriteLine(animalList.SelectedItems[i].ToString());
    }
  }
 
  protected void Things_Changed(object sender, EventArgs e) {
    text.Text = "You selected " + things.SelectedItem;
  }
 
  protected void Picture_Changed(Object sender, EventArgs e) {
    if (show.Checked)
      picture.Visible = true;
    else
      picture.Visible = false;
    Invalidate();
  }
 
  protected void Photo_Changed(Object sender, EventArgs e) {
    if (author.Checked)
      photo.Visible = true;
    else
      photo.Visible = false;
    Invalidate();
  }
  static void Main() {
     Application.Run(new ListText());
  }
}


SizeMode in PictureBox

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.PictureBox pictureBox2;
    private System.Windows.Forms.PictureBox pictureBox3;
    private System.Windows.Forms.PictureBox pictureBox4;
    public Form1() {
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.button1 = new System.Windows.Forms.Button();
        this.pictureBox2 = new System.Windows.Forms.PictureBox();
        this.pictureBox3 = new System.Windows.Forms.PictureBox();
        this.pictureBox4 = new System.Windows.Forms.PictureBox();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pictureBox1.Location = new System.Drawing.Point(8, 8);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(100, 70);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(232, 24);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(56, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "button1";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // pictureBox2
        // 
        this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pictureBox2.Location = new System.Drawing.Point(120, 8);
        this.pictureBox2.Name = "pictureBox2";
        this.pictureBox2.Size = new System.Drawing.Size(100, 70);
        this.pictureBox2.TabIndex = 0;
        this.pictureBox2.TabStop = false;
        // 
        // pictureBox3
        // 
        this.pictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pictureBox3.Location = new System.Drawing.Point(8, 88);
        this.pictureBox3.Name = "pictureBox3";
        this.pictureBox3.Size = new System.Drawing.Size(100, 70);
        this.pictureBox3.TabIndex = 0;
        this.pictureBox3.TabStop = false;
        // 
        // pictureBox4
        // 
        this.pictureBox4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pictureBox4.Location = new System.Drawing.Point(120, 88);
        this.pictureBox4.Name = "pictureBox4";
        this.pictureBox4.Size = new System.Drawing.Size(100, 70);
        this.pictureBox4.TabIndex = 0;
        this.pictureBox4.TabStop = false;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(376, 254);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button1,
                                      this.pictureBox2,
                                      this.pictureBox3,
                                      this.pictureBox4,
                                      this.pictureBox1});
        this.ResumeLayout(false);
        this.button1.Text = "Display";
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void button1_Click(object sender, System.EventArgs e) {
        SetPictureBoxSizeMode();
    }
    private void SetPictureBoxSizeMode() {
        string path = "3.BMP";  // Change the path if needed.
        pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        pictureBox1.Image = Image.FromFile(path);
        pictureBox2.SizeMode = PictureBoxSizeMode.Normal;
        pictureBox2.Image = Image.FromFile(path);
        pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox3.Image = Image.FromFile(path);
        pictureBox4.SizeMode = PictureBoxSizeMode.AutoSize;
        pictureBox4.Image = Image.FromFile(path);
    }
}


Subclass PictureBox

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class PictureBoxPlusDemo: Form
{
     public static void Main()
     {
          Application.Run(new PictureBoxPlusDemo());
     }
     public PictureBoxPlusDemo()
     {
          Text = "PictureBoxPlus Demo";
   
          PictureBoxPlus picbox = new PictureBoxPlus();
          picbox.Parent = this;
          picbox.Dock = DockStyle.Fill;
          picbox.Image = Image.FromFile("Color.jpg");
     }
}
     class PictureBoxPlus: PictureBox
     {
          protected override void OnPaint(PaintEventArgs pea)
          {
               ScaleImageIsotropically(pea.Graphics, Image,ClientRectangle);
          }
          void ScaleImageIsotropically(Graphics grfx, Image image,
                                       Rectangle rect)
          {
               SizeF sizef = 
                    new SizeF(image.Width / image.HorizontalResolution,
                              image.Height / image.VerticalResolution);
   
               float fScale = Math.Min(rect.Width  / sizef.Width,
                                       rect.Height / sizef.Height);
   
               sizef.Width  *= fScale;
               sizef.Height *= fScale;
          
               grfx.DrawImage(image,
                              rect.X + (rect.Width  - sizef.Width ) / 2,
                              rect.Y + (rect.Height - sizef.Height) / 2,
                              sizef.Width, sizef.Height);
          }
     }